如何在字典中查找特定值并正确 return 它们
How to look for specific values in a dictionary and return them properly
所以我有一个字典列表字典 (lst),我试图遍历它,比较值,然后 return 适当的值。
我有以下代码来检索从命令行给出的 2 个参数,通过字典条目比较它们,return 适当的值:
def get_attribute_number(cmd1, cmd2, lst):
app_name = cmd1
attribute = cmd2
for name in lst: #lst is a dictionary
if name["description"] == app_name:
print(int(name[attribute]))
get_attribute_number(cmd1=sys.argv[1], cmd2=sys.argv[2], lst=data_list)
但是,我的程序没有 returning 任何东西。例如,如果我提供 cmd 行参数 python3 program.py [NAME] [ATTRIBUTE]
,它应该查询上述数据,直到找到名称的匹配项,并且 return 其请求的属性为整数。任何人都可以就为什么我不能 print(int(name[attribute]))
对应 napp_name
提出建议吗?
我稍微更改了您的代码以对用户更友好:
import sys
data_list = [{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos']", '{#PASSED}': '316', '{#FAILED}': '1', '{#WARNING}': '204', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/python-openflow']", '{#PASSED}': '400', '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': '40', '{#XFAILED}': '1'},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_core']", '{#PASSED}': '49', '{#FAILED}': 0, '{#WARNING}': '39', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos-utils']", '{#PASSED}': '102', '{#FAILED}': '2', '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/mef_eline']", '{#PASSED}': '102', '{#FAILED}': 0, '{#WARNING}': '88', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/flow_manager']", '{#PASSED}': '36', '{#FAILED}': 0, '{#WARNING}': '22', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/topology']", '{#PASSED}': '58', '{#FAILED}': 0, '{#WARNING}': '56', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/kronos']", '{#PASSED}': '61', '{#FAILED}': 0, '{#WARNING}': '21', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/pathfinder']", '{#PASSED}': 0, '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/status']", '{#PASSED}': '81', '{#FAILED}': 0, '{#WARNING}': '1', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_l2ls']", '{#PASSED}': '7', '{#FAILED}': 0, '{#WARNING}': '7', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_lldp']", '{#PASSED}': '17', '{#FAILED}': 0, '{#WARNING}': '17', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/maintenance']", '{#PASSED}': '28', '{#FAILED}': 0, '{#WARNING}': '28', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/storehouse']", '{#PASSED}': '63', '{#FAILED}': '1', '{#WARNING}': '32', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos-end-to-end-tester']", '{#PASSED}': 0, '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0}]
def get_attribute_number(cmd1, cmd2, lst):
"""For each attribute under a napp, retrieve that attribute as a NUMBER"""
napp_name = cmd1
attribute_name = cmd2
for napp in lst: #lst is a dictionary
if napp_name in napp["{#DESCRIPTION}"]: # change happened here
print(napp)
print(int(napp[attribute_name]))
get_attribute_number(sys.argv[1], sys.argv[2], data_list)
基本上他们只需要输入子字符串就可以得到一些结果,所以这个:
python tt.py 'kytos/kytos' {#SKIPPED}
只输出需要的数据:
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos']", '{#PASSED}': '316', '{#FAILED}': '1', '{#WARNING}': '204', '{#SKIPPED}': 0, '{#XFAILED}': 0}
0
要回答有关转换为字符串的问题:您可以尝试评估将为您提供列表的表达式;如果格式始终相同,您还可以使用正则表达式:
import ast
ast.literal_eval(napp["{#DESCRIPTION}"])[0]
所以在你的情况下:
import ast
def get_attribute_number(cmd1, cmd2, lst):
"""For each attribute under a napp, retrieve that attribute as a NUMBER"""
napp_name = cmd1
attribute_name = cmd2
for napp in lst: #lst is a dictionary
if ast.literal_eval(napp["{#DESCRIPTION}"])[0] == napp_name:
print(int(napp[attribute_name]))
所以我有一个字典列表字典 (lst),我试图遍历它,比较值,然后 return 适当的值。
我有以下代码来检索从命令行给出的 2 个参数,通过字典条目比较它们,return 适当的值:
def get_attribute_number(cmd1, cmd2, lst):
app_name = cmd1
attribute = cmd2
for name in lst: #lst is a dictionary
if name["description"] == app_name:
print(int(name[attribute]))
get_attribute_number(cmd1=sys.argv[1], cmd2=sys.argv[2], lst=data_list)
但是,我的程序没有 returning 任何东西。例如,如果我提供 cmd 行参数 python3 program.py [NAME] [ATTRIBUTE]
,它应该查询上述数据,直到找到名称的匹配项,并且 return 其请求的属性为整数。任何人都可以就为什么我不能 print(int(name[attribute]))
对应 napp_name
提出建议吗?
我稍微更改了您的代码以对用户更友好:
import sys
data_list = [{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos']", '{#PASSED}': '316', '{#FAILED}': '1', '{#WARNING}': '204', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/python-openflow']", '{#PASSED}': '400', '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': '40', '{#XFAILED}': '1'},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_core']", '{#PASSED}': '49', '{#FAILED}': 0, '{#WARNING}': '39', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos-utils']", '{#PASSED}': '102', '{#FAILED}': '2', '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/mef_eline']", '{#PASSED}': '102', '{#FAILED}': 0, '{#WARNING}': '88', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/flow_manager']", '{#PASSED}': '36', '{#FAILED}': 0, '{#WARNING}': '22', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/topology']", '{#PASSED}': '58', '{#FAILED}': 0, '{#WARNING}': '56', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/kronos']", '{#PASSED}': '61', '{#FAILED}': 0, '{#WARNING}': '21', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/pathfinder']", '{#PASSED}': 0, '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/status']", '{#PASSED}': '81', '{#FAILED}': 0, '{#WARNING}': '1', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_l2ls']", '{#PASSED}': '7', '{#FAILED}': 0, '{#WARNING}': '7', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_lldp']", '{#PASSED}': '17', '{#FAILED}': 0, '{#WARNING}': '17', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/maintenance']", '{#PASSED}': '28', '{#FAILED}': 0, '{#WARNING}': '28', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/storehouse']", '{#PASSED}': '63', '{#FAILED}': '1', '{#WARNING}': '32', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos-end-to-end-tester']", '{#PASSED}': 0, '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0}]
def get_attribute_number(cmd1, cmd2, lst):
"""For each attribute under a napp, retrieve that attribute as a NUMBER"""
napp_name = cmd1
attribute_name = cmd2
for napp in lst: #lst is a dictionary
if napp_name in napp["{#DESCRIPTION}"]: # change happened here
print(napp)
print(int(napp[attribute_name]))
get_attribute_number(sys.argv[1], sys.argv[2], data_list)
基本上他们只需要输入子字符串就可以得到一些结果,所以这个:
python tt.py 'kytos/kytos' {#SKIPPED}
只输出需要的数据:
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos']", '{#PASSED}': '316', '{#FAILED}': '1', '{#WARNING}': '204', '{#SKIPPED}': 0, '{#XFAILED}': 0}
0
要回答有关转换为字符串的问题:您可以尝试评估将为您提供列表的表达式;如果格式始终相同,您还可以使用正则表达式:
import ast
ast.literal_eval(napp["{#DESCRIPTION}"])[0]
所以在你的情况下:
import ast
def get_attribute_number(cmd1, cmd2, lst):
"""For each attribute under a napp, retrieve that attribute as a NUMBER"""
napp_name = cmd1
attribute_name = cmd2
for napp in lst: #lst is a dictionary
if ast.literal_eval(napp["{#DESCRIPTION}"])[0] == napp_name:
print(int(napp[attribute_name]))