performance - Compare two lists in python and print the output -
hi have list of lists , need compare value of each list 1 extracted xml file. structure similar this:
[('example', '123', 'foo', 'bar'), ('example2', '456', 'foo', 'bar'), ...] i need compare second value of each list values in xml:
for item in main_list: child in xml_data: if item[4] == child.get('value'): print item[4] the problem main_list has huge ammount of lines (1000+) , multiplied values xml (100+) results in lot of iterations becoming method unefficient.
is there way efficiently?
regards.
a membership check on set faster manually iterating , checking:
children = {child.get('value') child in xml_data} item in main_list: if item[4] in children: print(item[4]) here construct set simple set comprehension.
note may worth swapping data in set - if main_list longer, more efficient make set of data.
items = {item[4] item in main_list} child in xml_data: value = child.get('value') if value in items: print(value) these both processing on data once, rather each time check made.
note set not handle duplicate values or order on set side - if important, isn't valid solution. version use order/duplicates data iterating over. if isn't valid, can still process data beforehand, , use itertools.product() iterate little quicker.
items = [item[4] item in main_list] children = [child.get('value') child in xml_data] item, child in itertools.product(items, children): if item == child: print(item) as karl knechtel points out, if don't care order duplicates at all, can set intersection:
for item in ({child.get('value') child in xml_data} & {item[4] item in main_list}): print(item)
Comments
Post a Comment