0
- 列表的兩個列表h & i。
- h [0]的長度等於h [1]並且等於h [2]。同樣適用於我[0],我[1]和我[2]。
- h [0]的長度可能不等於i [0]。
- 當h [0] [x] = i [0] [y]時,「比較」h [2] [x] & i [2] [y]。
- 功能 「比較」 指的是下述:
- 分割了x & Y考慮列表遇到 '\ n' 與x.split( '\ n')和y.split時( '\ n') 。
- 對於x.split('\ n')和y.split('\ n')的每個元素,使用list_difference刪除重複項。
- 將結果存儲到新的列表結果中。
非工作如下當不同元素匹配時刪除列表中的重複元素 - Python
def list_difference(list1, list2):
"""Uses list1 as the reference, returns list of items not in list2."""
diff_list = []
for item in list1:
if not item in list2:
diff_list.append(item)
return diff_list
h=[['match','meh'],['0','1'],['remove\n0\n12','1']]
i=[['match','ignore0','ignore1'],['0','2','3'],['1\nremove','2','3']]
result = result(h,i)
# result should be:
# result = [['match','meh'],['0','1'],['0','1']]
# code not working:
results = []
for l in h:
for p in i:
if l[0] == p[0]:
results.append(list_difference(l[2].split('\n'), p[2].split('\n')))
# Traceback (most recent call last):
# File "<pyshell#19>", line 4, in <module>
# results.append(list_difference(l[2].split('\n'), p[2].split('\n')))
# IndexError: list index out of range
代碼越來越近:
for l0, l2 in h[0], h[2]:
for p0, p2 in i[0], i[2]:
if l0 == p0:
results.append(list_difference(l2.split('\n'), p2.split('\n')))
print results
# [['h0_1']]