Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Unique Values - Fatal编程技术网

Python 从列表中的列表中提取唯一字符串值

Python 从列表中的列表中提取唯一字符串值,python,list,unique-values,Python,List,Unique Values,所以我有一个.csv文件,里面有一堆电影标题,其中一列是Netflix上的列表,它是一个列表,所以它有多个列表,它看起来像下面的电子表格: 我已经设法提取了我需要的各个列,所以现在我的主要问题是如何简化类型列表,以便不添加重复 我有以下代码: enter code ef getUniqueGenres(genrelist): uniqueGenres = [] for titles in genrelist: for listing in titles if not l

所以我有一个.csv文件,里面有一堆电影标题,其中一列是Netflix上的列表,它是一个列表,所以它有多个列表,它看起来像下面的电子表格:

我已经设法提取了我需要的各个列,所以现在我的主要问题是如何简化类型列表,以便不添加重复

我有以下代码:

enter code ef getUniqueGenres(genrelist):
uniqueGenres = []
for titles in genrelist:
    for listing in titles
        if not listing in uniqueGenres:
            uniqueGenres.append(listing)
return uniqueGenreshere
我希望代码能够遍历每个标题的每个列表,看看它是否是一种独特的类型。如果是,它将存储它,否则它将忽略它

澄清: data colum中列出的_是一个列表,其中每个索引都是自己的列表。因此,唯一值必须考虑所有这些因素。我试过这样做:

uniqueGenres = []
for titles in netflixgenre:
    for listing in titles:
        if not listing in uniqueGenres:
            uniqueGenres.append(listing)

但这也没有给我所需的输出

您只需使用set即可。以下是一个例子:

mylist = ['a', 'b', 'a', 'a', 'c', 'd', 'e']
unique_list = list(set(mylist))
对于列表中的列表

import itertools
mylist = [['a', 'b', 'a', 'a', 'c', 'd', 'e'], ['c','a','d']]
// combine multiple list to single list using itertools
final_list = list(itertools.chain.from_iterable(mylist))
unique_list = list(set(final_list))

这实际上是来自和的重复问题。尽管如此,普遍共识如下:

只需将角色转换为一组并返回到列表是最简单的方法。这是因为集合中的所有项都必须是唯一的,否则不会添加它们

unique_list=list(set(list)).sort()

我试过了,但没用。它只是打印了我所有的数据。我的listen_in set是一个列表,每个索引都是它自己的列表,因此我想知道如何获取列表中每个列表的唯一值。您可以展示列表示例吗?我是说它看起来怎么样。这是当我直接打印包含listingsI have edit my answer的我提取的列时,命令提示符上显示的输出。检查。当我这样做时,它现在将单词分解为字符,因此每个索引现在是一个字母而不是一个单词