Python - 字符串列表中单词的开始和结束索引
给定一个字符串,我们的任务是编写一个Python程序,从字符串提取另一个列表的单词的所有元素的开始和结束索引。
Input : test_str = “gfg is best for all CS geeks and engineering job seekers”, check_list = [“geeks”, “engineering”, “best”, “gfg”]
Output : {‘geeks’: [23, 27], ‘engineering’: [33, 43], ‘best’: [7, 10], ‘gfg’: [0, 2]}
Explanation : “geeks” starts from index number 23 till 27, hence the result.
Input : test_str = “gfg is best for all CS geeks and engineering job seekers”, check_list = [“geeks”, “gfg”]
Output : {‘geeks’: [23, 27], ‘gfg’: [0, 2]}
Explanation : “geeks” starts from index number 23 till 27, hence the result.
方法 #1:使用循环+ index() + len()
在这里,循环用于从列表中获取每个元素。 index() 获取初始索引, len() 获取字符串list 中所有元素的最后一个索引。
Python3
# Python3 code to demonstrate working of
# Start and End Indices of words from list in String
# Using loop + index() + len()
# initializing string
test_str = "gfg is best for all CS geeks and engineering job seekers"
# printing original string
print("The original string is : " + str(test_str))
# initializing check_list
check_list = ["geeks", "engineering", "best", "gfg"]
res = dict()
for ele in check_list :
if ele in test_str:
# getting front index
strt = test_str.index(ele)
# getting ending index
res[ele] = [strt, strt + len(ele) - 1]
# printing result
print("Required extracted indices : " + str(res))
Python3
# Python3 code to demonstrate working of
# Start and End Indices of words from list in String
# Using dictionary comprehension + len() + index()
# initializing string
test_str = "gfg is best for all CS geeks and engineering job seekers"
# printing original string
print("The original string is : " + str(test_str))
# initializing check_list
check_list = ["geeks", "engineering", "best", "gfg"]
# Dictionary comprehension to be used as shorthand for
# forming result Dictionary
res = {key: [test_str.index(key), test_str.index(key) + len(key) - 1]
for key in check_list if key in test_str}
# printing result
print("Required extracted indices : " + str(res))
输出:
The original string is : gfg is best for all CS geeks and engineering job seekers
Required extracted indices : {‘geeks’: [23, 27], ‘engineering’: [33, 43], ‘best’: [7, 10], ‘gfg’: [0, 2]}
方法 #2:使用字典理解+ len() + index()
在这里,我们执行类似于上述函数的任务,但结果字典的构建是使用字典理解使用速记完成的。
蟒蛇3
# Python3 code to demonstrate working of
# Start and End Indices of words from list in String
# Using dictionary comprehension + len() + index()
# initializing string
test_str = "gfg is best for all CS geeks and engineering job seekers"
# printing original string
print("The original string is : " + str(test_str))
# initializing check_list
check_list = ["geeks", "engineering", "best", "gfg"]
# Dictionary comprehension to be used as shorthand for
# forming result Dictionary
res = {key: [test_str.index(key), test_str.index(key) + len(key) - 1]
for key in check_list if key in test_str}
# printing result
print("Required extracted indices : " + str(res))
输出:
The original string is : gfg is best for all CS geeks and engineering job seekers
Required extracted indices : {‘geeks’: [23, 27], ‘engineering’: [33, 43], ‘best’: [7, 10], ‘gfg’: [0, 2]}