Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

python - Filter a list of lists of tuples

I have a list of lists of tuples:

oldList = [[(1,None),(2,45),(3,67)],[(1,None), (2,None), (3,None),(4,56),(5,78)],[(1, None),(2, 98)]]

I would like to filter any instance of "None":

newList = [[(2,45),(3,67)], [(4,56),(5,78)], [(2, 98)]]

The closest I've come is with this loop, but it does not drop the entire tuple (only the 'None') and it also destroys the list of lists of tuples structure:

newList = []
for data in oldList:
    for point in data:
        newList.append(filter(None,point))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The shortest way to do this is with a nested list comprehension:

>>> newList = [[t for t in l if None not in t] for l in oldList]
>>> newList
[[(2, 45), (3, 67)], [(4, 56), (5, 78)], [(2, 98)]]

You need to nest two list comprehensions because you are dealing with a list of lists. The outer part of the list comprehension [[...] for l in oldList] takes care of iterating through the outer list for each inner list contained. Then inside the inner list comprehension you have [t for t in l if None not in t], which is a pretty straightforward way of saying you want each tuple in the list which does not contain a None.

(Arguably, you should choose better names than l and t, but that would depend on your problem domain. I've chosen single-letter names to better highlight the structure of the code.)

If you are unfamiliar or uncomfortable with list comprehensions, this is logically equivalent to the following:

>>> newList = []
>>> for l in oldList:
...     temp = []
...     for t in l:
...         if None not in t:
...             temp.append(t)
...     newList.append(temp)
...
>>> newList
[[(2, 45), (3, 67)], [(4, 56), (5, 78)], [(2, 98)]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...