gpt4 book ai didi

python - 如何在 PYTHON 中的列表字典中迭代值

转载 作者:行者123 更新时间:2023-12-05 03:48:56 25 4
gpt4 key购买 nike

我有一个复杂的方法需要正常工作,但我似乎无法弄清楚为什么它不能正常工作。

我有一个字典如下:

{'view': ['premium_subscribers', 'premium_content'], 'delete': ['admins', 'normal_content', 'premium_content']}

我需要知道如何逐个迭代每个特定的键值,它们是数组。例如:

key = delete

for loop (that iterates 1 by 1 through key "delete's" values

takes "admins" value and does some processing

in the next iteration takes normal_content and does same processing

and so on ......

its basically checking for a match to be found.

如果您对我的方法感兴趣,可以在下面找到。它有 3 个字典,正在比较它们的键值以完成访问权限。

如果 for 循环正确遍历该键的每个值,它将按预期开始工作。


def accessPermission(operation, user, object):

domains = dict()
types = dict()
access = dict()

access = json.load(open("access.txt"))
domains = json.load(open("domains.txt"))
types = json.load(open("types.txt"))

print(types)
print(access)
print(domains)

if operation in access.keys():
firstkeyvalue = access.get(operation)[0]
if firstkeyvalue in domains.keys():
if user in domains.get(firstkeyvalue):
for access.get(operation)[0] in access:
if object in types.values():
print("Success")
else:
print("Error: access denied")
else:
print("Error: access denied")
else:
print("Error: access denied")
else:
print("Error: access denied")

最佳答案

似乎您只需要像这样遍历元素:

dict = {'view': ['premium_subscribers', 'premium_content'], 'delete': ['admins', 'normal_content', 'premium_content']}

key = 'delete' #set somewhere
for v in dict[key]:
print(v)
#do what you need with that

输出:

admins
normal_content
premium_content

关于python - 如何在 PYTHON 中的列表字典中迭代值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64166591/

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