简体   繁体   English

Python-将字典中的值转换为列表

[英]Python- turn values in a dictionary into a list

i have a question:我有个问题:

I have a dictionary like this我有一本这样的字典

{1: [4, 2, 1, 3], 2: [4, 3, 1, 2], 3: [4, 3, 1, 2]}

and I want to get each of the values into their own list within a loop eg.我想在一个循环中将每个值放入它们自己的列表中,例如。

for each key in myDict
    myList = [4,2,1,3]

do something in my list/ clear my list then loop around so...在我的列表中做一些事情/清除我的列表然后循环所以......

myList = [4,3,1,2]

I have no idea how to attempt this, does anybody have any suggestions?我不知道如何尝试这个,有人有什么建议吗?

Dictionaries have a.values() method, and you can use it like so:字典有a.values()方法,你可以像这样使用它:

for myList in myDict.values():
    print(myList)  # Do stuff

Keep in mind camelCase isn't a convention in Python.请记住,骆驼大小写不是 Python 中的约定。

solution of you problem你的问题的解决方案

a={1: [4, 2, 1, 3], 2: [4, 3, 1, 2], 3: [4, 3, 1, 2]} a={1: [4, 2, 1, 3], 2: [4, 3, 1, 2], 3: [4, 3, 1, 2]}

mylist=a[1]我的列表=一个[1]

print(mylist)打印(我的列表)

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

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