Python – 字典列表中特定键的二进制操作
有时,在使用Python字典时,我们可能会遇到一个问题,即我们需要对整个列表中字典中的某些键执行某些操作并返回结果。这种问题在涉及 Web 开发等数据的领域中很常见。让我们讨论可以执行此任务的某些方式。
Input : test_list = [{‘best’: 8, ‘Gfg’: 13}, {‘is’: 5, ‘for’: 9, ‘best’: 15, ‘Gfg’: 7, ‘geeks’: 7}]
Output : [(0, 104), (1, 105)]
Input : test_list = [{‘Gfg’ : 3, ‘best’ : 10}]
Output : [(0, 30)]
方法#1:循环+ enumerate()
上述功能的组合可以用来解决这个问题。这是一种蛮力方法,我们循环遍历每个字典列表并执行操作并存储结果。
# Python3 code to demonstrate working of
# Binary operation on specific keys in Dictionary List
# Using loop + enumerate()
# initializing list
test_list = [{'Gfg' : 5, 'is' : 6, 'best' : 7, 'for' : 8, 'geeks' : 10},
{'Gfg' : 9, 'is' : 4, 'best' : 10, 'for' : 4, 'geeks' :7},
{'Gfg' : 2, 'is' : 10, 'best' : 8, 'for' : 9, 'geeks' : 3}]
# printing original list
print("The original list is : " + str(test_list))
# initializing keys
op1_key = 'Gfg'
op2_key = 'best'
# Binary operation on specific keys in Dictionary List
# Using loop + enumerate()
res = []
for idx, val in enumerate(test_list):
res.append((idx, val[op1_key] * val[op2_key]))
# printing result
print("The constructed result list : " + str(res))
The original list is : [{‘geeks’: 10, ‘for’: 8, ‘Gfg’: 5, ‘is’: 6, ‘best’: 7}, {‘geeks’: 7, ‘for’: 4, ‘Gfg’: 9, ‘is’: 4, ‘best’: 10}, {‘geeks’: 3, ‘for’: 9, ‘Gfg’: 2, ‘is’: 10, ‘best’: 8}]
The constructed result list : [(0, 35), (1, 90), (2, 16)]
方法 #2:使用列表理解 + enumerate()
上述功能的组合也可以用来解决这个问题。在此,我们以速记方式使用列表推导执行类似的任务。
# Python3 code to demonstrate working of
# Binary operation on specific keys in Dictionary List
# Using list comprehension + enumerate()
# initializing list
test_list = [{'Gfg' : 5, 'is' : 6, 'best' : 7, 'for' : 8, 'geeks' : 10},
{'Gfg' : 9, 'is' : 4, 'best' : 10, 'for' : 4, 'geeks' :7},
{'Gfg' : 2, 'is' : 10, 'best' : 8, 'for' : 9, 'geeks' : 3}]
# printing original list
print("The original list is : " + str(test_list))
# initializing keys
op1_key = 'Gfg'
op2_key = 'best'
# Binary operation on specific keys in Dictionary List
# Using list comprehension + enumerate()
res = [(idx, val[op1_key] * val[op2_key]) for idx, val in enumerate(test_list)]
# printing result
print("The constructed result list : " + str(res))
The original list is : [{‘geeks’: 10, ‘for’: 8, ‘Gfg’: 5, ‘is’: 6, ‘best’: 7}, {‘geeks’: 7, ‘for’: 4, ‘Gfg’: 9, ‘is’: 4, ‘best’: 10}, {‘geeks’: 3, ‘for’: 9, ‘Gfg’: 2, ‘is’: 10, ‘best’: 8}]
The constructed result list : [(0, 35), (1, 90), (2, 16)]