python - 在不丢失括号的情况下,将列表转换为集合,然后与集合列表一起“加入”
问题描述
- 我有一个名为
new_c[a]
.new_c[a] == [1,2,3]
- 我有一个名为
check_com_of_three
. 这是[(4,5,6), (7,8,9)]
。 - 我想
set(new_c[a])
加入check_com_of_three
。
(例如。[(1,2,3),(4,5,6),(7,8,9)]
)
我正在使用 set 来确保没有重复的集合set(check_com_of_three)
>>>set(new_c[a]) + str(',') + set(check_com_of_three)
输出
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
set(new_c[a]) + str(',') + set(check_com_of_three)
TypeError: unsupported operand type(s) for +: 'set' and 'str'
关于我为什么做错的任何解释,以及最好和最简单的方法是什么?
解决方案
由于您已经有一个列表,您可以使用insert
索引位置:
x = (1,2,3)
y = [(4,5,6), (7,8,9)]
y.insert(0, tuple(x))
print(y)
输出:
[(1,2,3), (4,5,6), (7,8,9)]
推荐阅读
- php - 如果一个索引相同而另一个索引不同,则用逗号合并关联数组
- javascript - 如果该产品是变体,如何返回每个产品的变体值?
- php - 如何在laravel中获取两列值,其中第一个是键,第二个是值?
- mysql - 合并多个相关的 MySQL 表并返回 JSON
- android - 使用“enableDangerousExperimentalLeanBuilds”时,世博会构建崩溃
- amazon-web-services - 如何使用 ingress istio 网关的部署为 AWS NLB/ALB 配置 AWS 标签?
- ios - Xcode 没有看到。obj-c 项目中的 swift 文件
- c - 提高通用交换的性能
- javascript - Dayjs 日历返回错误的格式
- python - Python:将文本拆分成单独的英文句子;保留标点符号