所以基本上,我需要遍历s,s1和s2中X的每个列表,并确定第一个值,例如。 FOX并在前三个字符串[0,1,2]中替换为'MONKEY'(大写)。我可以使用所有其他字符串,但是这些字符串在算法中会引起混淆。这是因为在字符串X的位置中'fox'和'monkey'的位置以及位置的变化,就像.find()忽略了它一样。
X = [ ["The fox chattered the dim monkey's ears off!", 'FOX' , 'MoNkEy' ],
["The fox chattered the dim monkey's ears off!", 'MoNkEy', 'FOX' ],
["The monkey chattered the dim fox's ears off!", 'FOX' , 'MoNkEy' ],
["Silly monkey chattered dim fox's ears off!" , 'siLLy' , 'dIm' ]]
def swap_strs(s, s1, s2):
if s1.upper() == '' or s1.upper() not in s.upper():
return "s1 NO GOOD"
if s2.upper() == '' or s2.upper() not in s.upper():
return "s2 NO GOOD"
l1, l2 = len(s1), len(s2)
slower = s.lower()
p1, p2 = slower.find(s1.lower()), slower.find(s2.lower())
s1 = s1.upper()
s2 = s2.upper()
target = s[:p1] + s2 + s[p1+len(s1):p2] +s1
return target
def Q1():
for s, s1, s2 in X:
print(s, '\n', swap_strs(s, s1, s2))
Q1()
当前我得到的代码是这个,有什么建议吗?
Q1()
The fox chattered the dim monkey's ears off!
The MONKEY chattered the dim FOX
The fox chattered the dim monkey's ears off!
The fox chattered the dim FOXMONKEY
The monkey chattered the dim fox's ears off!
The monkey chattered the dim MONKEYFOX
Silly monkey chattered dim fox's ears off!
DIM monkey chattered SILLY
所需的输出:
Q1()
The fox chattered the dim monkey's ears off!
The MONKEY chattered the dim FOX's ears off!
The fox chattered the dim monkey's ears off!
The MONKEY chattered the dim FOX's ears off!
The monkey chattered the dim fox's ears off!
The FOX chattered the dim MONKEY's ears off!
Silly monkey chattered dim fox's ears off!
DIM monkey chattered SILLY fox's ears off!
答案 0 :(得分:0)
您要实现的目标是通过 replace()方法实现的。您应该使用它,除非分配任务的人告诉您。
X = [ ["The fox chattered the dim monkey's ears off!", 'FOX' , 'MoNkEy' ],
["The fox chattered the dim monkey's ears off!", 'MoNkEy', 'FOX' ],
["The monkey chattered the dim fox's ears off!", 'FOX' , 'MoNkEy' ],
["Silly monkey chattered dim fox's ears off!" , 'siLLy' , 'dIm' ]]
def swap_strs(s, s1, s2):
if s1.upper() == '' or s1.upper() not in s.upper():
return "s1 NO GOOD"
if s2.upper() == '' or s2.upper() not in s.upper():
return "s2 NO GOOD"
return s.lower().replace(s1.lower(), s2.lower())
def Q1():
for s, s1, s2 in X:
print(s, '\n', swap_strs(s, s1, s2))
Q1()
答案 1 :(得分:0)
您当前的方法似乎有点棘手:我敢肯定一个人可以使它起作用,但是似乎很难正确地处理所有细节。而且直接使用replace()
也不能解决这个问题。
我从编程中获得的最佳建议是创建智能数据结构,以使您的算法变得愚蠢。这是这个想法的说明:
TESTS = [
["The fox chattered the dim monkey's ears off!", 'FOX' , 'MoNkEy' ],
["The fox chattered the dim monkey's ears off!", 'MoNkEy', 'FOX' ],
["The monkey chattered the dim fox's ears off!", 'FOX' , 'MoNkEy' ],
["Silly monkey chattered dim fox's ears off!" , 'siLLy' , 'dIm' ]
]
def swap_strs(s, r1, r2):
# Normalized strings.
lows = s.lower()
low1 = r1.lower()
low2 = r2.lower()
# Create a pair of (POS, OLD, NEW) tuples.
replacements = [
(lows.find(low1), low1, r2.upper()),
(lows.find(low2), low2, r1.upper()),
]
# Sort on POS, reverse order so that we make replacements
# starting at end of string.
replacements.sort(reverse = True)
# Now the replacement logic (the algorithmic part) is very simple.
for p, old, new in replacements:
s = s[0:p] + new + s[p + len(old):]
return s
def main():
for s, r1, r2 in TESTS:
res = swap_strs(s, r1, r2)
print(s)
print(res)
print()
main()