一个 Python 循环问题

2016-12-27 15:58:56 +08:00
 coolair
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 的位置,这里循环了两遍,有方法一遍循环搞定么?
1443 次点击
所在节点    问与答
17 条回复
lln133208
2016-12-27 16:09:24 +08:00
如果‘ ab ’和‘ a ’都没有呢?
imn1
2016-12-27 16:12:17 +08:00
1.用 in 判断
2.用 enumerate()返回 index 更方便
imn1
2016-12-27 16:17:19 +08:00
另外,主贴的逻辑是不是错了? if 在循环内,还不止一次呢
判断后再循环啊,那无论怎么最多也是只有一次循环吧
weyou
2016-12-27 16:19:03 +08:00
你那个是循环了 3 次, index 隐含了一次循环。

foundPos = -1
for pos, item in enumerate(list_test):
if a == item:
return pos
elif a[0] == item:
foundPos = pos
return foundPos

都没找到的情况返回-1
weyou
2016-12-27 16:20:11 +08:00
怎么代码都乱了
coolair
2016-12-27 16:35:13 +08:00
@imn1 可能没表述清楚,意思是遍历一遍列表看下 a 在不在里面,在就结束了,如果不在,再遍历一遍看 a[0],在不在。

@weyou 这个可以,厉害啊!多谢。
Kilerd
2016-12-27 16:38:25 +08:00
4 楼的估计是最好的答案了。
CoX
2016-12-27 16:46:01 +08:00
if a in list_test:
return list_test.index(a)
if a[0] in list_test:
return list_test.index(a[0])
return -1
为啥要循环呢?
ipwx
2016-12-27 17:01:36 +08:00
我是来搞笑的:

findpos = (lambda items, s1, s2: (lambda f: ([i for (l, i) in sorted((x for x in (f(i, v) for (i, v) in enumerate(items)) if x))] + [-1])[0])(lambda i, v: (0, i) if v == s1 else ((1, i) if v == s2 else None)))

print(findpos(['a', 'b', 'ab', 'ab'], 'ab', 'a'))
print(findpos(['a', 'a', 'b'], 'ab', 'a'))
print(findpos(['b'], 'ab', 'a'))
kimchan
2016-12-27 17:16:42 +08:00
@Kilerd 确定 4 楼的答案是正确的吗.....题主的要求是, "ab"不在数组中时, 才去查"a"是否在数组中, 但是 4 楼的答案. 如果"ab" 在数组的后边, 那么会直接返回"a"的 index. 我认为遍历肯定是需要两次遍历了. 不过代码的质量上可以大大的改进.
我的想法是和 8 楼一样的.
kimchan
2016-12-27 17:17:04 +08:00
@coolair 还是说我理解错了需求?
kimchan
2016-12-27 17:21:14 +08:00
@Kilerd 测试了一下. 先 a.sort().reverse()之后可以把"a"排到"ab"的后边 这时候就可以直接套用 4 楼了 @coolair
ipwx
2016-12-27 17:26:24 +08:00
@kimchan 。。。四楼的答案当然是对的,因为 return foundPos 在 for 循环体外。

话说你们没人关注一下我的代码吗?它是对的,且只遍历了一遍。
kimchan
2016-12-27 17:32:37 +08:00
@ipwx 擦, 我太年轻, 看错了. 以为 for 里边 2 个都是直接 return...你那行代码太长了哈哈哈. 看着头疼.
CoX
2016-12-27 17:42:03 +08:00
@ipwx 四楼的方法 当 list_test = ['a', 'b', 'a'] 时返回的是最后一个 a 的位置;当然 lz 也没说清这种情况下返回哪个。
Kilerd
2016-12-27 18:02:27 +08:00
@kimchan

四楼答案格式化

def
····foundPos = -1
····for pos, item in enumerate(list_test):
········if a == item:
············return pos
········elif a[0] == item:
············foundPos = pos
····return foundPos



@CoX 这个也不难啊, 可以用一个 list 记住所有位置,返回收个就好了。


@ipwx

你这样用 lambda 是要被捉去枪毙的 或者 拿去给杨教授电疗一下也可以。 ( 手动滑稽
weyou
2016-12-27 18:09:05 +08:00
@CoX 要返回首个 a[0],加个条件限制即可:

def
····foundPos = -1
····for pos, item in enumerate(list_test):
········if a == item:
············return pos
········elif a[0] == item and foundPos == -1:
············foundPos = pos
····return foundPos

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/330510

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX