Python|从给定的元组列表中删除具有重复第一个值的元组
给定一个元组列表,任务是从给定的元组列表中删除所有具有重复第一个值的元组。
例子:
Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'),
(12.121, 'Tuple3'), (923232.2323, 'Tuple4')]
Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]
Input: [('Tuple1', 121), ('Tuple2', 125),
('Tuple1', 135), ('Tuple4', 478)]
Output: [('Tuple1', 121), ('Tuple2', 125), ('Tuple4', 478)]
以下是实现上述任务的一些方法。
方法#1:使用迭代
# Python code to remove tuples having
# duplicate first value from given
# list of tuples
# Input list initialization
Input = [(12.121, 'Geeksforgeeks is best'),
(19212.22, 'India is best'),
(12.121, 'Cyware is best.'),
(923232.2323, 'Jiit is best')]
# using set
visited = set()
# Output list initialization
Output = []
# Iteration
for a, b in Input:
if not a in visited:
visited.add(a)
Output.append((a, b))
# Printing
print("Initial list of tuple is \n", Input)
print("List of tuple after removing duplicates:\n ", Output)
Initial list of tuple is
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (12.121, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)]
List of tuple after removing duplicates:
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (923232.2323, ‘Jiit is best’)]
方法 #2:使用列表推导
# Python code to remove tuples having
# duplicate first value from given
# list of tuples
# Input list initialization
Input = [(12.121, 'Geeksforgeeks is best'),
(19212.22, 'India is best'),
(19212.22, 'Cyware is best.'),
(923232.2323, 'Jiit is best')]
# Using set
seen = set()
# using list comprehension
Output = [(a, b) for a, b in Input
if not (a in seen or seen.add(a))]
# Printing
print("Initial list of tuple is" \n, Input)
print("\nList of tuple after removing duplicates \n", Output)
Initial list of tuple is
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (19212.22, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)]
List of tuple after removing duplicates [(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (923232.2323, ‘Jiit is best’)]
方法 #3:使用 itertools
# Python code to remove tuples having
# duplicate first value from given
# list of tuples
import itertools
# Input list initialization
Input = [(12.121, 'Geeksforgeeks is best'),
(19212.22, 'India is best'),
(923232.2323, 'Cyware is best.'),
(923232.2323, 'Jiit is best')]
# Using groupby
Output = ([next(b) for a, b in itertools.groupby(
Input, lambda y: y[0])])
# Printing
print("Initial list of tuple is\n", Input)
print("\nList of tuple after removing duplicates\n", Output)
Initial list of tuple is
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (923232.2323, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)]
List of tuple after removing duplicates
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (923232.2323, ‘Cyware is best.’)]
方法#4:使用 OrderedDict
这是删除重复项最优雅的方法是使用 OrderedDict。
# Python code to remove tuples having
# duplicate first value from given
# list of tuples
from collections import OrderedDict
# Input list initialization
Input = [(12.121, 'Geeksforgeeks is best'),
(19212.22, 'India is best'),
(19212.22, 'Cyware is best.'),
(923232.2323, 'Jiit is best')]
# Using orderedDIct
Output = OrderedDict(Input).items()
# Printing
print("Initial list of tuple is\n", Input)
print("\nList of tuple after removing duplicates\n", Output)
Initial list of tuple is
[(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘India is best’), (19212.22, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)]
List of tuple after removing duplicates
odict_items([(12.121, ‘Geeksforgeeks is best’), (19212.22, ‘Cyware is best.’), (923232.2323, ‘Jiit is best’)])