Finding in elements in a tuple and filtering them

后端 未结 5 2047
借酒劲吻你
借酒劲吻你 2020-11-30 10:23

Assuming I have a tuple like:

[(\'text-1\',\'xxx\'), (\'img-1\',\'iii\'), (\'img-2\',\'jjj\'), (\'text-2\',\'xxx\')]

I want to filter out t

相关标签:
5条回答
  • 2020-11-30 10:52

    Just another way with generator expression (it could make some difference on large lists)

    >>> l = [('text-1','xxx'), ('img-1','iii'), ('img-2','jjj'), ('text-2','xxx')]
    >>> (x for x in l if x[0].find('img') == 0)
    0: <generator object <genexpr> at 0x917a00>
    >>> gen = (x for x in l if x[0].find('img') == 0)
    >>> list(gen)
    1: [('img-1', 'iii'), ('img-2', 'jjj')]
    
    0 讨论(0)
  • 2020-11-30 10:58

    One way:

    >>> l = [('text-1','xxx'), ('img-1','iii'), ('img-2','jjj'), ('text-2','xxx')]
    >>> [t for t in l if t[0].startswith('img')]
    [('img-1', 'iii'), ('img-2', 'jjj')]
    

    Another way:

    >>> filter(lambda x: x[0].startswith('img'), l)
    [('img-1', 'iii'), ('img-2', 'jjj')]
    

    The first is called a list comprehension. See F.C.'s answer for a related technique. The basic syntax is [{expression} for {item_var_or_vars} in {iterable} if {boolean_expression}]. It's semantically equivalent to something like this:

    new_list = []
    for {item_var_or_vars} in {iterable}:
        if {boolean_expression}:
            new_list.append({expression})
    

    The if {boolean_expression} bit is optional, just as it is in the for loop.

    The second is simply the built-in function filter, which accepts a test function and an iterable, and returns a list containing every element that "passes" the test function. lambda, if you haven't seen it before, is just a quick way of defining a function. You could do this instead:

    def keep_this_element(element):
        return element[0].startswith('img')   # returns True for ('img...', '...')
    
    new_list = filter(keep_this_element, l)   # keeps only elements that return True
    
    0 讨论(0)
  • 2020-11-30 11:00

    Something like this perhaps?

    l2 = [item for item in l1 if item[0].startswith('img')]
    
    0 讨论(0)
  • 2020-11-30 11:08
    tuple_filter = lambda t, i, w: filter(lambda a: a[i].startswith(w), t)
    newtuple = tuple_filter(thetuple, 0, 'img')
    
    0 讨论(0)
  • 2020-11-30 11:10

    With Normal Loop

    input = [('text-1','xxx'), ('img-1','iii'), ('img-2','jjj'), ('text-2','xxx')]
    inp = []
    for i in input:
        if 'img' in i[0]:
            inp.append(i)
    print(inp)
    

    or by Using lambda function

    inp = list(filter(lambda x: 'img' in x[0],input))
     print(inp)
    

    or by Using List Comprehension

     inp = [i for i in input if 'img' in i[0]]
     print(inp)
    
    0 讨论(0)
提交回复
热议问题