如何获取字符串在 Python 列表中的位置?
How do I get the position of a string inside a list in Python?
我想在 Python 的列表中获取字符串的位置?我应该怎么做?
比如用户说一句“你好,我叫Dolfinwu”,我事先把整句话做成一个列表,想在这里得到每个“o”的位置,怎么办做吗?在这种情况下,第一个“o”的位置是“4”,第二个“o”的位置是“18”。但是很明显,用户会使用不同的词输入不同的句子,那么在这种不可预测的情况下,如何才能获得特定字符串值的位置呢?
我试过下面这段代码。我知道它包含语法错误,但我想不出更好的方法。
sentence = input('Please type a sentence: ')
space = ' '
for space in sentence:
if space in sentence:
space_position = sentence[space]
print(space_position)
你的这段代码有点乱
space = ' ' # This is fine but redundant
for space in sentence: # Shouldn't reuse variable names. Should be <for char in sentence. But I wouldn't use that either since we don't need the char it self but the index
if space in sentence: #Should be <if char == space> Your just returns a True if there's a single space anywhere in the string. Assuming it's using the correct variable named space. Honestly I don't know what this code will do since I didn't run it :P
space_position = sentence[space]
print(space_position)
这是我会做的,因为我也是初学者,可以做得更好。
sentence = input('Please type a sentence: ')
for i in range(len(sentence)):
if sentence[i] == " ":
print(i)
#>>>Please type a sentence: A sentence and spaces
#>>>1
#>>>10
#>>>14
这个怎么样?我昨天想出了另一个解决方案。
import re
sentence = input('Please type a sentence: ')
print([match.span()[0] for match in re.finditer(' ', sentence)])
你的第一个代码中也有一个错误,其中 space_position = sentence[space] 行使用了一个字符串,如果我是正确的话,作为行中列表的索引,我认为应该是一个整数。所以我会做的是
sentence = input('Please type a sentence: ') # Asks the user to input the sentence
space_position = [] # The list where the list indexes of the spaces will be stored (can work for any other symbol)
for i in range(len(sentence)): # Scans every single index of the list
if sentence[i] == " ": # Determines whether if the value of the specific list index is the chosen string value
# (The value could be swapped for another one aside from strings)
print(i) # Print out the index where the value is the chosen string value
其余费用与@pudup 发布的一样
P.S。我也是初学者,所以我的代码可能不是最好的解决方案或有语法错误,如果它们不起作用或效率不高,请道歉。
我想在 Python 的列表中获取字符串的位置?我应该怎么做?
比如用户说一句“你好,我叫Dolfinwu”,我事先把整句话做成一个列表,想在这里得到每个“o”的位置,怎么办做吗?在这种情况下,第一个“o”的位置是“4”,第二个“o”的位置是“18”。但是很明显,用户会使用不同的词输入不同的句子,那么在这种不可预测的情况下,如何才能获得特定字符串值的位置呢?
我试过下面这段代码。我知道它包含语法错误,但我想不出更好的方法。
sentence = input('Please type a sentence: ')
space = ' '
for space in sentence:
if space in sentence:
space_position = sentence[space]
print(space_position)
你的这段代码有点乱
space = ' ' # This is fine but redundant
for space in sentence: # Shouldn't reuse variable names. Should be <for char in sentence. But I wouldn't use that either since we don't need the char it self but the index
if space in sentence: #Should be <if char == space> Your just returns a True if there's a single space anywhere in the string. Assuming it's using the correct variable named space. Honestly I don't know what this code will do since I didn't run it :P
space_position = sentence[space]
print(space_position)
这是我会做的,因为我也是初学者,可以做得更好。
sentence = input('Please type a sentence: ')
for i in range(len(sentence)):
if sentence[i] == " ":
print(i)
#>>>Please type a sentence: A sentence and spaces
#>>>1
#>>>10
#>>>14
这个怎么样?我昨天想出了另一个解决方案。
import re
sentence = input('Please type a sentence: ')
print([match.span()[0] for match in re.finditer(' ', sentence)])
你的第一个代码中也有一个错误,其中 space_position = sentence[space] 行使用了一个字符串,如果我是正确的话,作为行中列表的索引,我认为应该是一个整数。所以我会做的是
sentence = input('Please type a sentence: ') # Asks the user to input the sentence
space_position = [] # The list where the list indexes of the spaces will be stored (can work for any other symbol)
for i in range(len(sentence)): # Scans every single index of the list
if sentence[i] == " ": # Determines whether if the value of the specific list index is the chosen string value
# (The value could be swapped for another one aside from strings)
print(i) # Print out the index where the value is the chosen string value
其余费用与@pudup 发布的一样
P.S。我也是初学者,所以我的代码可能不是最好的解决方案或有语法错误,如果它们不起作用或效率不高,请道歉。