Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从嵌套列表中的特定点获取唯一值_Python_Python 2.7_Renpy - Fatal编程技术网

Python 从嵌套列表中的特定点获取唯一值

Python 从嵌套列表中的特定点获取唯一值,python,python-2.7,renpy,Python,Python 2.7,Renpy,我有这样一份清单: $ mylist = [(u'nr',<object>,'string1'),(u'fm',<object>,'string2'),(u'nr',<object>,'string3')] $mylist=[(u'nr','string1'),(u'fm','string2'),(u'nr','string3')] 我想从该列表中获得nr和fm的唯一值(即,第一个元素没有重复值) 我一直在考虑使用set()等工具获取唯一列表,我尝试了以下

我有这样一份清单:

$ mylist = [(u'nr',<object>,'string1'),(u'fm',<object>,'string2'),(u'nr',<object>,'string3')]
$mylist=[(u'nr','string1'),(u'fm','string2'),(u'nr','string3')]
我想从该列表中获得
nr
fm
的唯一值(即,第一个元素没有重复值)

我一直在考虑使用
set()
等工具获取唯一列表,我尝试了以下方法(从另一个线程):

$unique=reduce(lambda l,x:l.append(x)或l如果x不在l else l,mylist,[])

但那没用


被要求澄清:我希望在最终结果中使用['nr','fm'],这将生成元组列表中唯一的第一个值:

set(x[0] for x in mylist)
如果要将其返回列表,请执行以下操作:

list(set(x[0] for x in mylist))

这将生成元组列表中唯一的第一个值:

set(x[0] for x in mylist)
如果要将其返回列表,请执行以下操作:

list(set(x[0] for x in mylist))
尝试:

说明:
将问题分为几个阶段:

  • 从嵌套列表中获取索引0处的所有项:

    first_items_in_nested = (tpl[0] for tpl in mylist)  
    
  • 从步骤1中获取唯一值:

    unique_items = set(first_items_in_nested)
    
  • (可选)将结果转换回列表:

    result = list(unique_items)
    
  • 尝试:

    说明:
    将问题分为几个阶段:

  • 从嵌套列表中获取索引0处的所有项:

    first_items_in_nested = (tpl[0] for tpl in mylist)  
    
  • 从步骤1中获取唯一值:

    unique_items = set(first_items_in_nested)
    
  • (可选)将结果转换回列表:

    result = list(unique_items)
    

  • 设置(mylist)
    应该正确吗?!我不确定我是否理解这个问题。您能否为您的示例提供预期的输出?您希望使用['nr'、'fm']还是[(u'nr'、'string')、(u'fm'、'string')]?重复数据消除时的值重要吗?已更新和clarified@KeerthanaPrabhakaran不,这不起作用
    set(mylist)
    应该正确吗?!我不确定我是否理解这个问题。您能否为您的示例提供预期的输出?您希望使用['nr'、'fm']还是[(u'nr'、'string')、(u'fm'、'string')]?重复数据消除时的值重要吗?已更新和clarified@KeerthanaPrabhakaran不,那不行。首先,我需要事先知道列表中包含的内容。我不。(我可以设法设置,但不愿意)。另外,我不需要集合,我只需要每个元组的第一个值。而且只有独一无二的答案。更新答案以回应您的预期问题否。首先,我需要事先知道列表中包含的内容。我不。(我可以设法设置,但不愿意)。另外,我不需要集合,我只需要每个元组的第一个值。而且只有独一无二的。更新答案以回应您的预期问题