如何在不合并它们的情况下将多个元组(列表,等等)添加到单个字典键中?

时间:2021-10-23 22:30:38

I've been trying to figure out how to add multiple tuples that contain multiple values to to a single key in a dictionary. But with no success so far. I can add the values to a tuple or list, but I can't figure out how to add a tuple so that the key will now have 2 tuples containing values, as opposed to one tuple with all of them.

我一直在试图弄清楚如何将包含多个值的多个元组添加到字典中的单个键。但到目前为止还没有成功。我可以将值添加到元组或列表,但我无法弄清楚如何添加元组,以便键现在有2个包含值的元组,而不是一个包含所有元组的元组。

For instance say the dictionary = {'Key1':(1.000,2.003,3.0029)}

比如说词典= {'Key1':( 1.000,2.003,3.0029)}

and I want to add (2.3232,13.5232,1325.123) so that I end up with:

我想添加(2.3232,13.5232,1325.123)以便我最终得到:

dictionary = {'Key1':((1.000,2.003,3.0029),(2.3232,13.5232,1325.123))} (forgot a set of brackets!)

dictionary = {'Key1':((1.000,2.003,3.0029),(2.3232,13.5232,1325.123))}(忘了一组括号!)

If someone knows how this can be done I'd appreciate the help as it's really starting to annoy me now.

如果有人知道如何做到这一点,我会很感激帮助,因为它现在真的开始惹恼我了。

Thanks!

谢谢!

Edit: Thanks everyone! Ironic that I tried that except at the time I was trying to make the value multiple lists instead of multiple tuples; when the solution was to just enclose the tuples in a list. Ah the irony.

编辑:谢谢大家!讽刺我试过,除了当时我试图使值多个列表而不是多个元组;当解决方案只是将元组括在列表中时。讽刺啊。

6 个解决方案

#1


9  

Use defaultdict and always use append and this will be seemless.

使用defaultdict并始终使用append,这将是无缝的。

from collections import defaultdict

x = defaultdict(list)
x['Key1'].append((1.000,2.003,3.0029))

#2


4  

Just map your key to a list, and append tuples to the list.

只需将您的密钥映射到列表,然后将元组附加到列表中。

d = {'Key1': [(1.000,2.003,3.0029)]}

Then later..

然后......

d['Key1'].append((2.3232,13.5232,1325.123))

Now you have:

现在你有:

{'Key1': [(1.0, 2.003, 3.0029), (2.3232, 13.5232, 1325.123)]}

#3


3  

A dictionary value can't contain two tuples just by themselves. Each dictionary key maps to a single value, so the only way you can have two separate tuples associated with that key is for them to be themselves contained within a tuple or list: {'Key1':[(1.000,2.003,3.0029),(2.3232,13.5232,1325.123)]} - note the extra pair of square brackets.

字典值不能仅包含两个元组。每个字典键映射到一个单独的值,因此唯一可以让两个单独的元组与该键相关联的元组是它们自身包含在元组或列表中:{'Key1':[(1.000,2.003,3.0029), (2.3232,13.5232,1325.123)]} - 注意额外的一对方括号。

One way of doing this would be to get the current value associated with your key, and append it to a list before setting the new list back to that key. But if there's a possibility you'll need that for any key, you should do it for all keys right at the start, otherwise you'll get into all sorts of difficulties working out what level you're at.

执行此操作的一种方法是获取与密钥关联的当前值,并在将新列表设置回该密钥之前将其附加到列表中。但是如果有可能你需要任何键,那么你应该在开始时对所有键都这样做,否则你会遇到各种各样的困难,找出你所处的水平。

#4


1  

Instead of:

代替:

{'Key1':(1.000,2.003,3.0029)}

What you want is:

你想要的是:

{'Key1':[(1.000,2.003,3.0029)]}

When you add another tuple, you'll get:

当你添加另一个元组时,你会得到:

{'Key1':[(1.000,2.003,3.0029), (2.3232,13.5232,1325.123)]}

#5


1  

I think your questions is somewhat badly formulated. You want to:

我认为你的问题有点严重。你想要:

  1. Associate a tuple to a key, if the key is not in the dictionary
  2. 如果密钥不在字典中,则将元组与密钥相关联
  3. Replace the tuple with a list of two tuples, if the key is in the dictionary and points to a tuple
  4. 如果密钥在字典中并指向元组,则将元组替换为两个元组的列表
  5. Append a tuple to the list of tuples associated to a key
  6. 将元组附加到与键关联的元组列表

This could be achieved with the following code:

这可以通过以下代码实现:

def insertTuple(d, k, tup):
    if k not in d:
        d[k] = tup
    elif type(d[k]) == tuple:
        d[k] = [ d[k], tup ]
    else:
        d[k].append(tup)

#6


0  

As we know tuples are not mutuable. Instead of using a tuple of tuples. Use list of tuples this will work.

我们知道元组不可变。而不是使用元组元组。使用这将起作用的元组列表。

first create a list of tuples and then append it to a dictionay key.\

首先创建一个元组列表,然后将其附加到一个dictionay键。

dictionary = {}
listoftuples = []//create a tuples of list
dictionary[key].append(listoftuples)//appending it to a dictionary key 

#1


9  

Use defaultdict and always use append and this will be seemless.

使用defaultdict并始终使用append,这将是无缝的。

from collections import defaultdict

x = defaultdict(list)
x['Key1'].append((1.000,2.003,3.0029))

#2


4  

Just map your key to a list, and append tuples to the list.

只需将您的密钥映射到列表,然后将元组附加到列表中。

d = {'Key1': [(1.000,2.003,3.0029)]}

Then later..

然后......

d['Key1'].append((2.3232,13.5232,1325.123))

Now you have:

现在你有:

{'Key1': [(1.0, 2.003, 3.0029), (2.3232, 13.5232, 1325.123)]}

#3


3  

A dictionary value can't contain two tuples just by themselves. Each dictionary key maps to a single value, so the only way you can have two separate tuples associated with that key is for them to be themselves contained within a tuple or list: {'Key1':[(1.000,2.003,3.0029),(2.3232,13.5232,1325.123)]} - note the extra pair of square brackets.

字典值不能仅包含两个元组。每个字典键映射到一个单独的值,因此唯一可以让两个单独的元组与该键相关联的元组是它们自身包含在元组或列表中:{'Key1':[(1.000,2.003,3.0029), (2.3232,13.5232,1325.123)]} - 注意额外的一对方括号。

One way of doing this would be to get the current value associated with your key, and append it to a list before setting the new list back to that key. But if there's a possibility you'll need that for any key, you should do it for all keys right at the start, otherwise you'll get into all sorts of difficulties working out what level you're at.

执行此操作的一种方法是获取与密钥关联的当前值,并在将新列表设置回该密钥之前将其附加到列表中。但是如果有可能你需要任何键,那么你应该在开始时对所有键都这样做,否则你会遇到各种各样的困难,找出你所处的水平。

#4


1  

Instead of:

代替:

{'Key1':(1.000,2.003,3.0029)}

What you want is:

你想要的是:

{'Key1':[(1.000,2.003,3.0029)]}

When you add another tuple, you'll get:

当你添加另一个元组时,你会得到:

{'Key1':[(1.000,2.003,3.0029), (2.3232,13.5232,1325.123)]}

#5


1  

I think your questions is somewhat badly formulated. You want to:

我认为你的问题有点严重。你想要:

  1. Associate a tuple to a key, if the key is not in the dictionary
  2. 如果密钥不在字典中,则将元组与密钥相关联
  3. Replace the tuple with a list of two tuples, if the key is in the dictionary and points to a tuple
  4. 如果密钥在字典中并指向元组,则将元组替换为两个元组的列表
  5. Append a tuple to the list of tuples associated to a key
  6. 将元组附加到与键关联的元组列表

This could be achieved with the following code:

这可以通过以下代码实现:

def insertTuple(d, k, tup):
    if k not in d:
        d[k] = tup
    elif type(d[k]) == tuple:
        d[k] = [ d[k], tup ]
    else:
        d[k].append(tup)

#6


0  

As we know tuples are not mutuable. Instead of using a tuple of tuples. Use list of tuples this will work.

我们知道元组不可变。而不是使用元组元组。使用这将起作用的元组列表。

first create a list of tuples and then append it to a dictionay key.\

首先创建一个元组列表,然后将其附加到一个dictionay键。

dictionary = {}
listoftuples = []//create a tuples of list
dictionary[key].append(listoftuples)//appending it to a dictionary key