在列表Python3

时间:2017-04-21 14:59:50

标签: python list python-3.x

我试过itertools,map()但我不知道错了什么。我有这个:

[['>Fungi|A0A017STG4.1/69-603 UP-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', ['-', '-', '-', ... , '-', '-', '-', '-']],['>Fungi|A0A017STG4.1/69-603 UP1-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', ['-', '-', '-', ... , '-', '-', '-', '-']],['>Fungi|A0A017STG4.1/69-603 UP12-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', ['-', '-', '-', ... , '-', '-', '-', '-']]]

我想要这个:

[['>Fungi|A0A017STG4.1/69-603 UP-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}','-', '-', '-', ... , '-', '-', '-', '-'],['>Fungi|A0A017STG4.1/69-603 UP1-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}','-', '-', '-', ... , '-', '-', '-', '-'],['>Fungi|A0A017STG4.1/69-603 UP10-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}','-', '-', '-', ... , '-', '-', '-', '-']]

我试过

for i in x:
    map(i,[])

和这个

import itertools
a = [["a","b"], ["c"]]
print list(itertools.chain.from_iterable(a))

请开导我!

2 个答案:

答案 0 :(得分:1)

必须有更好的 Pythonic 解决方案,但您可以使用:

n = []
for x in your_list:
    temp_list = [x[0]]
    [temp_list.append(y) for y in x[1]]
    n.append(temp_list)

print(n)

输出:

[['>Fungi|A0A017STG4.1/69-603 UP-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', '-', '-', '-', Ellipsis, '-', '-', '-', '-'], ['>Fungi|A0A017STG4.1/69-603 UP1-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', '-', '-', '-', Ellipsis, '-', '-', '-', '-'], ['>Fungi|A0A017STG4.1/69-603 UP12-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', '-', '-', '-', Ellipsis, '-', '-', '-', '-']]

答案 1 :(得分:0)

简单的oneliner可以做到:

[sum(x, []) for x in yourlist]

注意sum(x,[])相当慢,因此对于严重的列表合并使用更多有趣和点亮的快速列表合并技术

join list of lists in python

例如,简单的两个衬垫更快

import itertools
map(list, (map(itertools.chain.from_iterable, yourlist)))