how to add value to a tuple?(如何为元组增加价值?)
问题描述
我正在编写一个脚本,其中有一个元组列表,例如 ('1','2','3','4').例如:
I'm working on a script where I have a list of tuples like ('1','2','3','4'). e.g.:
list = [('1','2','3','4'),
('2','3','4','5'),
('3','4','5','6'),
('4','5','6','7')]
现在我需要添加'1234'、'2345'、'3456'和'4567' 分别在每个元组的末尾.例如:
Now I need to add '1234', '2345','3456' and '4567' respectively at the end of each tuple. e.g:
list = [('1','2','3','4','1234'),
('2','3','4','5','2345'),
('3','4','5','6','3456'),
('4','5','6','7','4567')]
有没有可能?
推荐答案
元组是不可变的,不应该改变——这就是列表类型的用途.
Tuples are immutable and not supposed to be changed - that is what the list type is for.
但是,您可以使用 originalTuple + (newElement,) 替换每个元组,从而创建一个新元组.例如:
However, you can replace each tuple using originalTuple + (newElement,), thus creating a new tuple. For example:
t = (1,2,3)
t = t + (1,)
print(t)
(1,2,3,1)
但我宁愿建议从头开始使用列表,因为它们插入项目的速度更快.
But I'd rather suggest to go with lists from the beginning, because they are faster for inserting items.
还有一个提示:不要覆盖程序中的内置名称 list,而是调用变量 l 或其他名称.如果覆盖内置名称,则不能在当前范围内再使用它.
And another hint: Do not overwrite the built-in name list in your program, rather call the variable l or some other name. If you overwrite the built-in name, you can't use it anymore in the current scope.
这篇关于如何为元组增加价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为元组增加价值?
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
