我有一个元组和字符串值的列表。如下所示
lst = [
'ACER', (['1000.0'], None),
'ACER', (['1000.0'], None),
'HP', (['1000.0'], None),
'ACER', (['1001.0'], None),
'ACER', (['1001.0'], None),
'ACER', (['1001.0'], None),
'ACER', (['1001.0'], None),
'HP', (['1001.0'], None)
]
必须将其分隔为字符串列表和元组列表。并删除字符串和元组的重复。
我需要结果如:
Str_lst = ['ACER', 'HP']
tup_lst = [(['1000.0'], None), (['1001.0'], None)]
注意:我在列表中有超过80000个值,所以请给我最好的性能解决方案
答案 0 :(得分:0)
简单检查元素是instance of tuple
。如果它是tuple附加到tupl_list
lst = [
'ACER', (['1000.0'], None),
'ACER', (['1000.0'], None),
'HP', (['1000.0'], None),
'ACER', (['1001.0'], None),
'ACER', (['1001.0'], None),
'ACER', (['1001.0'], None),
'ACER', (['1001.0'], None),
'HP', (['1001.0'], None)
]
tupl_list=[]
normalist=[]
for i in lst:
if isinstance(i,tuple):
tupl_list.append(i)
else:normalist.append(i)
答案 1 :(得分:0)
你可以试试这个。
>>> tup = [lst[x+1] for x in range(0, len(lst), 2)]
>>> names = [lst[x] for x in range(0, len(lst), 2)]
>>> b = []
>>> [b.append(x) for x in tup if x not in b]
>>> b
[(['1000.0'], None), (['1001.0'], None)]
>>> list(set(names))
['ACER', 'HP']
这可能是最佳解决方案,但它有助于解决您的问题