如果元组的第一个值与另一个元组重复,则需要从元组列表中删除该元组。例如,这是一个元组列表
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('genera', 23, 'https://en.wikipedia.org/wiki/Genus'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland_Airport'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland_Region'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland_Region'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('Islands',28,'https://en.wikipedia.org dddfdfdf/wiki/'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island'),
输出必须是这样的:
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('genera', 23, 'https://en.wikipedia.org/wiki/Genus'),
('Islands',28,'https://en.wikipedia.org dddfdfdf/wiki/'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island')
我正在尝试一切在网络上进行搜索
答案 0 :(得分:1)
您好,欢迎来到Stack Overflow。您的问题提到了元组的列表。因此,我假设您的列表如下所示:
all =[('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('genera', 23, 'https://en.wikipedia.org/wiki/Genus'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland_Airport'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland_Region'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland_Region'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('Islands',28,'https://en.wikipedia.org dddfdfdf/wiki/'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island')]
在这种情况下,基于第一个值获得唯一结果的最简单方法是将元组列表更改为一组(始终唯一,并在以下for循环中保存许多不必要的迭代),然后循环执行此操作方式:
unique_all = list(set(all))
first_values = []
for t in unique_all:
if t[0] in first_values:
unique_all.remove(t)
else:
first_values.append(t[0])
print(unique_all)
输出将是:
[('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland_Airport'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island'),
('genera', 23, 'https://en.wikipedia.org/wiki/Genus'),
('Islands', 28, 'https://en.wikipedia.org dddfdfdf/wiki/')]
我想这就是你的问题。
答案 1 :(得分:0)
只要遇到唯一元组[0],您就可以遍历列表并将元组添加到新列表中。
l = [your list from above]
uniques = set()
new_l = []
for tup in l:
if tup[0] not in uniques:
new_l.append(tup)
uniques.add(tup[0])
print(new_l)
输出:
[('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('genera', 23, 'https://en.wikipedia.org/wiki/Genus'),
('Islands', 28, 'https://en.wikipedia.org dddfdfdf/wiki/'),
('South Island', 29, 'https://en.wikipedia.org/wiki/South_Island')]
如果您真的想删除,列表可能不是保存它的最佳数据结构,因为从列表中间删除是O(n)操作,而不是从结尾删除或追加是O (1)。
答案 2 :(得分:0)
将所有这些都放在列表中。
l = [('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland'),
('genera', 23, 'https://en.wikipedia.org/wiki/Genus'),
('Auckland', 23, 'https://en.wikipedia.org/wiki/Auckland_Airport')]
...
现在输入:
k = []
m = []
for i in l :
if i[0] in k :
pass
else :
k.append([i[0]])
m.append(i)
print(m)