简体   繁体   English

Python:向元组添加元素

[英]Python: adding element to tuple

i have google searched for hours but still not able to find a solution.我已经谷歌搜索了几个小时,但仍然无法找到解决方案。

x = ((1, 2), (3, 4))
y = 5.0

Is that possible to convert to the tuple format below?是否可以转换为下面的元组格式?

(((1, 2), 5.0), (3, 4))

Edit: Solved.编辑:解决了。 Thanks all.谢谢大家。

Tuples are immutable.元组是不可变的。 But they do support indexing, so you can easily create a new one:但它们确实支持索引,因此您可以轻松创建一个新索引:

x = ((x[0], y), x[1])

# (((1, 2), 5.0), (3, 4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM