开发者

Is there a better way to build lists like this?

I'm new to Python and I am loving it, but was wondering if there was a better way to do a couple list manipulations.

This one is relatively sane, but it seems like I might have missed a built-in function.

def zip_plus(source_list, additional_list):
    """
    Like zip but does plus operation where zip makes a tuple

    >>> a = []
    >>> zip_plus(a, [[1, 2], [3, 4]])
    >>> a
    [[1, 2], [3, 4]]
    >>> zip_plus(a, [[11, 12], [13, 14]])
    >>> a
    [[1, 2, 11, 12], [3, 4, 13, 14]]
    """
    if source_list:
        for i, v in enumerate(additional_list):
            source_list[i] += v
    else:
        source_list.extend(additional_list)

This one is hacky and hard to read, any ideas on doing it cleaner or more pythonic?

def zip_join2(source_list, additional_list):
    """
    Pretty gross and specialized function to combine 2 types of lists of things,
    specifically a list of tuples of something, list
    of which the somethin开发者_如何学编程g is left untouched

    >>> a = []
    >>> zip_join2(a, [(5, [1, 2]), (6, [3, 4])])
    >>> a
    [(5, [1, 2]), (6, [3, 4])]
    >>> zip_join2(a, [(5, [11, 12]), (6, [13, 14])])
    >>> a
    [(5, [1, 2, 11, 12]), (6, [3, 4, 13, 14])]
    """
    if source_list:
        for i, v in enumerate(additional_list):
            source_list[i] = (source_list[i][0], source_list[i][1] + v[1])
    else:
        source_list.extend(additional_list)


def zip_plus(first, second):
    return [x+y for (x,y) in zip(first, second)]

def zip_join2(first, second):
    return [(x[0], x[1]+y[1]) for (x,y) in zip(first, second)]


First, to be more Pythonic, I'd avoid mutating inputs.

Second, you probably want something more like a dict instead of a list of tuples for your zip_join2 function. Like so:

>>> a = {5 : [1,2], 6 : [3,4]}
>>> b = {5 : [11,12], 6 : [13,14]}
>>> for k,v in b.iteritems():
...     a[k].extend(v)
>>> a = {5: [1,2,11,12], 6: [3,4,13,14]}

You might also want to consider using a defaultdict (from the collections module), in case the second dictionary has keys not present in the first.


List Comprehension using tuple unpacking and boolean logic.

zip_plus:

from itertools import izip_longest
def zip_plus(first, second):
    return [(a or []) + (b or []) for a, b in izip_longest(first, second)]

print zip_plus([], [[1, 2], [3, 4]])
print zip_plus([[1, 2], [3, 4]], [[11, 12], [13, 14]])
print zip_plus([[1, 2], [3, 4]], [[11, 12]])
print zip_plus([[1, 2]], [[11, 12], [13, 14]])

zip_join2:

from itertools import izip_longest
def zip_join2(first, second):
    return [(a or c or 0, (b or []) + (d or [])) for (a, b), (c, d) in \
              izip_longest(first, second, fillvalue=(None, None))]

print zip_join2([], [(5, [1, 2]), (6, [3, 4])])
print zip_join2([(5, [1, 2]), (6, [3, 4])], [(5, [11, 12]), (6, [13, 14])])

The 0 covers the case where a is 0 and c is None. Some of this makes me cringe too.


def zip_plus(source_list, additional_list):
  return map(lambda a, b: a + b if a and b else a or b, source_list, additional_list)

def zip_join2(source_list, additional_list):
  return map(lambda x, y: (x[0], x[1] + y[1]), source_list, additional_list)

map operates in parallel.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜