简体   繁体   English

如何将元组元组中的变量更改为具有所需值的元组

[英]How to change a variable in a tuple of tuples to a tuple with wanted value

How to add a variable to each tuple's item that is all ready in a tuple and make the item and the variable a tuple.如何将变量添加到元组中所有准备好的每个元组的项目中,并使项目和变量成为元组。
Input: x = (('U', 'H', 'S', 'H'), ('H', 'U', 'H', 'S'), ('U', 'H', 'H', 'U'))输入: x = (('U', 'H', 'S', 'H'), ('H', 'U', 'H', 'S'), ('U', 'H', 'H', 'U'))

Wanted output: y = ((('U',0), ('H',0), ('S',0), ('H',0)), (('H',0), ('U',0), ('H',0), ('S',0)), (('U',0), ('H',0), ('H',0), ('U',0)))想要的输出: y = ((('U',0), ('H',0), ('S',0), ('H',0)), (('H',0), ('U',0), ('H',0), ('S',0)), (('U',0), ('H',0), ('H',0), ('U',0)))

I tried to convert it into a dictionary and then back to a tuple but it seemed inefficient.我试图将其转换为字典,然后再转换回元组,但似乎效率低下。 Is there a better way of doing so?有没有更好的方法呢?

使用发电机:

y = tuple(tuple((sub_el, 0) for sub_el in el) for el in x)

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

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