Python: Filter in tuples

Pedro Pinto

I have two lists of tuples. I want a new list with every member of l2 and every member of l1 that does not begin with the same element from l2.

I used a for loop and my output is ok.

My question is: How can I use the filter function or a list comprehension?

def ov(l1, l2):

    l3=l1.copy()    
    for i in l2:

        for j in l1:

            if i[0]==j[0]:
                l3.pop(l3.index(j))

    print (l3+l2)            

ov([('c','d'),('c','e'),('a','b'),('a', 'd')], [('a','c'),('b','d')])

The output is:

[('c', 'd'), ('c', 'e'), ('a', 'c'), ('b', 'd')]
timgeb

If I understand correctly, this should be the straight forward solution:

>>> l1 = [('c','d'),('c','e'),('a','b'),('a', 'd')]                                                                               
>>> l2 = [('a','c'),('b','d')]                                                                                                    
>>>                                                                                                                               
>>> starters = set(x for x, _ in l2)                                                                                              
>>> [(x, y) for x, y in l1 if x not in starters] + l2                                                                             
[('c', 'd'), ('c', 'e'), ('a', 'c'), ('b', 'd')]

This can be generalized to work with longer tuples with extended iterable unpacking.

>>> starters = set(head for head, *_ in l2)                                                                                             
>>> [(head, *tail) for head, *tail in l1 if head not in starters] + l2                                                            
[('c', 'd'), ('c', 'e'), ('a', 'c'), ('b', 'd')]

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Using Tuples as Keys in Python

分類Dev

How to nest tuples in Python

分類Dev

Using tuples with .format in python

分類Dev

Filter through a list of 3(or greater)-tuples based on an element

分類Dev

Grab unique tuples in python list, irrespective of order

分類Dev

Creating tuples using for..in loops in python

分類Dev

How to group list items into sequential tuples in Python?

分類Dev

Python: Convert list of tuples into a list of objects?

分類Dev

Transforming Python list of tuples to R named vector

分類Dev

python print dictionary tuples in order grid format

分類Dev

In Python how to access elements of tuples using reduce()?

分類Dev

Python, to pick specific elements from a list of tuples

分類Dev

Fastest approach to finding the most common first and second value of tuples in an N-dimensional array of tuples in Python

分類Dev

Filter generated permutations in python

分類Dev

Python filter()関数

分類Dev

Python: filter python dataframe by time

分類Dev

Python loop through api and append multiple objects as tuples to list

分類Dev

How to remove duplicate combinations tuples from python list?

分類Dev

Performantly create a list of python dictionaries by combining a numpy array and a list of tuples

分類Dev

How to create a function (Iteration/Recursion) to run over a dictionary of tuples in Python?

分類Dev

Python -- rank tuples by first item, resolve ties by second item

分類Dev

How to check if all tuples have a specific value in a python list

分類Dev

Filter object error in Python 3

分類Dev

Python - dynamic filter for list comprehension?

分類Dev

Python, using lambda, map and filter

分類Dev

How to filter files in qlistview python

分類Dev

Firestore - Python filter by value in a list

分類Dev

How do I filter nested cases to be filter out python

分類Dev

Subtracting tuples

Related 関連記事

  1. 1

    Using Tuples as Keys in Python

  2. 2

    How to nest tuples in Python

  3. 3

    Using tuples with .format in python

  4. 4

    Filter through a list of 3(or greater)-tuples based on an element

  5. 5

    Grab unique tuples in python list, irrespective of order

  6. 6

    Creating tuples using for..in loops in python

  7. 7

    How to group list items into sequential tuples in Python?

  8. 8

    Python: Convert list of tuples into a list of objects?

  9. 9

    Transforming Python list of tuples to R named vector

  10. 10

    python print dictionary tuples in order grid format

  11. 11

    In Python how to access elements of tuples using reduce()?

  12. 12

    Python, to pick specific elements from a list of tuples

  13. 13

    Fastest approach to finding the most common first and second value of tuples in an N-dimensional array of tuples in Python

  14. 14

    Filter generated permutations in python

  15. 15

    Python filter()関数

  16. 16

    Python: filter python dataframe by time

  17. 17

    Python loop through api and append multiple objects as tuples to list

  18. 18

    How to remove duplicate combinations tuples from python list?

  19. 19

    Performantly create a list of python dictionaries by combining a numpy array and a list of tuples

  20. 20

    How to create a function (Iteration/Recursion) to run over a dictionary of tuples in Python?

  21. 21

    Python -- rank tuples by first item, resolve ties by second item

  22. 22

    How to check if all tuples have a specific value in a python list

  23. 23

    Filter object error in Python 3

  24. 24

    Python - dynamic filter for list comprehension?

  25. 25

    Python, using lambda, map and filter

  26. 26

    How to filter files in qlistview python

  27. 27

    Firestore - Python filter by value in a list

  28. 28

    How do I filter nested cases to be filter out python

  29. 29

    Subtracting tuples

ホットタグ

アーカイブ