I have a list of lists like the below:
lst = [['A', 2001, ...], ['A', 2001, ...], ['B', 2001, ...], ['A', 2002, ...], ['C', 2002, ...], ...]
I would like to iterate over the combinations of a zipped list between the first two elements of each inner list, excluding duplicates.
I could do the below, but it doesn't exclude duplicates.
letters = [item[0] for item in lst]
years = [item[1] for item in lst]
for letter, year in zipped(letters, years):
In this example, the pairs that I am looking to iterate over would be:
'A': 2001
'B': 2001
'A': 2002
'C': 2002
(Note the exclusion of the additional 'A':2001
pair)
I feel that my attempted code is more complicated than it needs to be. ANy ideas are much appreciated.
Use a set
to remove the duplicates:
for letter, year in set(zipped(letters, years)):
...
Use groupby
and ignore the subiterator.
from itertools import groupby
def f(x):
return x[:2]
for (letter, number), _ in groupby(sorted(lst, key=f), f):
print(letter, number)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.