我有一个长数组,例如:
array1= [
[300, "Document1"],
[301, "Document2"],
[302, "Document3"],
[303, "Document4"],
[304, "Document5"],
[305, "Document6"],
[306, "Document7"],
[307, "Document8"],
[308, "Document9"],]
然后我得到一个较短的数组,例如:
array2 = [301, 305, 306]
我需要的是“匹配”数组2中的数字,并提供给我的数组输出:
outputarray = [Document2, Document6, Document7]
答案 0 :(得分:2)
output = [j for i, j in array1 if i in array2]
这应该有效。
答案 1 :(得分:0)
您似乎希望将数据保存在字典中,因为它是数字到文档名称的映射,并且dict
用于此类映射:
data_in_a_dict = dict(array1)
# {300: 'Document1', 301: 'Document2', 302: 'Document3', 303: 'Document4',
# 304: 'Document5', 305: 'Document6', 306: 'Document7', 307: 'Document8',
# 308: 'Document9'}
然后,从中提取数据变得很容易:
outputarray = [data_in_a_dict[key] for key in array2]
# ['Document2', 'Document6', 'Document7']