Is there a better way to create lists?

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

This is relatively reasonable, but it looks like I might have missed the inline 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 hoarse and hard to read, any ideas for cleaning it 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 something 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)
+3
source share
4 answers

Description of the list using unpacking and logical 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])])

0 covers the case when a is 0 and c is None. Some of them make me cringe.

+1
source
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)]
+4
source

-, Pythonic, .

-, , , dict zip_join2. :

>>> 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]}

defaultdict ( ), , .

+3
source
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 works in parallel.

+1
source

Source: https://habr.com/ru/post/1780358/


All Articles