Input : [['1538', '1'], [False], [True], ['firm']]
Output : [('1538', False, True, 'firm'),
('1', False, True, 'firm')]
lzip仅给出第一行
In [91]: lzip(*[['1538', '1'], [False], [True], ['firm']])
Out[91]: [('1538', False, True, 'firm')]
也希望所有的args都是可迭代的。我希望这能处理即使输入像
[['1538', '1'], False, True, 'firm']
简单的方法是什么
答案 0 :(得分:2)
您可以使用itertools.product
from itertools import product
list(product(*[['1538', '1'], [False], [True], ['firm']]))
#[('1538', False, True, 'firm'), ('1', False, True, 'firm')]
答案 1 :(得分:2)
以ExplodingGayFish's answer为基础,如果您也希望能够处理第二种情况:
from itertools import product
Input = [['1538', '1'], [False], [True], ['firm']]
Input2 = [['1538', '1'], False, True, 'firm']
def sep(iterable):
new_iter = (item if isinstance(item,list) else [item] for item in iterable)
return list(product(*new_iter))
print(sep(Input))
print(sep(Input2))
输出:
[('1538', False, True, 'firm'), ('1', False, True, 'firm')]
[('1538', False, True, 'firm'), ('1', False, True, 'firm')]