如何更改元组中的值? [英] How to change values in a tuple?
本文介绍了如何更改元组中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为 values
的元组,它包含以下内容:
I have a tuple called values
which contains the following:
('275', '54000', '0.0', '5000.0', '0.0')
我想更改此元组中的第一个值(即 275
),但我知道元组是不可变的,因此 values[0] = 200
将不起作用.我怎样才能做到这一点?
I want to change the first value (i.e., 275
) in this tuple but I understand that tuples are immutable so values[0] = 200
will not work. How can I achieve this?
推荐答案
首先你需要问,你为什么要这样做?
First you need to ask, why you want to do this?
但可以通过:
t = ('275', '54000', '0.0', '5000.0', '0.0')
lst = list(t)
lst[0] = '300'
t = tuple(lst)
但是如果您需要更改某些内容,最好将其保留为 list
But if you're going to need to change things, you probably are better off keeping it as a list
这篇关于如何更改元组中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文