Python字符串拆分格式

时间:2018-06-11 09:00:29

标签: python-3.x

我有一个像这样的字符串列表,我想一起打印第一项和第二项,并在下一行第一和第三项一起打印] 1

2 个答案:

答案 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']]