开发者

How to rewrite the code more elegant

I wrote this function. The input and expected results are indicated in the docstring.

def summarize_significance(sign_list):
    """Summarizes a series of individual significance data in a list of ocurrences.

    For a group of p.e. 5 measurements and two diferent states, the input data
    has the form:

    sign_list = [[-1, 1],
                 [0, 1],
                 [0, 0],
                 [0,-1],
                 [0,-1]]

    where -1, 0, 1  indicates decrease, no change or increase respectively.
    The result is a list of 3 items lists indicating how many measurements
    decrease, do not change or increase (as list items 0,1,2 respectively) for each state:

    returns: [[1, 4, 0], [2, 1, 2]]

    """
    swaped = numpy.swapaxes(sign_list, 0, 1)

    summary = []
    for row in swaped:
        mydd = defaultdict(int)
        for item in row:
            mydd[item] += 1
        summary.append([mydd.get(-1开发者_JAVA技巧, 0), mydd.get(0, 0), mydd.get(1, 0)])

    return summary

I am wondering if there is a more elegant, efficient way of doing the same thing. Some ideas?


Here's one that uses less code and is probably more efficient because it just iterates through sign_list once without calling swapaxes, and doesn't build a bunch of dictionaries.

summary = [[0,0,0] for _ in sign_list[0]]

for row in sign_list:
  for index,sign in enumerate(row):
    summary[index][sign+1] += 1
return summary


No, just more complex ways of doing so.

import itertools

def summarize_significance(sign_list):
  res = []
  for s in zip(*sign_list):
    d = dict((x[0], len(list(x[1]))) for x in itertools.groupby(sorted(s)))
    res.append([d.get(x, 0) for x in (-1, 0, 1)])
  return res


For starters, you could do:

swapped = numpy.swapaxes(sign_list, 0, 1)
for row in swapped:
  mydd = {-1:0, 0:0, 1:0}
  for item in row:
     mydd[item] += 1
  summary.append([mydd[-1], mydd[0], mydd[1])
return summary
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜