操作列表、查找索引、python
我应该从单词列表中找出一个项目的索引。 职能:操作列表、查找索引、python,python,indexing,find,Python,Indexing,Find,我应该从单词列表中找出一个项目的索引。 职能: def index(lst_words, word): 应返回lst\u words中的word索引。 e、 g 应该返回0 为什么这个不适合我 def index (lst_words, word): find = lst_words.index(word) return find 你可能是说 ['how','to','find'].索引('how') 不是 ['how','to','find'].索引(['how']) 这不
def index(lst_words, word):
应返回lst\u words
中的word
索引。
e、 g
应该返回0
为什么这个不适合我
def index (lst_words, word):
find = lst_words.index(word)
return find
你可能是说
['how','to','find'].索引('how')
不是
['how','to','find'].索引(['how'])
这不是在搜索字符串,而是在搜索列表。它会匹配的
['how','to','find',['how']]索引(['how'])
你可能是说
['how','to','find'].索引('how')
不是
['how','to','find'].索引(['how'])
这不是在搜索字符串,而是在搜索列表。它会匹配的
['how','to','find',['how']]索引(['how'])
这可能就是你的意思。当您想查找条
的位置时,可以将条
作为字符串参数传递,而不是列表。因为您拥有的列表是一个字符串列表
区别在于:
>>> x = ['bar']
>>> type(x)
<type 'list'>
>>> x = 'bar'
>>> type(x)
<type 'str'>
这可能就是你的意思。当您想查找条
的位置时,可以将条
作为字符串参数传递,而不是列表。因为您拥有的列表是一个字符串列表
区别在于:
>>> x = ['bar']
>>> type(x)
<type 'list'>
>>> x = 'bar'
>>> type(x)
<type 'str'>
我很抱歉,但是没有,结果完全像索引(['how','to','find'],['how'])。@Gusto:结果应该是0,这就是这个代码返回的结果。也许如果你说
[[how'],'to','find']。索引(['how'])
会工作并返回0
,OP会更好地理解。对不起,没有,结果完全像索引(['how','to','find'],['how'])。@Gusto:结果应该是0,这就是这个代码返回的结果。也许如果你说了[['how'],'to','find']。索引(['how'])
会工作并返回0
,OP会更好地理解。
>>> x = ['bar']
>>> type(x)
<type 'list'>
>>> x = 'bar'
>>> type(x)
<type 'str'>
>>> x = ['hello', 'foo', ['bar']]
>>> index(x, ['bar']) # since bar is a list not a string
2