Vielleicht können Sie mit so etwas wie Generatoren tun (was effizienter ist) anstelle von Listen per se.
Gibt es einen bestimmten Grund, warum Sie es mit nur einer Liste machen müssen?
if any(True for tuple_list in list_of_tuple_lists if any(True for t in tuple_list if t[2]=='JJ') and any(True for t in tuple_list if t[3] == 'nsubj'))
Und Ihre Benennung entsprechen:
if any(True for tuple_list in parse_tree[i] if any(True for t in tuple_list if t[2]=='JJ') and any(True for t in tuple_list if t[3] == 'nsubj'))
Dieser Code für alle Fälle funktioniert. Einfacher Test:
import random
import time
none = [(1,2,3,4),(1,2,3,4),(1,2,3,4),(1,2,3,4)] # none of the conditions
first = [(1,2,3,4),(1,2,'JJ',4),(1,2,3,4),(1,2,3,4)]# only first of the conditions
second = [(1,2,3,4),(1,2,'test',4),(1,2,4,'nsubj'),(1,2,3,4)]# only second of the conditions
both = [(1,2,'JJ',4),(1,2,'test',4),(1,2,3,'nsubj'),(1,2,3,4)]# both of the conditions in different tuples
same = [(1,2,'JJ','nsubj'),(1,2,'test',4),(1,2,2,4),(1,2,3,4)]# both of the conditions in same tuple
possible_tuples=[none,first,second,both,same]
def our_check(list_of_tuple_lists):
if any(True for tuple_list in list_of_tuple_lists if any(True for t in tuple_list if t[2]=='JJ') and any(True for t in tuple_list if t[3] == 'nsubj')):
return True
else:
return False
def our_check_w_lists(list_of_tuple_lists):
if any([True for tuple_list in list_of_tuple_lists
if any([True for t in tuple_list if t[2]=='JJ']) and any([True for t in tuple_list if t[3] == 'nsubj'])]):
return True
else:
return False
def third_snippet(list_of_tuple_lists):
if any([tup for tup in list_of_tuple_lists if tup[2] == 'JJ']) and any([tup for tup in list_of_tuple_lists if tup[3] == 'nsubj']):
return True
else:
return False
def test(func):
test_cases = []
for n in range(100000):
test_list=[]
for i in range(10):
test_list.append(random.choice(possible_tuples))
expected = False
if both in test_list or same in test_list:
expected = True
test_case = expected, test_list
test_cases.append(test_case)
start = time.clock()
for expected, case_list in test_cases:
if expected != func(case_list):
print('%s, Fail for: %s'%(func,case_list))
return False
end = time.clock()
print('function:%s, %f'%(func, end-start))
test(our_check)
test(our_check_w_lists)
test(third_snippet)
Diese Testergebnisse zeigt Unterschied Ausführungszeit zwischen Generatoren und Liste Verständnis mit für nur 10-Tupel lange Liste.
function:<function our_check at 0x00000000028CE7B8>, 0.378369
function:<function our_check_w_lists at 0x00000000031472F0>, 1.270924
<function third_snippet at 0x00000000031E0840>, Fail for: [[...
Während es Ihr Problem nicht lösen wird, reduziert die Verwendung von Generatorausdrücken die Laufzeit.Lege die '[]' von deiner letzten Anweisung ab - 'any' stoppt, sobald ein Element' True' ist, aber die '[]' erzwingt, dass alle Elemente im Moment erzeugt werden. – MisterMiyagi
Ein Listenverstehen mit Status zu haben ist eine schlechte Idee. Der einfachste Weg, dies zu tun, ist eine einfache Schleife und Flags für jedes Ding, das Sie suchen. – jonrsharpe
@jonrsharpe Ist das nicht unpythonisch? –