【发布时间】:2015-10-31 03:50:44
【问题描述】:
有什么方法可以获取映射函数的列表的索引?我的地图几乎可以工作了,但我能够访问我的 long_words 列表中的特定项目
# full_chunk is a very long string of plaintext (eg:pages from a book)
# long_words is a list of words for which I wish to substitute other things
# works
for x in xrange(len(long_words)):
full_chunk = full_chunk.replace(long_words[x],str(x))
# doesn't work :(
subber = lambda a,b,c: a.replace(b,c)
map(subber(full_chunk,long_words[long_words.index(x)],long_words.index(x)),long_words)
目前,我只想能够用long_words 列表中的该单词的索引替换出现在full_chunk 中的long_words 的每个单词的每次出现。例如:
# example input
long_words = ['programming','pantaloons']
full_chunk = 'While programming, I prefer to wear my most vividly-hued pantaloons.'
# desired output (which is what the for loop gives me)
print(full_chunk)
'While 0, I prefer to wear my most vividly-hued 1.'
如果我需要提供更多信息,请告诉我,并提前感谢您的帮助!
【问题讨论】:
标签: python string dictionary replace