a = 'ab'
list_test = ['a', 'b', 'ab'] # list_test = ['a', 'b']
for item in list_test:
....if a == item:
........return list_test.index(item)
else:
....for item in list_test:
........if a[0] == item:
............return list_test.index(item)
在 list_test 中有 ab 时,返回 ab 的位置,在 list_test 中没有 ab 时,返回 a 的位置,这里循环了两遍,有方法一遍循环搞定么?
list_test = ['a', 'b', 'ab'] # list_test = ['a', 'b']
for item in list_test:
....if a == item:
........return list_test.index(item)
else:
....for item in list_test:
........if a[0] == item:
............return list_test.index(item)
在 list_test 中有 ab 时,返回 ab 的位置,在 list_test 中没有 ab 时,返回 a 的位置,这里循环了两遍,有方法一遍循环搞定么?