简体   繁体   English

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

[英]How do you add multiple tuples(lists, whatever) to a single dictionary key without merging them?

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. 讽刺啊。

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))

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)]}

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)]} - 注意额外的一{'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. 但是如果有可能你需要任何键,那么你应该在开始时对所有键都这样做,否则你将遇到各种各样的困难,找出你所处的水平。

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)]}

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. Replace the tuple with a list of two tuples, if the key is in the dictionary and points to a tuple 如果密钥在字典中并指向元组,则将元组替换为两个元组的列表
  3. Append a tuple to the list of tuples associated to a key 将元组附加到与键关联的元组列表

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)

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 

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

相关问题 如何将多个列表添加到单个字典键而不合并它们? 并转换为CSV - How do you add multiple lists to a single dictionary key without merging them? and convert to csv 在不合并元组的情况下将多个元组添加到单个字典键中? - Adding multiple tuples to a single dictionary key without merging the tuples? 如何在没有逗号的情况下将多个值添加到字典中的单个键中? - How can you add multiple values into a single key in a dictionary without it having commas? 如何将两个列表添加到一个新列表中而不在 Python 中合并它们? - How to add two lists into a new one without merging them in Python? 如何组合多个列表并将它们作为单个键放入字典? - How to combine several lists and put them as a single key into a dictionary? python列表/元组列表合并字典 - python merging dictionary of lists of lists/tuples 将列表中的嵌套元组转换为字典并添加新的键值 - convert nested tuples in lists into a dictionary and add new key values 如何将多个列表附加到python字典中的一个键? - How do I append multiple lists to one key in a python dictionary? 如何展平 Python 字典中键的值(元组列表列表)? - How to flatten the value (list of lists of tuples) of a key in Python dictionary? 从元组列表中包含列表的字典理解 - Dictionary comprehension from a list of tuples with lists in them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM