-
白猪掌柜的
您可以将列表复制到一个新列表,而不是每次都遍历列表并用“/”分割input_tuples = [('jay', 'other'), ('blah', 'other stuff')]list_strings = ['blah', 'foo', 'bar', 'jay/day']# Using a set as @Patrick Haugh suggested for faster look upnew_strings = {x.split('/')[0] for x in list_strings}for tup in input_tuples: if tup[0] in new_strings: print('found', tup[0]) # outputs found jay, found blah
-
暮色呼如
选择传统的循环方式。这将元组中的名称与 lst 中的名称匹配:lst = ['blah', 'foo', 'bar', 'jay/day']tupl = ('unknown', 'bar', 'foo', 'jay', 'anonymous', 'ja', 'day')for x in tupl: for y in lst: if x == y.split('/')[0]: print(x, y)# bar bar# foo foo # jay jay/day
-
慕娘9325324
使用正则表达式:import rel = ['blah', 'foo', 'bar', 'jay/day']def match(name, l): for each in l: if re.match("^{}(\/|$)".format(name), each): return True # each if you want the string return False结果:match('ja', l) # Falsematch('jay', l) # Truematch('foo', l) # True使用元组:tupl = ('unknown', 'bar', 'foo', 'jay', 'anonymous', 'ja')res = [match(x, l) for x in tupl]资源:[False, True, True, True, False, False]