我有一个像这样的字符串列表,我想一起打印第一项和第二项,并在下一行第一和第三项一起打印] 1
答案 0 :(得分:0)
试试这个:
import itertools
l = ['Vento Power', 'Quinoxyfan', 'Miclobutanil']
for i in [list(i) for i in itertools.combinations(l,r=2) if 'Vento Power' in i]:
print(i)
输出:
['Vento Power', 'Quinoxyfan']
['Vento Power', 'Miclobutanil']
答案 1 :(得分:0)
您可以尝试:
res=list([el[0], el_inner] for el in x for el_inner in el[1:])
x
是列表的输入列表
关于样本数据:
x=[["a", "b", "c", "x"], ["a", "g"], ["x", "y", "z"], ["x", "q"], ["a", "b", "c"]]
y=list([el[0], el_inner] for el in x for el_inner in el[1:])
print(x)
print(y)
输出:
[['a', 'b', 'c', 'x'], ['a', 'g'], ['x', 'y', 'z'], ['x', 'q'], ['a', 'b', 'c']]
[['a', 'b'], ['a', 'c'], ['a', 'x'], ['a', 'g'], ['x', 'y'], ['x', 'z'], ['x', 'q'], ['a', 'b'], ['a', 'c']]