问题:如何将键值元组列表转换成字典?
我有一个列表,看起来像:
[('A', 1), ('B', 2), ('C', 3)]
我想把它变成一个像这样的字典:
{'A': 1, 'B': 2, 'C': 3}
最好的方法是什么?
编辑:我的元组列表实际上更像是:
[(A, 12937012397), (BERA, 2034927830), (CE, 2349057340)]
I have a list that looks like:
[('A', 1), ('B', 2), ('C', 3)]
I want to turn it into a dictionary that looks like:
{'A': 1, 'B': 2, 'C': 3}
What’s the best way to go about this?
EDIT: My list of tuples is actually more like:
[(A, 12937012397), (BERA, 2034927830), (CE, 2349057340)]
回答 0
这给了我与尝试拆分列表并压缩列表相同的错误。ValueError:字典更新序列元素#0的长度为1916;2个为必填项
那是你的实际问题。
答案是列表中的元素与您认为的不一样。如果键入,myList[0]
您会发现列表的第一个元素不是二元组,例如('A', 1)
,而是1916长度的iterable。
一旦您真正有了原始问题(myList = [('A',1),('B',2),...]
)中所述表格的列表,您所需要做的就是dict(myList)
。
This gives me the same error as trying to split the list up and zip it. ValueError: dictionary update sequence element #0 has length 1916; 2 is required
THAT is your actual question.
The answer is that the elements of your list are not what you think they are. If you type myList[0]
you will find that the first element of your list is not a two-tuple, e.g. ('A', 1)
, but rather a 1916-length iterable.
Once you actually have a list in the form you stated in your original question (myList = [('A',1),('B',2),...]
), all you need to do is dict(myList)
.
回答 1
>>> dict([('A', 1), ('B', 2), ('C', 3)])
{'A': 1, 'C': 3, 'B': 2}
>>> dict([('A', 1), ('B', 2), ('C', 3)])
{'A': 1, 'C': 3, 'B': 2}
回答 2
你有尝试过吗?
>>> l=[('A',1), ('B',2), ('C',3)]
>>> d=dict(l)
>>> d
{'A': 1, 'C': 3, 'B': 2}
Have you tried this?
>>> l=[('A',1), ('B',2), ('C',3)]
>>> d=dict(l)
>>> d
{'A': 1, 'C': 3, 'B': 2}
回答 3
这是处理重复的元组“键”的方法:
# An example
l = [('A', 1), ('B', 2), ('C', 3), ('A', 5), ('D', 0), ('D', 9)]
# A solution
d = dict()
[d [t [0]].append(t [1]) if t [0] in list(d.keys())
else d.update({t [0]: [t [1]]}) for t in l]
d
OUTPUT: {'A': [1, 5], 'B': [2], 'C': [3], 'D': [0, 9]}
Here is a way to handle duplicate tuple “keys”:
# An example
l = [('A', 1), ('B', 2), ('C', 3), ('A', 5), ('D', 0), ('D', 9)]
# A solution
d = dict()
[d [t [0]].append(t [1]) if t [0] in list(d.keys())
else d.update({t [0]: [t [1]]}) for t in l]
d
OUTPUT: {'A': [1, 5], 'B': [2], 'C': [3], 'D': [0, 9]}
回答 4
使用字典推导的另一种方式
>>> t = [('A', 1), ('B', 2), ('C', 3)]
>>> d = { i:j for i,j in t }
>>> d
{'A': 1, 'B': 2, 'C': 3}
Another way using dictionary comprehensions,
>>> t = [('A', 1), ('B', 2), ('C', 3)]
>>> d = { i:j for i,j in t }
>>> d
{'A': 1, 'B': 2, 'C': 3}
回答 5
如果Tuple没有重复键,则很简单。
tup = [("A",0),("B",3),("C",5)]
dic = dict(tup)
print(dic)
如果元组具有键重复。
tup = [("A",0),("B",3),("C",5),("A",9),("B",4)]
dic = {}
for i, j in tup:
dic.setdefault(i,[]).append(j)
print(dic)
If Tuple has no key repetitions, it’s Simple.
tup = [("A",0),("B",3),("C",5)]
dic = dict(tup)
print(dic)
If tuple has key repetitions.
tup = [("A",0),("B",3),("C",5),("A",9),("B",4)]
dic = {}
for i, j in tup:
dic.setdefault(i,[]).append(j)
print(dic)
回答 6
l=[['A', 1], ['B', 2], ['C', 3]]
d={}
for i,j in l:
d.setdefault(i,j)
print(d)
l=[['A', 1], ['B', 2], ['C', 3]]
d={}
for i,j in l:
d.setdefault(i,j)
print(d)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。