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))
添加回答
舉報