我有一个字典(包含列表)和一个列表,我想比较:
首先,我想知道[1, 1, 0, 0, 0, 1, 1, 1]
ref 中的每个值列表(例如对于 issue1,值列表是 )是否与 list 具有相同的长度abf
。
然后是棘手的部分:如果它们的长度相同,我想将列表abf
中的每个项目与ref
.
但是......在一种情况下,程序将继续前进到下一个值列表ref
(不检查当前值列表的剩余项目),这是如果值列表的项目是 1 并且对应的列表中的项目abf
为 0。
为了清楚起见,这里有一个例子:
dict ref 中键 'issue1' 的值列表是[1, 1, 0, 0, 0, 1, 1, 1].
The list abf
is [1, 1, 0, 1, 0, 1, 0, 0]
。
现在,我想检查这两个列表中的每一项(issue1 的值列表的第一项与 list 的第一项abf
,然后是第二项issue1
的第二项,abf
以此类推……):前两个项目是 1 和 1 并且条件(见上文)不满足,它应该继续接下来的两个项目(再次是 1 和 1)等等,直到它到达(在这种情况下)第七项(即 1 和 0)。此时它将停止将 value-list ofissue1
与 listabf
进行比较,并继续将下一个 value-list (of issue2
) 与 list进行比较abf
。我希望你能明白!
到目前为止,这是我的代码:
## ref is a dict with lists as values, abf is a list
ref = {'issue1': [1, 1, 0, 0, 0, 1, 1, 1],
'issue2': [1, 0, 0, 1, 0, 0, 0, 0],
'issue3': [0, 1, 0, 0, 1, 0, 0, 1]}
abf = [1, 1, 0, 1, 0, 1, 0, 0]
## getting the length of the lists in ref and abf ans save them in ref_total & abf_total
for key in ref:
[int(item) for item in ref[key]]
ref_total = len(ref[key])
abf_total = len(abf)
## check whether ref_total and abf_total has same value
if ref_total == abf_total:
for key, value in ref.items():
for j in value:
if (ref[key][j] == 1) and (abf[j] == 0): ## if item in ref is 1 and in abf is 0, go on to the next value-list
break
if j == abf_total-1: ## if he compared the whole value-list of the current key of ref with abf and the condition above did not occur, save the key of this value-list in resp!
resp = ref[key]
else:
resp = 'Length of strings varies!' ##if the lists don't have the same length
print resp ##let me know, which key "went through"
我真的很期待你的回应。代码不起作用,我不知道为什么!