代码
def substring(word,f,l):
word_length = len(word)
l1 = str(word.index(f))
l2 = str(word.index(l))
a = word[int(l1):int(l2)+1]
sub_list = []
for i in l1:
for j in l2:
sub_list.append(a)
return sub_list
w1 = input("Enter String: ")
w2 = input("Enter start: ")
w3 = input("Enter end: ")
print(substring(w1,w2,w3))
输入
python programming # string
p # start
g # end
输出
['python prog', 'python prog']
预期产量
['python prog', 'prog', 'programming']
我该怎么做才能根据用户输入获取字符串中的所有子字符串?
答案 0 :(得分:0)
您可以使用正则表达式查找字符串中所有出现的开始和结束符号,并使用列表推导构造答案:
s = input("Enter String: ")
head = input("Enter start: ")
tail = input("Enter end: ")
result = [s[i.start():j.start() + 1]
for i in re.finditer(head, s) for j in re.finditer(tail, s)
if i.start() < j.start()]
print(result)
输出
['python prog', 'prog', 'programming', 'python programming']
答案 1 :(得分:0)
def substring(line, start, end):
for i in (start, end):
if i not in line:
raise Exception(f"`{i}` not found in `{line}`!")
start_indexes, end_indexes, sub_list = [], [], []
line_length, end_length = len(line), len(end)
for i in range(len(line)):
if line[i:].startswith(start):
start_indexes.append(i)
elif line[i:].startswith(end):
end_indexes.append(i)
for i in start_indexes:
for j in end_indexes:
right = j + end_length
if i < right and right-i < line_length:
sub_list.append(line[i : right])
return sub_list
输出:
['python prog', 'prog', 'programming']
答案 2 :(得分:0)
.index()
仅给出第一个索引(而.rindex()
给出最后的索引),您需要所有索引,所以enumerate
这样做:
def substring(word,f,l):
word_length = len(word)
l1 = [i for i,v in enumerate(word) if v==f]
l2 = [i for i,v in enumerate(word) if v==l]
sub_list = []
for i in l1:
for j in l2:
sub_list.append(word[i:j+1])
return sub_list
w1 = input("Enter String: ")
w2 = input("Enter start: ")
w3 = input("Enter end: ")
print(substring(w1,w2,w3))
输出:
Enter String: python programming
Enter start: p
Enter end: g
['python prog', 'python programming', 'prog', 'programming']
答案 3 :(得分:0)
使用re
模块(explanation of regexp here)的另一尝试:
import re
from itertools import chain
string = 'python programming' # string
start = 'p' # start
end = 'g' # end
groups = re.finditer(rf'(?=({start}.*{end}))(?=({start}.*?{end}))', string)
s = set(chain.from_iterable([(g.start(1), g.end(1), g[1]), (g.start(2), g.end(2), g[2])] for g in groups))
print([i[2] for i in s])
打印:
['prog', 'python prog', 'programming', 'python programming']