Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用zip_加入2个列表,但删除没有组的组_Python_Python 3.x_Itertools - Fatal编程技术网

Python 使用zip_加入2个列表,但删除没有组的组

Python 使用zip_加入2个列表,但删除没有组的组,python,python-3.x,itertools,Python,Python 3.x,Itertools,我如何删除这些组中没有的组?除了zip_,还有其他选择吗 str1 = ['-', '+'] str2 = ['a','b','c'] list(itertools.zip_longest(str1, str2)) 输出: [('-','a'), ('+','b'), (None,'c')] 预期产出: [[-a], [+b]] zip\u longest()是常规内置的zip()的替代品,它将截断为您作为参数给出的最短列表: >>> str1 = ['-', '+']

我如何删除这些组中没有的组?除了zip_,还有其他选择吗

str1 = ['-', '+']
str2 = ['a','b','c']

list(itertools.zip_longest(str1, str2))
输出:

[('-','a'), ('+','b'), (None,'c')]
预期产出:

[[-a], [+b]]
zip\u longest()
是常规内置的
zip()
的替代品,它将截断为您作为参数给出的最短列表:

>>> str1 = ['-', '+']
>>> str2 = ['a','b','c']
>>> zipped = list(zip(str1, str2))
>>> print(zipped)
[('-', 'a'), ('+', 'b')]
>>> # the following more closely resembles your desired output
>>> condensed = [''.join(tup) for tup in zipped]
>>> print(condensed)
['-a', '+b']
请注意,您还可以为
itertools.zip\u longest()
提供一个关键字参数
fillvalue
,使其填充除
None
之外的内容:

>>> zipped_long = list(itertools.zip_longest(str1, str2, fillvalue='~'))
>>> print(zipped_long)
[('-', 'a'), ('+', 'b'), ('~', 'c')]
只需使用普通的旧
zip()
??