我有一个像这样的列表
[['N_ALA_A0001', [9.25, 24.41, 64.13]]]
[['O_ALA_A0001', [9.05, 21.51, 64.38]]]
[['N_ALA_A0001', [9.25, 24.41, 64.13]]]
[['N_TYR_A0002', [7.84, 21.93, 66.24]]]
[['N_ALA_A0001', [9.25, 24.41, 64.13]]]
[['O_TYR_A0002', [5.48, 20.92, 65.6]]]
[['N_ALA_A0001', [9.25, 24.41, 64.13]]]
[['N_VAL_A0003', [6.19, 18.75, 65.89]]]
[['N_ALA_A0001', [9.25, 24.41, 64.13]]]
我想删除冗余列表并打印uniq行
[['N_ALA_A0001', [9.25, 24.41, 64.13]]]
[['O_ALA_A0001', [9.05, 21.51, 64.38]]]
[['N_TYR_A0002', [7.84, 21.93, 66.24]]]
[['O_TYR_A0002', [5.48, 20.92, 65.6]]]
[['N_VAL_A0003', [6.19, 18.75, 65.89]]]
我尝试过,strip
,set
,uniq
但这不起作用;它给出了错误:
AttributeError:' list'对象没有属性' readline'
print set(uniqlist)
TypeError:不可用类型:' list'
任何帮助都将不胜感激。
答案 0 :(得分:2)
试试这个
the_list = [['N_ALA_A0001', [9.25, 24.41, 64.13]],
['O_ALA_A0001', [9.05, 21.51, 64.38]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['N_TYR_A0002', [7.84, 21.93, 66.24]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['O_TYR_A0002', [5.48, 20.92, 65.6]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['N_VAL_A0003', [6.19, 18.75, 65.89]],
['N_ALA_A0001', [9.25, 24.41, 64.13]]]
print map(eval, set(map(str, the_list)))
set(the_list)
无效,因为列表不可用。使用str
将其转换为可哈希类型的字符串。
答案 1 :(得分:1)
您可以将所有内部列表转换为元组,然后进行设置,然后将其转换回来:
a = [['N_ALA_A0001', [9.25, 24.41, 64.13]],['O_ALA_A0001', [9.05, 21.51, 64.38]],['N_ALA_A0001', [9.25, 24.41, 64.13]], ['N_TYR_A0002', [7.84, 21.93, 66.24]], ['N_ALA_A0001', [9.25, 24.41, 64.13]], ['O_TYR_A0002', [5.48, 20.92, 65.6]], ['N_ALA_A0001', [9.25, 24.41, 64.13]], ['N_VAL_A0003', [6.19, 18.75, 65.89]], ['N_ALA_A0001', [9.25, 24.41, 64.13]]]
s = set((k, tuple(v)) for k, v in a)
a = [k, list(v) for k, v in s]
如果您想保留订单,可以使用OrderedDict:
from collections import OrderedDict
s = OrderedDict({(k, tuple(v)):1 for k, v in a})
a = [[k, list(v)] for k, v in s.keys()]
请注意,我的解决方案会检查列表的整个元素是否具有唯一性。如果您只想检查第一个元素(N_ALA_A0001, ...
),可以使用OrderedDict,如
s = OrderedDict({k: v for k, v in a})
a = [[k, v] for k, v in s.iteritems()]
答案 2 :(得分:0)
首先,删除一行嵌套:
x = [['N_ALA_A0001', [9.25, 24.41, 64.13]],
['O_ALA_A0001', [9.05, 21.51, 64.38]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['N_TYR_A0002', [7.84, 21.93, 66.24]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['O_TYR_A0002', [5.48, 20.92, 65.6]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['N_VAL_A0003', [6.19, 18.75, 65.89]],
['N_ALA_A0001', [9.25, 24.41, 64.13]]]
现在,简单的字典理解将为您提供所需的信息:
d = {z:w for z, w in x}
给出了:
{'O_ALA_A0001': [9.05, 21.51, 64.38],
'N_ALA_A0001': [9.25, 24.41, 64.13],
'N_VAL_A0003': [6.19, 18.75, 65.89],
'N_TYR_A0002': [7.84, 21.93, 66.24],
'O_TYR_A0002': [5.48, 20.92, 65.6]}
如果需要,可以很容易地将其转换回列表。
正如@ m01指出的那样,如果你想保持这些顺序,可以使用OrderedDict
(以及一个小的语法更改)轻松完成:
from collections import OrderedDict
d = OrderedDict(((z, w) for z, w in x))
答案 3 :(得分:0)
如果您希望它基于第一个值是唯一的,只需将其转换为字典并返回。
the_list = [['N_ALA_A0001', [9.25, 24.41, 64.13]],
['O_ALA_A0001', [9.05, 21.51, 64.38]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['N_TYR_A0002', [7.84, 21.93, 66.24]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['O_TYR_A0002', [5.48, 20.92, 65.6]],
['N_ALA_A0001', [9.25, 24.41, 64.13]],
['N_VAL_A0003', [6.19, 18.75, 65.89]],
['N_ALA_A0001', [9.25, 24.41, 64.13]]]
unique_list = dict(the_list).items()