我是python的新手。有人可以解释我如何操纵这样的字符串吗?
此功能接收三个输入:
'-'
)也没有空格。应返回的输出是:
基本上,我正在使用定义:
def generate_next_fmla (complete_fmla, partial_fmla, symbol):
我将它们变成列表吗?然后追加?
另外,我应该找出complete_fmla
中符号的索引号,以便我知道在带有连字符的字符串中将其附加到哪里?
答案 0 :(得分:1)
这是一个简单的在线功能
def generate_next_fmla(base, filt, char):
return ''.join( c if c==char else filt[idx] for idx,c in enumerate(base) )
核心思想是if else子句:
c if c==char else filt[idx]
,给定每个字符及其在原始字符串中的位置,如果等于所选字符,则将其放入新字符串中,否则将该值放入过滤字符串
以更详细的方式编写,如下所示:
def generate_next_fmla (complete_fmla, partial_fmla, symbol):
chars = ""
for idx in range(len(complete_fmla)):
c = complete_fmla[idx]
if c==symbol:
chars = chars + c
else:
chars = chars + partial_fmla[idx]
return chars
这是相同的功能写在更多的几行(它实际上效率较低,因为字符串的太阳是一个坏习惯)
答案 1 :(得分:0)
问候你可以尝试使用正则表达式,我认为它们会帮助你实现你想要达到的目标,在这里你有link
该文档中的一些例子:
>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'
答案 2 :(得分:0)
毫无疑问,您需要添加边缘情况,但这应该适用于简单的情况:
def generate_next_fmla (complete_fmla, partial_fmla, symbol):
result = partial_fmla.strip().split(' ')[:]
for index, c in enumerate(complete_fmla):
if c == symbol:
result[index] = c
return "".join(result)
它会转换为一个列表并再次返回,以便更改它。
编辑:我现在意识到这与EnricoGiampieri的回答类似,但是有了清单。
答案 3 :(得分:0)
作为python的初学者,可能最好的方法是将您的要求1:1映射到您的代码 - 我希望以下内容尽可能不言自明:
def generate_next_fmla (complete_fmla, partial_fmla, symbol):
# test that complete_fmla does not contain '-'
if '-' in complete_fmla:
raise ValueError("comple_fmla contains '-'")
# delete all spaces from partial_fmla if needed (this need was suggested
# in the original question with some examples that contained spaces)
partial_fmla = partial_fmla.replace(' ', '')
# test if it is possible to test the "same positions" from both strings
if len(complete_fmla) != len(partial_fmla):
raise ValueError("complete_fmla does not have the same lenght as partial_fmla")
# add other input checking as needed ...
if symbol not in complete_fmla or symbol in partial_fmla:
return partial_fmla
# partial_fmla[i] = symbol wouldn't work in python
# because strings are immutable, but it is possible to do this:
# partial_fmla = partial_fmla[:i] + symbol + partial_fmla[i+1:]
# and another approach is to start with an empty string
result = ''
for i in range(len(partial_fmla)):
# for every character position in the formulas
# if there is '-' in this position in the partial formula
# and the symbol in this position in the complete formula
if partial_fmla[i] == '-' and complete_fmla[i] == symbol:
# then append the symbol to the result
result += symbol
else:
# otherwise use the character from this positon in the partial formula
result += partial_fmla[i]
return result
print(generate_next_fmla ('abcdeeaa', '--------', 'd')) # ‘---d----’
print(generate_next_fmla ('abcdeeaa', '- - - x - - - - ', 'e')) # ‘---xee--’
print(generate_next_fmla ('abcdeeaa', 'x-------', 'a')) # ‘x-----aa’
print(generate_next_fmla ('abcdeeaa', 'x-----', 'a')) # Exception
答案 4 :(得分:0)
您可以查看以下代码:
lst_fmla = []
def generate_next_fmla(s_str, fmla, c_char):
i = 0
for s in s_str:
if c_char is s: lst_fmla.append(c_char)
elif fmla[i] != '-': lst_fmla.append(fmla[i])
else: lst_fmla.append('-')
i = i + 1
print(''.join(lst_fmla))
generate_next_fmla('abcdeeaa', '---d----', 'e')
例如,如果函数generate_next_fmla中的第二个参数类似于'---- d ---',其中'd'将是第三个参数('e')的相同索引,它将被替换用'e'。