3 回答

TA贡献1866条经验 获得超5个赞
你想要索引x吗?在这种情况下,使用x.index(o)where 搜索oin x。
x = 'Poisson geometry plays an important role in noncommutative geometry.'
y = 'u'
i = 0
while i == 0:
for o in x:
if o == y:
print(y, "is first seen in index = ", x.index(o))
i += 1
然而,正确的写法是没有循环:
x = 'Poisson geometry plays an important role in noncommutative geometry.'
y = 'u'
print(y, "is first seen in index = ", x.index(y))
输出:
u is first seen in index = 51

TA贡献1797条经验 获得超6个赞
你需要迭代吗?如果您保留前 2 行然后使用,您将在不使用,或 的情况下x.index(y)获得相同的结果。whileforif
x = 'Poisson geometry plays an important role in noncommutative geometry.'
y = 'u'
print(y, "is first seen in index = ", x.index(y))
添加回答
举报