Python 有没有更好的方法来建立这样的列表?

Python 有没有更好的方法来建立这样的列表?,python,list,zip,Python,List,Zip,我是Python新手,我很喜欢它,但我想知道是否有更好的方法来进行一些列表操作 这是一个相对理智的,但似乎我可能错过了一个内置的功能 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]]

我是Python新手,我很喜欢它,但我想知道是否有更好的方法来进行一些列表操作

这是一个相对理智的,但似乎我可能错过了一个内置的功能

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)
这本书很粗糙,很难阅读,有没有更干净或更具python风格的想法

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)

首先,为了更通俗一点,我会避免输入变异

其次,对于
zip_join2
函数,您可能需要更像
dict
的东西,而不是元组列表。像这样:

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

您可能还想考虑使用<代码> Debug TDC/<代码(从集合模块),以防第二个字典中有第一个关键字不存在。

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)

并行操作。

使用元组解包和布尔逻辑进行列表理解

zip\u 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]])
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])])
zip_join2:

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]])
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包含a为0,c为无的情况。
其中一些让我也感到畏缩。

这加上让我在伊兹普大学呆了很长时间的研究(因为我想在积累的基础上完成我的工作),看起来不错,谢谢!这也需要其他答案中的if块:返回[x+y如果x和y其他x或y表示(x,y)在izip_最长(第一,第二)]我不会说它“并行运行”。。。对我来说,这听起来像是将计算分散到多个核心上。我指的是链接文档中的这条评论:“函数……并行应用于所有iterables中的项”同样需要将相同样式的if应用于zip_join2版本。我也喜欢这个答案。是的,我不喜欢改变输入的想法,谢谢你的反馈。我也喜欢(a或[])+(b或[])习惯用法,可能也会在我的算法中使用它,谢谢你花时间。@Scott b,不客气。我喜欢这些类型的问题和人们提供的解决方案。我每天都在从你身上拿走一些东西。您使用<代码> DOCTest< /COD>风格的DROBACK启发了我考虑使用<代码> DOCTest更多而不仅仅是<代码> PyTest/UnTest< <代码>: