python词典中的键来自列表

发布于 2025-01-22 06:05:11 字数 144 浏览 2 评论 0原文

如何打印字典的某些值,在其中我应该从列表中迭代它是键?

fe->我有一个带有密码子的列表:['auu','ggg'],让我们假设auu和ggg在字典中定义为键,它们的值是x和y,我希望能够在整个过程中读取列表并打印x和y。

感谢您的帮助。

How can I print the certain values of a dictionary,where I should iterate it’s keys from a list?

F.e. -> I have a list with codons: [‘AUU’,’GGG’], and let’s assume that auu and ggg is defined in a dictionary as keys, and their values are x and y, I want to be able to read the list throughoutly, and print x and y.

Thanks for the help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷爱 2025-01-29 06:05:11

尝试以下操作:

keys = ['auu', 'ggg']
a_dict = {'auu': 'x', 'ggg': 'y', 'hhh': 'z'}

for i in keys:
    print(a_dict[i])

Try this :

keys = ['auu', 'ggg']
a_dict = {'auu': 'x', 'ggg': 'y', 'hhh': 'z'}

for i in keys:
    print(a_dict[i])
小镇女孩 2025-01-29 06:05:11

使用项目()方法 - &gt; https://www.geeksforgeeks.orgss.orgs.org/python-dictionary-dictionary-itemms-method/ << /a>

a_dict = {'auu': 'x', 'ggg': 'y', 'hhh': 'z'}

for a, b  in a_dict.items():
    print(a, b)

Using items() method -> https://www.geeksforgeeks.org/python-dictionary-items-method/

a_dict = {'auu': 'x', 'ggg': 'y', 'hhh': 'z'}

for a, b  in a_dict.items():
    print(a, b)
那一片橙海, 2025-01-29 06:05:11

该代码可以完成工作,

test = {"AUU": "x", "GGG": "y"}  

for key in test:
  print(test[key])

test = {"AUU": "x", "GGG": "y"}  

for value in test.values():
  print(value)

代码都返回相同的输出。

输出 -

x
y

如果要将值存储在列表中,则只需使用

list(test.values())

test.value()将返回一个类型dict_values的对象,这是不可能的。因此,将其传递到list()中,将其转换为列表,使其具有疑问。

This code does the job,

test = {"AUU": "x", "GGG": "y"}  

for key in test:
  print(test[key])

OR

test = {"AUU": "x", "GGG": "y"}  

for value in test.values():
  print(value)

Both the code return the same output.

Output -

x
y

If you want to store the values in a list just use,

list(test.values())

test.value() returns you an object of type dict_values which isn't iterable. So passing it inside list() converts it into list, making it iterable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文