我正在尝试为以下代码片段编写相应的列表解析。
# Initialize data.
queryRelDict = {'1': [1, 2, 3],
'2': [4, 5, 6],
'3': [11, 13, 14]}
related_docs_indices = [1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14]
relOrNot = [0] * k
for item in queryRelDict.keys():
for i in range(len(related_docs_indices)):
if related_docs_indices[i] + 1 in queryRelDict[item]:
relOrNot[i] = 1
基本上我有一个字典,每个键都有一个列表作为其值。现在我的列表relOrNot[i]
必须为1,如果ith
的{{1}}元素位于字典中的任一列表中。
所需的输出是:
related_docs_indices
我尝试了以下两种变体,但无法获得所需的输出。
[1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
但输出是
relOrNot2 = [1 for item in queryRelDict.keys() for i in range(len(related_docs_indices)) if related_docs_indices[i] + 1 in queryRelDict[item]]
我也试过
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
对应输出:
relOrNot2 = [1 if related_docs_indices[i] + 1 in queryRelDict[item] else 0 for item in queryRelDict.keys() for i in range(len(related_docs_indices))]
获得所需输出需要进行哪些修改?
答案 0 :(得分:1)
如果您所需的输出是列表relOrNot
,其中relOrNot[i]
为1,则related_docs_indices
的第i个元素位于字典queryRelDict
中的任一列表中(它必须与related_docs_indices
)具有相同的长度,然后您可以执行以下操作:
# first create one flat list with all elements of the sublists in the dictionary
flatlist = [i for sublist in queryRelDict.itervalues() for i in sublist]
relOrNot = [1 if i in flatlist else 0 for i in related_docs_indices]
# [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
答案 1 :(得分:1)
对于每个键,您将遍历related_doc_indices
并检查该键的值集合中是否存在匹配值。对于键' 1',它看起来像这样:
key 1 values = [1, 2, 3]
related_docs_indices = [
1, # 1 (match)
2, # 1 (match)
3, # 1 (match)
4, # 0 (no match)
5, # 0 (no match)
6, # 0 (no match)
7, # 0 (no match)
8, # 0 (no match)
12, # 0 (no match)
13, # 0 (no match)
14] # 0 (no match)
此键的所需输出应为:
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
您遇到的一个问题是键在字典中是无序的,因此较长列表的结果可能会根据键的随机顺序而有所不同。例如:
>>> queryRelDict.keys()
['1', '3', '2']
我们先说你先对键进行排序,然后我相信所需的输出应该是这样的:
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, # key '1'
0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, # key '2'
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] # key '3'
keys = queryRelDict.keys()
keys.sort()
>>> [1 if i in queryRelDict.get(item) else 0
for item in keys for i in related_docs_indices]
#[1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14] related_doc_indices
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, # key '1' values: [1, 2, 3]
0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, # key '2' values: [4, 5, 6]
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] # key '3' values: [11, 13, 14] (note 11 is not in related_doc_indices)
答案 2 :(得分:1)
创建一个包含所有键和所有值的集合,并在循环中查看所需的值是否在集合中。
s = set()
for (k,v) in queryRelDict.items():
s.add(int(k))# because your keys are string
s = s | set(v)
map(lambda x:1 if x in s else 0, related_docs_indices)
=>[1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
答案 3 :(得分:1)
这里如果你想要一个单行:)
relOrNot = [1 if v in set().union(*queryRelDict.values()) else 0 for v in related_docs_indices]