简体   繁体   English

将元组添加到列表中而不加引号?

[英]Adding tuples to a list without them gaining quotation marks?

I have some variables that look something like this:我有一些看起来像这样的变量:

Word1 = list[0]
Word2 = list[2]

Each variable will output this when printed:每个变量在打印时都会 output 这个:

print(Word1)
#prints ('Example 1', 5, 10, 15, 20)
print(Word2)
#prints ('Example 2', 10, 20, 13, 17)

but as soon as I add these to a list, they gain quotation marks around them但是一旦我将这些添加到列表中,它们就会在它们周围加上引号

NewList.append(Word1)
NewList.append(Word2)
#Newlist becomes ["('Example 1', 5, 10, 15, 20)", "('Example 2', 10, 20, 13, 17)"] when printed.

Is there any way I can remove the quotation marks that appear when adding them to a list, so that NewList can look like this:有什么方法可以删除将它们添加到列表时出现的引号,以便NewList看起来像这样:

[('Example 1', 5, 10, 15, 20), ('Example 2', 10, 20, 13, 17)]

You can use eval您可以使用eval

Word1 = eval(list[0])
Word2 = eval(list[2])

NewList.append(Word1)
NewList.append(Word2)

Output: Output:

[('Example 1', 5, 10, 15, 20), ('Example 2', 10, 20, 13, 17)]

Hope this answers your question!!!希望这能回答你的问题!!!

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

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