简体   繁体   中英

Finding partial-match to item in list-of-lists

I have list of lists like this:

l = [["08:00", "09:00", 60, False, 1.0],
     ["09:00", "10:00", 60, False, 0.3],
     ["12:00", "13:00", 60, False, 2.0],]

I want to check if list l have a element but I don't know value of last float . I only know ["12:00", "13:00", 60, False, ] .

if ["12:00", "13:00", 60, False, ???? ] in l:
    pass

Do you have an idea?

You could create a dummy class which compares equal to everything. Then you can use it as a kind of wildcard in your in condition.

class Dummy:
    def __eq__(self, other):
        return True

l = [["08:00", "09:00", 60, False, 1.0],
     ["09:00", "10:00", 60, False, 0.3],
     ["12:00", "13:00", 60, False, 2.0],]

if ["12:00", "13:00", 60, False, Dummy() ] in l:
    print "found"

I'd do it this way:

# The input.
l = [["08:00", "09:00", 60, False, 1.0],
     ["09:00", "10:00", 60, False, 0.3],
     ["12:00", "13:00", 60, False, 2.0],]

# The pattern you want to match.
pattern = ["12:00", "13:00", 60, False]

# Make a list of all comparisons.
result = [pattern == e[:4] for e in l]

# Test.
if any(result):
    pass

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM