Filter a list of tuples based on condition

前端 未结 3 2057
失恋的感觉
失恋的感觉 2020-12-04 02:27

I have a list like this:

A=[(1,\'A\'),(2,\'H\'),(3,\'K\'),(4,\'J\')]

Each member of this list is like this: (number, string)

Now i

3条回答
  •  臣服心动
    2020-12-04 03:01

    Use a list comprehension:

    [y for x,y in A if x>2]
    

    Demo:

    >>> A=[(1,'A'),(2,'H'),(3,'K'),(4,'J')]
    >>> [y for x,y in A if x>2]
    ['K', 'J']
    >>> 
    

提交回复
热议问题