Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从输入获取列表中项目的位置(Python)_Python_List - Fatal编程技术网

从输入获取列表中项目的位置(Python)

从输入获取列表中项目的位置(Python),python,list,Python,List,我有一张单子,上面有单词。我想知道这个词在用户要求的句子中的位置。(我正在使用python) 例如,如果我有这样一句话:“你好,世界,你今天过得怎么样?”“世界”出现在第一和第八位。如果用户想知道单词'world'在该句中的位置,它会打印“单词world位于位置1和位置8”。我知道enumerate方法,但无法使其与输入或elif语句一起工作。我想得到任何单词在句子中的位置,无论单词出现多少次。您可以使用正则表达式提取单词,然后在列表理解中使用enumerate()查找单词的索引: >&g

我有一张单子,上面有单词。我想知道这个词在用户要求的句子中的位置。(我正在使用python)
例如,如果我有这样一句话:
“你好,世界,你今天过得怎么样?”
“世界”
出现在第一和第八位。如果用户想知道单词
'world'
在该句中的位置,它会打印
“单词world位于位置1和位置8”
。我知道
enumerate
方法,但无法使其与输入或
elif
语句一起工作。我想得到任何单词在句子中的位置,无论单词出现多少次。

您可以使用正则表达式提取单词,然后在列表理解中使用
enumerate()
查找单词的索引:

>>> import re
>>> s =  "Hello world how are you doing today world?"
>>> word = input("Enter a word: ").lower()
Enter a word: world
>>> [i for i, v in enumerate(re.findall(r'\w+', s)) if v == word]
[1, 7]

使用
re.finditer
enumerate

>>> import re
>>> s='Hello world how are you doing today world?'
>>> word='world'
>>> [i for i, w in enumerate(re.finditer('\w+', s)) if w.group()==word]
[1, 7]

我们(贪婪地)找到由非单词字符分隔的每个字符序列,对其进行迭代,如果它等于目标单词,则存储索引。

在您的句子中,单词
“world”
出现在位置1和7处

> sentence = "Hello world how are you doing today world?"
> word = input("Enter word:  ").lower()
> answer = [i for i, w in enumerate(sentence.lower().split()) if word in w]
> answer
> [1, 7]

无论大小写或标点符号如何,这都有效。

word=input(“输入word:”).strip().lower()
answer=[i为i,w为enouemrate(句子.lower().split())如果w==word]
@inspector4dget-nope,请注意“?”@timgeb:不确定我是不是follow@inspectorG4dget你的代码也是我第一次尝试,但是它在“世界”中找不到“世界”,但是说明书上说它应该找到。这也会打印单词作为子字符串的位置,例如
“世界”
。或者
“超世界”
,这些单词真的应该被计算吗?是的。re解决方案看起来不错,或者如果R.McEvoy不想使用re,@Michael解决方案看起来不错。我使用了你的方法,但它使用了错误的位置。例如,当它应为1时为0。我知道为什么会这样。我尝试在答案中添加1,但这只是导致了一个错误。我想知道您是否知道解决方案?从位置0开始列出。Hello位于“Hello world”中的位置0。我不推荐,但如果有必要,我会把答案改为下面的。答案=[position+1表示位置,如果searchword==word,则枚举中的单词(newsentence.split())
 import re
 s='Hello world how are you doing today world?'
 word='world'
 [i for i, w in enumerate(re.findall('\w+', s)) if w.lower() == word.lower()]
sentence = "Hello world how are you doing today world?".lower()
searchword = input("Enter word:  ").lower()

newsentence = ''

for character in sentence:
    if character.islower() or character == ' ': newsentence += character

answer = [position for position, word in enumerate(newsentence.split()) if searchword == word]

print(answer)