如何改变一个元组? [英] how to mutate a tuple?

查看:116
本文介绍了如何改变一个元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我有一个函数,作为一个参数需要一个元组和

返回该元组或它的变异版本。问题是

元组是不可变的,因此我必须创建一个新的元组并将旧元组的

内容复制到一个新元组。


但是如果我只是在运行时知道元组的大小,我该怎么做呢?我希望我可以通过列表来代替..这会更容易,但

我正在传递* args和** kwargs所以我不是真的那个决定使用元组或列表的人b $ b;)


我是第一个有这样问题的人吗?我无法在这个主题上使用谷歌找到任何

。希望有人可以帮助我;)


-Carlo v.Dango

-

使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/

Hello there. I have a function which as an argument takes a tuple and
either returns that tuple or a mutated version of it. The problem is that
tuples are imutable, hence I have to create a new tuple and copy the
content of the old tuple to a new one.

But how do I do this if I only at runtime know the size of the tuple? I
wish I could pass around lists instead.. that would be so much easier, but
I''m passing "*args" and "**kwargs" around so I''m not really the one
deciding the use of tuples or lists ;)

Am I the first one with a problem like this? I''m not able to find anything
using google on this topic. Hope someone can help me ;)

-Carlo v. Dango
--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/

推荐答案

" Carlo v.Dango" < OE ** @ soetu.eu>写道:
"Carlo v. Dango" <oe**@soetu.eu> wrote:
你好。我有一个函数,作为一个参数需要一个元组和
返回该元组或它的变异版本。问题是
元组是可以改变的,因此我必须创建一个新的元组并将旧元组的内容复制到一个新元组。

但是我该怎么做如果我只在运行时知道元组的大小?
Hello there. I have a function which as an argument takes a tuple and
either returns that tuple or a mutated version of it. The problem is that
tuples are imutable, hence I have to create a new tuple and copy the
content of the old tuple to a new one.

But how do I do this if I only at runtime know the size of the tuple?




将输入元组复制到列表中,然后说myTuple = tuple(myList)<对于myInputTuple中的项目,
myList = []



如果这是需要更改的项目:

item =其他东西

myList.append(item)


返回元组(myList)



Copy your input tuple to a list, then say "myTuple = tuple (myList)"

myList = []
for item in myInputTuple:
if this is the item that needs changing:
item = something else
myList.append (item)

return tuple (myList)


Carlo v.Dango问道:
Carlo v. Dango asks:
但是如果我只是在运行时知道元组的大小,我该怎么办呢?我希望我可以通过列表来代替..这会更容易,但是
我正在通过* args和** kwargs所以我不是真的那个决定使用元组或列表的人;)
But how do I do this if I only at runtime know the size of the tuple? I
wish I could pass around lists instead.. that would be so much easier, but
I''m passing "*args" and "**kwargs" around so I''m not really the one
deciding the use of tuples or lists ;)




将* args转换为列表:

args = list(args)


kwargs无论如何应该是一个dict,而不是一个元组。


Emile van Sebille
em***@fenx.com


" Carlo v .Dango < OE ** @ soetu.eu>写在

news:op ************** @ news.kadnet.dom:
"Carlo v. Dango" <oe**@soetu.eu> wrote in
news:op**************@news.kadnet.dom:
你好。我有一个函数,作为一个参数需要一个元组和
返回该元组或它的变异版本。问题是元组是不可改变的,因此我必须创建一个新的元组并将旧元组的内容复制到一个新元组。

但我该怎么办这个,如果我只是在运行时知道元组的大小?
我希望我可以传递列表而不是......这将是如此之多
更容易,但我正在通过* args和** kwargs所以我不是真的决定使用元组或列表的人;)
Hello there. I have a function which as an argument takes a tuple and
either returns that tuple or a mutated version of it. The problem is
that tuples are imutable, hence I have to create a new tuple and copy
the content of the old tuple to a new one.

But how do I do this if I only at runtime know the size of the tuple?
I wish I could pass around lists instead.. that would be so much
easier, but I''m passing "*args" and "**kwargs" around so I''m not
really the one deciding the use of tuples or lists ;)




进入函数后,将原始元组转换为一个列表,然后

改变列表并在返回时将其转换回元组。


例如



On entry to your function, convert the original tuple to a list, then
mutate the list and convert it back to a tuple when returning.

e.g.

def ChangeIt(aTuple):
work = list(aTuple)

work [0] =" Hello"

返回元组(工作)

ChangeIt((1,2,3))
(''Hello'',2,3)
def ChangeIt(aTuple): work = list(aTuple)
work[0] = "Hello"
return tuple(work)
ChangeIt((1,2,3)) (''Hello'', 2, 3)



-

Duncan Booth du ** **@rcp.co.uk

int month(char * p){return(124864 /((p [0] + p [1] -p [2]& 0x1f)+1)%12)[" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\ 4"];} //谁说我的代码模糊不清?


--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?


这篇关于如何改变一个元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆