gpt4 book ai didi

Python: Append tuple to a set with tuples(Python:将元组附加到具有元组的集合)

转载 作者:bug小助手 更新时间:2023-10-24 20:32:27 28 4
gpt4 key购买 nike



Following is my code which is a set of tuples:

以下是我的代码,它是一组元组:



data = {('A',20160129,36.44),('A',20160201,37.37),('A',20160104,41.06)};
print(data);


Output: set([('A', 20160129, 36.44), ('A', 20160104, 41.06), ('A', 20160201, 37.37)])

输出:设置([(‘A’,20160129,36.44),(‘A’,20160104,41.06),(‘A’,20160201,37.37)])



How do I append another tuple ('A', 20160000, 22) to data?

如何将另一个元组(‘A’,20160000,22)追加到数据?



Expected output: set([('A', 20160129, 36.44), ('A', 20160104, 41.06), ('A', 20160201, 37.37), ('A', 20160000, 22)])

预期产量:SET([(‘A’,20160129,36.44),(‘A’,20160104,41.06),(‘A’,20160201,37.37),(‘A’,20160000,22)])



Note: I found a lot of resources to append data to a set but unfortunately none of them have the input data in the above format. I tried append, | & set functions as well.

注意:我找到了很多资源来将数据追加到集合中,但不幸的是,它们都没有上述格式的输入数据。我还尝试了APPEND、|和SET函数。


更多回答

data.add(('A', 20160000, 22))?

Data.add((‘A’,20160000,22))?

优秀答案推荐

the trick is to send it inside brackets so it doesn't get exploded

诀窍是把它放在括号里,这样它就不会爆炸



data.update([('A', 20160000, 22)])


just use data.add. Here's an example:

只需使用data.add.下面是一个例子:



x = {(1, '1a'), (2, '2a'), (3, '3a')}

x.add((4, '4a'))

print(x)


Output: {(3, '3a'), (1, '1a'), (2, '2a'), (4, '4a')}

输出:{(3,‘3a’),(1,‘1a’),(2,‘2a’),(4,‘4a’)}



s = {('a',1)}
s.add(('b',2))


output: {('a', 1),('b', 2)}



s.update(('c',3))


output: {('a', 1), 3, 'c', ('b', 2)}



There can be loss of generality by using update.


Better to use add.

It is a cleaner way of operating sets.

使用UPDATE可能会失去通用性。最好使用Add。这是一种更清洁的操作设备的方式。



This failure also matching the question title:

此失败还与问题标题匹配:


your_set = set
your_set.add(('A', 20160000, 22))

because it should be:

因为它应该是:


your_set = set()

更多回答

Do you mind explaining why passing in a list of tuple(s) works?

你介意解释一下为什么传递一个元组列表(S)有效吗?

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com