【发布时间】:2021-08-02 16:33:16
【问题描述】:
我已将 csv 文件转换为列表列表(每一行都是一个列表),我正在尝试查看该行的索引 x(我的脚本中的“2”)处的元素是否重复在任何其他行。如果它是重复的,我需要检查索引y(我的脚本中的'5')是否也重复。我编写了以下嵌套 for 循环:
def duplicate_twice(list_of_lists):
temp = []
for i in row_list:
for j in row_list:
if row_list[i][2] == row_list[j][2]:
if row_list[i][5] != row_list[j][5]:
diff_part.append(row_list[j])
return diff_part
这在逻辑上对我来说是有道理的,但我遇到了TypeError: list indices must be integers or slices, not list
- 有没有更 Pythonic 的方式来执行我正在努力的目标?
- 我可以改变什么来绕过 TypeError?
【问题讨论】:
-
这是蟒蛇。这里 i 和 j 是列表的元素,而不是索引。考虑使用 pandas 来解决您的问题。 pandas.pydata.org/docs/reference/api/…
标签: python python-3.x error-handling