如何为元组增加价值?

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.

这篇关于如何为元组增加价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)