我想从列表列表中删除所有重复值。
所以我有一个像这样的列表列表。
a=[['102 min', '', 'Comedy', 'User Rating: 6.6/10 (4,072 user ratings)', '69', 'Metascore', '', 'Rank:', '10', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '', '1:35 pm', '', '4:10', '', '4:55', '', '7:40', '', '9:55', '', '10:35'], ['110 min', '', 'Comedy', '', 'Drama', '', 'Romance', 'User Rating: 8.1/10 (11,478 user ratings)', '73', 'Metascore', '', 'Rank:', '18', 'Showtimes:', 'Studio Movie Grill - Downey', '11:30 am', '', '2:10 pm', '', '4:45', '', '7:25', '', '10:00'], ['111 min', '', 'Action', '', 'Adventure', '', 'SciFi', 'User Rating: 6/10 (23,905 user ratings)', '44', 'Metascore', '', 'Rank:', '7', 'Showtimes:', 'Studio Movie Grill - Downey', '11:05 am', '', '1:50 pm', '', '4:35', '', '7:20', '', '10:05'], ['118 min', '', 'Action', '', 'Adventure', '', 'Drama', '', 'Fantasy', '', 'Thriller', 'User Rating: 6.8/10 (45,126 user ratings)', '48', 'Metascore', '', 'Rank:', '8', 'Showtimes:', 'Studio Movie Grill - Downey', '11:10 am', '', '1:55 pm', '', '4:40', '', '7:35', '', '10:20'], ['120 min', '', 'Thriller', 'User Rating: 4.9/10 (1,002 user ratings)', '32', 'Metascore', '', 'Rank:', '16', 'Showtimes:', 'Studio Movie Grill - Downey', '11:20 am', '', '2:05 pm', '', '4:50', '', '7:45', '', '10:40'], ['134 min', '', 'Action', '', 'Adventure', '', 'SciFi', 'User Rating: 7.8/10 (223,161 user ratings)', '88', 'Metascore', '', 'Rank:', '4', 'Showtimes:', 'Studio Movie Grill - Downey', '12:00 pm', '', '4:05', '', '7:15', '', '10:15'], ['140 min', '', 'Action', '', 'Adventure', '', 'SciFi', 'User Rating: 7.9/10 (76,138 user ratings)', '64', 'Metascore', '', 'Rank:', '1', 'Showtimes:', 'Studio Movie Grill - Downey', '11:45 am', '', '4:00 pm', '', '7:10', '', '10:10'], ['86 min', '', 'Animation', '', 'Adventure', '', 'Comedy', '', 'Family', '', 'Fantasy', '', 'Mystery', '', 'Romance', 'User Rating: 4.7/10 (1,275 user ratings)', '36', 'Metascore', '', 'Rank:', '75', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '', '1:15 pm', '', '3:30', '', '5:45', '', '7:55'], ['90 min', '', 'Drama', '', 'Horror', '', 'Thriller', 'User Rating: 8.2/10 (28,256 user ratings)', '82', 'Metascore', '', 'Rank:', '2', 'Showtimes:', 'Studio Movie Grill - Downey', '11:15 am', '', '12:05 pm', '', '1:40', '', '2:30', '', '4:15', '', '6:40', '', '7:30', '', '9:05', '', '10:15']]
我希望:
unique = [['102 min', 'Comedy', 'User Rating: 6.6/10 (4,072 user ratings)', '69', 'Metascore', 'Rank:', '10', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '1:35 pm', '4:10', '4:55', '7:40', '9:55', '10:35'], ['110 min', 'Comedy', 'Drama', 'Romance', 'User Rating: 8.1/10 (11,478 user ratings)', '73', 'Metascore', 'Rank:', '18', 'Showtimes:', 'Studio Movie Grill - Downey', '11:30 am', '2:10 pm', '4:45', '7:25', '10:00'], ['111 min', 'Action', 'Adventure', 'SciFi', 'User Rating: 6/10 (23,905 user ratings)', '44', 'Metascore', 'Rank:', '7', 'Showtimes:', 'Studio Movie Grill - Downey', '11:05 am', '1:50 pm', '4:35', '7:20', '10:05'], ['118 min', 'Action', 'Adventure', 'Drama', 'Fantasy', 'Thriller', 'User Rating: 6.8/10 (45,126 user ratings)', '48', 'Metascore', 'Rank:', '8', 'Showtimes:', 'Studio Movie Grill - Downey', '11:10 am', '1:55 pm', '4:40', '7:35', '10:20'], ['120 min', 'Thriller', 'User Rating: 4.9/10 (1,002 user ratings)', '32', 'Metascore', 'Rank:', '16', 'Showtimes:', 'Studio Movie Grill - Downey', '11:20 am', '2:05 pm', '4:50', '7:45', '10:40'], ['134 min', 'Action', 'Adventure', 'SciFi', 'User Rating: 7.8/10 (223,161 user ratings)', '88', 'Metascore', 'Rank:', '4', 'Showtimes:', 'Studio Movie Grill - Downey', '12:00 pm', '4:05', '7:15', '10:15'], ['140 min', 'Action', 'Adventure', 'SciFi', 'User Rating: 7.9/10 (76,138 user ratings)', '64', 'Metascore', 'Rank:', '1', 'Showtimes:', 'Studio Movie Grill - Downey', '11:45 am', '4:00 pm', '7:10', '10:10'], ['86 min', 'Animation', 'Adventure', 'Comedy', 'Family', 'Fantasy', 'Mystery', 'Romance', 'User Rating: 4.7/10 (1,275 user ratings)', '36', 'Metascore', 'Rank:', '75', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '1:15 pm', '3:30', '5:45', '7:55'], ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,256 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'Studio Movie Grill - Downey', '11:15 am', '12:05 pm', '1:40', '2:30', '4:15', '6:40', '7:30', '9:05', '10:15']]
我不知道该怎么做。
我试过了
unique = []
[unique.append(item) for item in a if item not in unique]
谢谢
答案 0 :(得分:1)
从列表中删除重复项(仅保持唯一)很容易 - 您只需计算元素数量,然后仅保留仅出现一次的元素。您可以使用临时集来跟踪已计数的元素以稍微优化它,所以:
test_list = ['102 min', '', 'Comedy', 'User Rating: 6.6/10 (4,072 user ratings)', '69',
'Metascore', '', 'Rank:', '10', 'Showtimes:', 'Studio Movie Grill - Downey',
'11:00 am', '', '1:35 pm', '', '4:10', '', '4:55', '', '7:40', '', '9:55', '',
'10:35']
seen = set() # a temp set for a quick duplicates lookup
unique_list = [e for e in test_list
if e not in seen and not seen.add(e) and test_list.count(e) == 1]
# ['102 min', 'Comedy', 'User Rating: 6.6/10 (4,072 user ratings)', '69', 'Metascore',
# 'Rank:', '10', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '1:35 pm', '4:10',
# '4:55', '7:40', '9:55', '10:35']
由于你有一个列表列表,程序是相同的,你只需要将它应用于每个元素。所以只需将其迁移到一个函数中:
def get_unique(source):
seen = set() # a temp set for a quick duplicates lookup
return [e for e in source
if e not in seen and not seen.add(e) and source.count(e) == 1]
然后你可以遍历你的a
列表来获得唯一身份:
unique = [get_unique(e) for e in a]
如果您只想删除重复项(但至少保留一份),只需删除source.count()
项检查。
请记住,对于我们遇到的每个新元素,我们计算(基本上遍历整个列表),这会在非常长的列表中变慢。相反,您可以创建一个计数器dict
,然后进行就地计数查找:
import collections
def get_unique(source):
counter = collections.defaultdict(int) # our counter dict
for e in source:
counter[e] += 1
return [e for e in source if counter[e] == 1]
额外的Python内迭代将为更长的列表快速回报。
答案 1 :(得分:-1)
以下是您要找的内容。
您实际上想要从列表中删除所有空条目:
unique =[]
tempList = []
a=[['102 min', '', 'Comedy', 'User Rating: 6.6/10 (4,072 user ratings)', '69', 'Metascore', '', 'Rank:', '10', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '', '1:35 pm', '', '4:10', '', '4:55', '', '7:40', '', '9:55', '', '10:35'], ['110 min', '', 'Comedy', '', 'Drama', '', 'Romance', 'User Rating: 8.1/10 (11,478 user ratings)', '73', 'Metascore', '', 'Rank:', '18', 'Showtimes:', 'Studio Movie Grill - Downey', '11:30 am', '', '2:10 pm', '', '4:45', '', '7:25', '', '10:00'], ['111 min', '', 'Action', '', 'Adventure', '', 'SciFi', 'User Rating: 6/10 (23,905 user ratings)', '44', 'Metascore', '', 'Rank:', '7', 'Showtimes:', 'Studio Movie Grill - Downey', '11:05 am', '', '1:50 pm', '', '4:35', '', '7:20', '', '10:05'], ['118 min', '', 'Action', '', 'Adventure', '', 'Drama', '', 'Fantasy', '', 'Thriller', 'User Rating: 6.8/10 (45,126 user ratings)', '48', 'Metascore', '', 'Rank:', '8', 'Showtimes:', 'Studio Movie Grill - Downey', '11:10 am', '', '1:55 pm', '', '4:40', '', '7:35', '', '10:20'], ['120 min', '', 'Thriller', 'User Rating: 4.9/10 (1,002 user ratings)', '32', 'Metascore', '', 'Rank:', '16', 'Showtimes:', 'Studio Movie Grill - Downey', '11:20 am', '', '2:05 pm', '', '4:50', '', '7:45', '', '10:40'], ['134 min', '', 'Action', '', 'Adventure', '', 'SciFi', 'User Rating: 7.8/10 (223,161 user ratings)', '88', 'Metascore', '', 'Rank:', '4', 'Showtimes:', 'Studio Movie Grill - Downey', '12:00 pm', '', '4:05', '', '7:15', '', '10:15'], ['140 min', '', 'Action', '', 'Adventure', '', 'SciFi', 'User Rating: 7.9/10 (76,138 user ratings)', '64', 'Metascore', '', 'Rank:', '1', 'Showtimes:', 'Studio Movie Grill - Downey', '11:45 am', '', '4:00 pm', '', '7:10', '', '10:10'], ['86 min', '', 'Animation', '', 'Adventure', '', 'Comedy', '', 'Family', '', 'Fantasy', '', 'Mystery', '', 'Romance', 'User Rating: 4.7/10 (1,275 user ratings)', '36', 'Metascore', '', 'Rank:', '75', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '', '1:15 pm', '', '3:30', '', '5:45', '', '7:55'], ['90 min', '', 'Drama', '', 'Horror', '', 'Thriller', 'User Rating: 8.2/10 (28,256 user ratings)', '82', 'Metascore', '', 'Rank:', '2', 'Showtimes:', 'Studio Movie Grill - Downey', '11:15 am', '', '12:05 pm', '', '1:40', '', '2:30', '', '4:15', '', '6:40', '', '7:30', '', '9:05', '', '10:15']]
for mylist in a :
print(list)
for elems in mylist:
if elems !='':
tempList.append(elems)
unique.append(tempList)
tempList = []
print(unique)
期望的输出:
[['102 min', 'Comedy', 'User Rating: 6.6/10 (4,072 user ratings)', '69', 'Metascore', 'Rank:', '10', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '1:35 pm', '4:10', '4:55', '7:40', '9:55', '10:35'], ['110 min', 'Comedy', 'Drama', 'Romance', 'User Rating: 8.1/10 (11,478 user ratings)', '73', 'Metascore', 'Rank:', '18', 'Showtimes:', 'Studio Movie Grill - Downey', '11:30 am', '2:10 pm', '4:45', '7:25', '10:00'], ['111 min', 'Action', 'Adventure', 'SciFi', 'User Rating: 6/10 (23,905 user ratings)', '44', 'Metascore', 'Rank:', '7', 'Showtimes:', 'Studio Movie Grill - Downey', '11:05 am', '1:50 pm', '4:35', '7:20', '10:05'], ['118 min', 'Action', 'Adventure', 'Drama', 'Fantasy', 'Thriller', 'User Rating: 6.8/10 (45,126 user ratings)', '48', 'Metascore', 'Rank:', '8', 'Showtimes:', 'Studio Movie Grill - Downey', '11:10 am', '1:55 pm', '4:40', '7:35', '10:20'], ['120 min', 'Thriller', 'User Rating: 4.9/10 (1,002 user ratings)', '32', 'Metascore', 'Rank:', '16', 'Showtimes:', 'Studio Movie Grill - Downey', '11:20 am', '2:05 pm', '4:50', '7:45', '10:40'], ['134 min', 'Action', 'Adventure', 'SciFi', 'User Rating: 7.8/10 (223,161 user ratings)', '88', 'Metascore', 'Rank:', '4', 'Showtimes:', 'Studio Movie Grill - Downey', '12:00 pm', '4:05', '7:15', '10:15'], ['140 min', 'Action', 'Adventure', 'SciFi', 'User Rating: 7.9/10 (76,138 user ratings)', '64', 'Metascore', 'Rank:', '1', 'Showtimes:', 'Studio Movie Grill - Downey', '11:45 am', '4:00 pm', '7:10', '10:10'], ['86 min', 'Animation', 'Adventure', 'Comedy', 'Family', 'Fantasy', 'Mystery', 'Romance', 'User Rating: 4.7/10 (1,275 user ratings)', '36', 'Metascore', 'Rank:', '75', 'Showtimes:', 'Studio Movie Grill - Downey', '11:00 am', '1:15 pm', '3:30', '5:45', '7:55'], ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,256 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'Studio Movie Grill - Downey', '11:15 am', '12:05 pm', '1:40', '2:30', '4:15', '6:40', '7:30', '9:05', '10:15']]