Python 如何为元组添加值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4913397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:06:37  来源:igfitidea点击:

how to add value to a tuple?

pythontuples

提问by Shahzad

I'm working on a script where I have a list of tuples like ('1','2','3','4'). e.g.:

我正在编写一个脚本,其中有一个像('1','2','3','4'). 例如:

list = [('1','2','3','4'),
        ('2','3','4','5'),
        ('3','4','5','6'),
        ('4','5','6','7')]

Now I need to add '1234', '2345','3456'and '4567'respectively at the end of each tuple. e.g:

现在我需要添加'1234''2345''3456''4567'分别为每个元组的结尾。例如:

list = [('1','2','3','4','1234'),
        ('2','3','4','5','2345'),
        ('3','4','5','6','3456'),
        ('4','5','6','7','4567')]

Is it possible in any way?

有可能吗?

回答by Pablo Santa Cruz

In Python, you can't. Tuples are immutable.

在 Python 中,你不能。元组是不可变的。

On the containing list, you could replace tuple ('1', '2', '3', '4')with a different ('1', '2', '3', '4', '1234')tuple though.

在包含列表上,您可以用('1', '2', '3', '4')不同的('1', '2', '3', '4', '1234')元组替换元组。

回答by atp

Based on the syntax, I'm guessing this is Python. The point of a tuple is that it is immutable, so you need to replace each element with a new tuple:

根据语法,我猜这是 Python。元组的要点是它是不可变的,所以你需要用一个新的元组替换每个元素:

list = [l + (''.join(l),) for l in list]
# output:
[('1', '2', '3', '4', '1234'), 
 ('2', '3', '4', '5', '2345'), 
 ('3', '4', '5', '6', '3456'), 
 ('4', '5', '6', '7', '4567')]

回答by AndiDog

Tuples are immutable and not supposed to be changed - that is what the list type is for. You could replace each tuple by originalTuple + (newElement,), thus creating a new tuple. For example:

元组是不可变的,不应该被改变——这就是列表类型的用途。您可以用 替换每个元组originalTuple + (newElement,),从而创建一个新元组。例如:

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.

但我宁愿建议从一开始就使用列表,因为它们插入项目的速度更快。

And another hint: Do not overwrite the built-in name listin your program, rather call the variable lor some other name. If you overwrite the built-in name, you can't use it anymore in the current scope.

另一个提示:不要覆盖程序中的内置名称list,而是调用变量l或其他名称。如果覆盖内置名称,则不能在当前作用域中再使用它。

回答by Ryan

As other people have answered, tuples in python are immutable and the only way to 'modify' one is to create a new one with the appended elements included.

正如其他人所回答的那样,python 中的元组是不可变的,“修改”元组的唯一方法是创建一个包含附加元素的新元组。

But the best solution is a list. When whatever function or method that requires a tuple needs to be called, create a tuple by using tuple(list).

但最好的解决方案是列表。当需要调用需要元组的任何函数或方法时,使用 tuple(list) 创建一个元组。

回答by Viktor

    list_of_tuples = [('1', '2', '3', '4'),
                      ('2', '3', '4', '5'),
                      ('3', '4', '5', '6'),
                      ('4', '5', '6', '7')]


    def mod_tuples(list_of_tuples):
        for i in range(0, len(list_of_tuples)):
            addition = ''
            for x in list_of_tuples[i]:
                addition = addition + x
            list_of_tuples[i] = list_of_tuples[i] + (addition,)
        return list_of_tuples

    # check: 
    print mod_tuples(list_of_tuples)

回答by Dipak

I was going through some details related to tupleand list, and what I understood is:

我正在查看与tupleand相关的一些细节list,我的理解是:

  • Tuples are Heterogeneouscollection data type
  • Tuple has Fixed length (per tuple type)
  • Tuple are Always finite
  • 元组是Heterogeneous集合数据类型
  • 元组具有固定长度(每个元组类型)
  • 元组总是有限的

So for appending new item to a tuple, need to cast it to list, and do append()operation on it, then again cast it back to tuple.

因此,要将新项目附加到元组,需要将其转换为list,并对其进行append()操作,然后再次将其转换回元组。

But personally what I felt about the Question is, if Tuplesare supposed to be finite, fixed lengthitems and if we are using those data types in our application logics then there should notbe a scenario to appending new items OR updating an item value in it. So instead of list of tuplesit should be list of listitself, Am I right on this?

但我个人是什么,我觉得这样一个问题,如果元组应该是有限的固定长度的东西,如果我们在我们的应用程序逻辑使用这些数据类型的话,应该不会是追加新项目或更新中的一个项目价值的情景它。因此,而不是元组列表,它应该是列表本身的列表,我对吗?

回答by Terrabits

As mentioned in other answers, tuples are immutable once created, and a list might serve your purposes better.

正如其他答案中提到的,元组一旦创建就是不可变的,列表可能更适合您的目的。

That said, another option for creating a new tuple with extra items is to use the splat operator:

也就是说,创建具有额外项的新元组的另一种选择是使用 splat 运算符:

new_tuple = (*old_tuple, 'new', 'items')

new_tuple = (*old_tuple, 'new', 'items')

I like this syntax because it lookslike a new tuple, so it clearly communicates what you're trying to do.

我喜欢这种语法,因为它看起来像一个新的元组,所以它清楚地传达了你想要做什么。

Using splat, a potential solution is:

使用 splat,一个潜在的解决方案是:

list = [(*i, ''.join(i)) for i in list]

list = [(*i, ''.join(i)) for i in list]

回答by Questwalker

OUTPUTS = []
for number in range(len(list_of_tuples))):
    tup_ = list_of_tuples[number]
    list_ = list(tup_)  
    item_ = list_[0] + list_[1] + list_[2] + list_[3]
    list_.append(item_)
    OUTPUTS.append(tuple(list_))

OUTPUTS is what you desire

输出就是你想要的