python range命令如何与列表一起使用?

时间:2017-06-18 18:52:12

标签: python list

嘿大家我在这里遇到这个问题,我真的无法弄清问题是什么。 我知道范围(2)给出0和1可能的i。 len(R)= 2.并且列表以0开头。

va_arg

错误:

R = [[1,1],[5,5]]
for i in range(len(R)):
    if R[i][0] == R[i][1]:     
        R.remove(R[i])

2 个答案:

答案 0 :(得分:2)

R = [[1,1],[5,5]]
R = [x for x in R if x[0] != x[1]]

它应该是!=而不是==因为他想得到不相同的数字。

答案 1 :(得分:-3)

通常,迭代列表中的项目要容易得多:

Removal_items =[]
for I in R:
  #I is now a variable that represents the object in the list, or I = R[0]...R[n] where n = len(r) -1
  if I[0] == I[1]:
  Removal_items.append(I)

for I in Removal_items:
  R.remove(I)