小编ruh*_*ruh的帖子

Python - 元组 - 检索元组列表中的唯一元素

更新:

我去了这个:

set(item[1] for item in id) 
Run Code Online (Sandbox Code Playgroud)

谢谢大家,你的想法对我有帮助。


我正在处理一个元组列表:

使用以下代码行作为示例。我的清单可以是任何长度。但是,我将始终寻找我的元组的给定索引:

id = [(9,'Tup','Check'),(10,'Tup','Pyton'),(11,'Not Tup','Stack'),(12,'Not Tup','Stack')]
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我希望获取独特的第二个元素。

objective_ouput = ('Tup','Not Tup')
Run Code Online (Sandbox Code Playgroud)

python tuples list unique

3
推荐指数
1
解决办法
3308
查看次数

将一个元素元组(int)转换为str

假设一个元组有一个整数:

tup = (19201,)
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种获得此输出的方法:

tup = "(19201)"
Run Code Online (Sandbox Code Playgroud)

到目前为止,我试过这个:

str_tup = "(" + tup[0] + ")"
Run Code Online (Sandbox Code Playgroud)

但它给了我这个错误:

TypeError:无法隐式地将'int'对象转换为str

python string tuples

-1
推荐指数
1
解决办法
759
查看次数

标签 统计

python ×2

tuples ×2

list ×1

string ×1

unique ×1