gpt4 book ai didi

python - 对字典值的操作没有输出

转载 作者:太空宇宙 更新时间:2023-11-04 09:35:49 25 4
gpt4 key购买 nike

我想遍历字典的值并对其应用简单的操作。不幸的是,这两篇 SO 文章对我没有帮助(Perform an operation on each dictionary valuePerform operation on some not all dictionary values)

我的字典包含以下数据:

dict = {'WF:ACAA-CR (auto)': ['Manager', 'Access Responsible', 'Automatic'],
'WF:ACAA-CR-AccResp (auto)': ['Manager', 'Access Responsible', 'Automatic'],
'WF:ACAA-CR-IT-AccResp[AUTO]': ['Group', 'Access Responsible', 'Automatic']}

我的最终目标是根据键包含的值在键上设置标签。例如,“a”将是“Workflow 2”,因为它的值为“Manager”。但首先我只想确保我可以根据字典值运行操作。

我的代码是:

for key, values in dict.items():
if values == "Manager":
print(key)

我之前的尝试包括:

if key.values() == 'Manager':
print(key)

错误信息:

Traceback (most recent call last): File "C:/Users/.PyCharmCE2018.2/config/scratches/UserAR2-extract.py", line 28, in if key.values() == "Manager": AttributeError: 'str' object has no attribute 'values'

对于代码:

for key in dict.values():
if key == "Manager":
print(key)

我没有得到任何输出。

如何对字典应用操作?

最佳答案

看起来您的问题是,如果 Managerlist 值中,您想要print。您只需要将检查从相等(仅当值是 "Manager",而不是在 list 中时才会发生)更改为包含:

for key, values in dict.items():
if "Manager" in values:
print(key)

关于python - 对字典值的操作没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53823265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com