python-3.x - How to get the value with one of tuples?
问题描述
Here is my code
crel = {('hanso', 'unif', 'manjo', 'sandi'): ['coiu','lemin'],('yyy', 'qe', 'tgg', 'ijg'):['KOH','TYH']}
key = 'tgg'
for k, v in crel.items():
if key in k:
print(v)
else:
print('no result')
Output:
no result
['KOH', 'TYH']
I want to use one of tuples to get all the values in the list, but there is always "no result" comes out along with my desired result.
I know it's because I use "for" loop, but I have tried really hard to avoid this happening.
How do I use 'tgg' to get the result like this:
['KOH', 'TYH']
Or, use 'hanso' to get the result like this:
['coiu','lemin']
Could someone please help me use one of the tuples and show the corresponding value? When there is really not a match, show 'no result'
解决方案
crel = {('hanso', 'unif', 'manjo', 'sandi'): ['coiu', 'lemin'],
('yyy', 'qe', 'tgg', 'ijg'): ['KOH', 'TYH']}
key = 'tgg'
values = [crel[k] for k in crel.keys() if key in k]
if len(values) > 0:
for val in values:
print(val)
else:
print("no result")
推荐阅读
- node.js - 如何使用 node_mailer 发送 gzip 文件
- python - 如何在python中注释掉一行代码?
- spring - Spring TransactionManager 行为与 Spring Data 和 JpaRepository
- javascript - 使用 WP 辅助功能插件的 Wordpress 验证错误
- flutter - 使用 photo_view 包来适应像 BoxFit.cover 这样的图像
- sql - 动态调整最近 5 天的生产
- python-3.x - 如何防止 GCP Vertex 从我的 docker run 中丢失日志?
- angular - 如何使用 angular、firebase 实现无密码登录身份验证并将电子邮件存储在 mongodb-stitch 中?
- javascript - 如何将对象传递给formick表单
- flutter - Firebase rules/secure in collectionGroup use if state