Python 为什么我的一些字典键和值消失了?

Python 为什么我的一些字典键和值消失了?,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,这是我的代码: listy = [[(100, 1), (90, 2), (90, 2), (80, 3), (70, 4)], [(100, 1), (90, 2), (90, 2), (80, 3), (80, 3)]] track = [dict(x) for x in listy] print(track) 我有一个嵌套的元组列表,我正在将它们转换为列表中的字典,但问题是一些键和值在从元组更改后正在消失。输出结果如下所示: [{100: 1, 90: 2, 80: 3, 70: 4}

这是我的代码:

listy = [[(100, 1), (90, 2), (90, 2), (80, 3), (70, 4)], [(100, 1), (90, 2), (90, 2), (80, 3), (80, 3)]]
track = [dict(x) for x in listy] 
print(track)
我有一个嵌套的元组列表,我正在将它们转换为列表中的字典,但问题是一些键和值在从元组更改后正在消失。输出结果如下所示:

[{100: 1, 90: 2, 80: 3, 70: 4}, {100: 1, 90: 2, 80: 3}]
[{100:1, 90:2, 90:2, 80:3, 70:4},{100:1, 90:2, 90:2, 80:3, 80:3}]
class update:
    def __init__(self):
        self.items = {}

    def add(self, number, value):
        if number in self.items: # if we've seen this key before
            self.items[number].append(value) # add it to the list
        else: # if we haven't, create a new list
            self.items[number] = [value]

    def __repr__(self):
        return str(self.items)


update_lists = []
for l in listy:
    u = update()
    for (k, v) in l:
        u.add(k, v)
    update_lists.append(u)

print(update_lists)
相反,输出应该是这样的:

[{100: 1, 90: 2, 80: 3, 70: 4}, {100: 1, 90: 2, 80: 3}]
[{100:1, 90:2, 90:2, 80:3, 70:4},{100:1, 90:2, 90:2, 80:3, 80:3}]
class update:
    def __init__(self):
        self.items = {}

    def add(self, number, value):
        if number in self.items: # if we've seen this key before
            self.items[number].append(value) # add it to the list
        else: # if we haven't, create a new list
            self.items[number] = [value]

    def __repr__(self):
        return str(self.items)


update_lists = []
for l in listy:
    u = update()
    for (k, v) in l:
        u.add(k, v)
    update_lists.append(u)

print(update_lists)

为什么我的代码给出了错误的输出,问题的可能解决方案是什么。我正在使用Python 3.x

值似乎消失了,因为您尝试使用重复键创建dict:

将导致:

{1: 'one', 2: 'three'}
Python字典不支持重复键

@马希尔 您可以将多个值关联到字典中的单个键,这样做

multi_value_dict = {}
multi_value_dict.setdefault(key, []).append(value)

由于尝试使用重复键创建dict,值似乎消失:

将导致:

{1: 'one', 2: 'three'}
Python字典不支持重复键

@马希尔 您可以将多个值关联到字典中的单个键,这样做

multi_value_dict = {}
multi_value_dict.setdefault(key, []).append(value)

Python不允许字典中存在重复键。与您的示例类似:

>>> d={100:1, 90:2, 90:3, 80:3, 70:4}
>>> d[90]
3
如果它允许重复的键,它就无法知道是返回
90
2
还是
3

如果要跟踪元组的计数,可以使用
collections.Counter

from collections import Counter
listy = [[(100, 1), (90, 2), (90, 2), (80, 3), (70, 4)], [(100, 1), (90, 2), (90, 2), (80, 3), (80, 3)]]
count = [Counter(x) for x in listy]
print(count)
它给出了输出:

[计数器({(90,2):2,(100,1):1,(80,3):1,(70,4):1}),计数器({(90,2):2,(80,3):2,(100,1):1})]

或者,如果您只想将键与值关联起来,则可以使用以下方法:

[{100: 1, 90: 2, 80: 3, 70: 4}, {100: 1, 90: 2, 80: 3}]
[{100:1, 90:2, 90:2, 80:3, 70:4},{100:1, 90:2, 90:2, 80:3, 80:3}]
class update:
    def __init__(self):
        self.items = {}

    def add(self, number, value):
        if number in self.items: # if we've seen this key before
            self.items[number].append(value) # add it to the list
        else: # if we haven't, create a new list
            self.items[number] = [value]

    def __repr__(self):
        return str(self.items)


update_lists = []
for l in listy:
    u = update()
    for (k, v) in l:
        u.add(k, v)
    update_lists.append(u)

print(update_lists)
这将提供以下输出:


[{100:[1],90:[2,2],80:[3],70:[4]},{100:[1],90:[2,2],80:[3,3]}]
Python不允许字典中出现重复键。与您的示例类似:

>>> d={100:1, 90:2, 90:3, 80:3, 70:4}
>>> d[90]
3
如果它允许重复的键,它就无法知道是返回
90
2
还是
3

如果要跟踪元组的计数,可以使用
collections.Counter

from collections import Counter
listy = [[(100, 1), (90, 2), (90, 2), (80, 3), (70, 4)], [(100, 1), (90, 2), (90, 2), (80, 3), (80, 3)]]
count = [Counter(x) for x in listy]
print(count)
它给出了输出:

[计数器({(90,2):2,(100,1):1,(80,3):1,(70,4):1}),计数器({(90,2):2,(80,3):2,(100,1):1})]

或者,如果您只想将键与值关联起来,则可以使用以下方法:

[{100: 1, 90: 2, 80: 3, 70: 4}, {100: 1, 90: 2, 80: 3}]
[{100:1, 90:2, 90:2, 80:3, 70:4},{100:1, 90:2, 90:2, 80:3, 80:3}]
class update:
    def __init__(self):
        self.items = {}

    def add(self, number, value):
        if number in self.items: # if we've seen this key before
            self.items[number].append(value) # add it to the list
        else: # if we haven't, create a new list
            self.items[number] = [value]

    def __repr__(self):
        return str(self.items)


update_lists = []
for l in listy:
    u = update()
    for (k, v) in l:
        u.add(k, v)
    update_lists.append(u)

print(update_lists)
这将提供以下输出:


[{100:[1],90:[2,2],80:[3],70:[4]},{100:[1],90:[2,2],80:[3,3]}]

不允许重复键,是吗?字典中不能有重复键。但是,有解决方法吗?这取决于您试图解决的问题是什么。为什么你认为你需要它们成为一个dict?你不使用字典,不使用不同的键,也不将值列成一个列表。不允许使用重复键,不是吗?字典中不能有重复键。但是,有解决方法吗?这取决于你试图解决的问题是什么。为什么你认为你需要它们成为一个dict呢?你不需要使用字典,不需要使用不同的键,也不需要把值列成一个列表。