在元组中插入项目 [英] Inserting an item in a Tuple

查看:49
本文介绍了在元组中插入项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道元组是不可变的,但是这种情况使得我需要在每个元组中插入一个额外的值.因此,其中一项是金额,我需要在其旁边添加其他货币的新货币,如下所示:

Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:

('Product', '500.00', '1200.00')

可能吗?

谢谢!

推荐答案

您可以将其转换为列表,插入该项目,然后将其转换回元组.

You can cast it to a list, insert the item, then cast it back to a tuple.

a = ('Product', '500.00', '1200.00')
a = list(a)
a.insert(3, 'foobar')
a = tuple(a)
print a

>> ('Product', '500.00', '1200.00', 'foobar')

这篇关于在元组中插入项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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