简体   繁体   English

如何从python字典中的元组访问密钥?

[英]How to access the key from the tuple in python dictionary?

I have dictionary like this; 我有这样的字典;

  csvDict[key] = {'delivery': delivery, 'tanksystemid': tanksystemid}

Im trying to do a condition check like; 我正在尝试进行条件检查;

tanksystemid=100

for dic in csvDict.values():
                if tanksystemid == dic['tanksystemid']:
                    key of csvDict?

How can i get the key of the csvDict? 我如何获取csvDict的密钥?

You need to just use csvDict.items() instead of csvDict.values() . 您只需要使用csvDict.items()而不是csvDict.values() Like (python3) : 像(python3):

tanksystemid=100
for key, dic in csvDict.items():
                if tanksystemid == dic['tanksystemid']:
                    # key of csvDict?
                    print(f'key: {key}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM