Python 如何在数组中找到重复字符串
我对Python完全陌生,所以如果我使用了错误的术语,请纠正我 我有一个大约100万部电影的数据集,包括它们的名字、收视率、类型等。 我已经设法创建了一个二维数组,现在只保存流派和评级。 当然,电影可以有多种类型。在数据集中,它们用逗号分隔,因此数组如下所示:Python 如何在数组中找到重复字符串,python,python-3.x,Python,Python 3.x,我对Python完全陌生,所以如果我使用了错误的术语,请纠正我 我有一个大约100万部电影的数据集,包括它们的名字、收视率、类型等。 我已经设法创建了一个二维数组,现在只保存流派和评级。 当然,电影可以有多种类型。在数据集中,它们用逗号分隔,因此数组如下所示: seperateGenres = [[],[]] def multipleGenresFound(combinedGenre, rating)) # Split combined genre and put in new arr
seperateGenres = [[],[]]
def multipleGenresFound(combinedGenre, rating))
# Split combined genre and put in new array
singleGenres = combinedGenre.split(',')
for seperatedSingleGenre in singleGenres
if seperatedSingleGenre not in seperateGenres:
# Append if not in array
seperateGenres.append([seperatedSingleGenre, Rating])
else:
# If genre exists => add rating
searchedIndex = seperateGenres.index(combinedGenre)
seperateGenres[searchedIndex][1] + rating
seperateGenres = [[],[]]
def multipleGenresFound(combinedGenre, rating))
# Split combined genre and put in new array
singleGenres = combinedGenre.split(',')
for seperatedSingleGenre in singleGenres
if seperatedSingleGenre not in seperateGenres:
# Append if not in array
seperateGenres.append([seperatedSingleGenre, Rating])
else:
# If genre exists => add rating
searchedIndex = seperateGenres.index(combinedGenre)
seperateGenres[searchedIndex][1] + rating
如果代码中有任何输入错误:我只需从手机图片中手动重新输入即可
代码始终将流派添加到单独的流派数组中。它从不在else语句中运行代码
我是一名C#程序员,通常只需创建一个列表
,使用Linq循环并将评级添加到列表中。(至少在不为列表创建新对象的情况下是这样的)我相信在Python中也有类似的方法来创建它。
那是什么呢?combinedGenre实际上是什么样子的。根据您的代码,它看起来像字符串而不是数组。您还应该向函数中添加类型提示。您正在将字符串与列表进行比较,这就是为什么您总是在if语句中结束,而不是在else语句中结束。还有,你为什么要把combinedGenre附加到separategnes后面?看来off@It_is_Chris是的,它是一根线。我循环遍历代码中另一个位置的数组,并将multipleGenresFound函数作为参数。组合起来的电影可以是这样的:“动作、浪漫、冒险”和这样的评级:5Best shot可能是一部以流派为关键,以浮动值列表为价值的名言。在“|”处拆分单线,将右侧转换为浮动。左侧拆分为“”,并检查您的dict中是否有所有拆分的键(如果它已在dict中)。如果没有,请创建键,添加一个列表作为值,并使用浮点填充。如果存在,将浮点添加到该键的值列表中。我们建议您在此处发布一个real,以便我们了解您正在处理的内容—包括部分数据以及您如何调用它。您可以根据dict计算出中间值/平均值。