|
|
|
|
|
def find_unique_common_from_lists(input_list1, input_list2, only_com=False): |
|
|
''' |
|
|
find common items from 2 lists, the returned elements are unique. repetitive items will be ignored |
|
|
if the common items in two elements are not in the same order, the outputs follows the order in the first list |
|
|
|
|
|
parameters: |
|
|
input_list1, input_list2: two input lists |
|
|
only_com: True if only need the common list, i.e., the first output, saving computational time |
|
|
|
|
|
outputs: |
|
|
list_common: a list of elements existing both in list_src1 and list_src2 |
|
|
index_list1: a list of index that list 1 has common items |
|
|
index_list2: a list of index that list 2 has common items |
|
|
''' |
|
|
|
|
|
common_list = list(set(input_list1).intersection(input_list2)) |
|
|
|
|
|
if only_com: return common_list |
|
|
|
|
|
|
|
|
index_list1 = [] |
|
|
for index in range(len(input_list1)): |
|
|
item = input_list1[index] |
|
|
if item in common_list: |
|
|
index_list1.append(index) |
|
|
|
|
|
index_list2 = [] |
|
|
for index in range(len(input_list2)): |
|
|
item = input_list2[index] |
|
|
if item in common_list: |
|
|
index_list2.append(index) |
|
|
|
|
|
return common_list, index_list1, index_list2 |
|
|
|