Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带子字符串的列表切片_Python_Python 3.x - Fatal编程技术网

Python 带子字符串的列表切片

Python 带子字符串的列表切片,python,python-3.x,Python,Python 3.x,因此,基本上,我需要遍历s、s1和s2中X的每个列表,并确定第一个值,例如,在前3个字符串[0、1、2]中用'MONKEY'(大写)替换。我可以让所有其他字符串工作,但这些字符串在算法中会混淆。这是因为“fox”和“monkey”在字符串X中的位置以及位置的变化,就像。find()忽略了它 X = [ ["The fox chattered the dim monkey's ears off!", 'FOX' , 'MoNkEy' ], ["The fox chat

因此,基本上,我需要遍历s、s1和s2中X的每个列表,并确定第一个值,例如,在前3个字符串[0、1、2]中用'MONKEY'(大写)替换。我可以让所有其他字符串工作,但这些字符串在算法中会混淆。这是因为“fox”和“monkey”在字符串X中的位置以及位置的变化,就像。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!

您试图实现的是在replace()方法中实现的。你应该使用它,除非分配给你任务的人告诉你不是这样

X=[[“狐狸把那只昏暗的猴子的耳朵抖掉了!”,“狐狸”,“猴子”,
[“狐狸把那只昏暗的猴子的耳朵抖掉了!”,“猴子”,“狐狸”],
[“猴子把昏黄的狐狸的耳朵抖掉了!”,“狐狸”,“猴子”],
[“愚蠢的猴子把昏暗的狐狸的耳朵扯掉了!”,“愚蠢的”,“昏暗的”]
def交换系统(s、s1、s2):
如果s1.upper()==''或s1.upper()不在s.upper()中:
返回“s1不好”
如果s2.upper()==''或s2.upper()不在s.upper()中:
返回“s2不好”
返回s.lower().替换(s1.lower(),s2.lower())
def Q1():
对于X中的s、s1、s2:
打印(s、\n)、交换(s、s1、s2))
Q1()

您试图实现的目标是通过replace()方法实现的。你应该使用它,除非分配给你任务的人告诉你不是这样

X=[[“狐狸把那只昏暗的猴子的耳朵抖掉了!”,“狐狸”,“猴子”,
[“狐狸把那只昏暗的猴子的耳朵抖掉了!”,“猴子”,“狐狸”],
[“猴子把昏黄的狐狸的耳朵抖掉了!”,“狐狸”,“猴子”],
[“愚蠢的猴子把昏暗的狐狸的耳朵扯掉了!”,“愚蠢的”,“昏暗的”]
def交换系统(s、s1、s2):
如果s1.upper()==''或s1.upper()不在s.upper()中:
返回“s1不好”
如果s2.upper()==''或s2.upper()不在s.upper()中:
返回“s2不好”
返回s.lower().替换(s1.lower(),s2.lower())
def Q1():
对于X中的s、s1、s2:
打印(s、\n)、交换(s、s1、s2))
Q1()

你目前的方法似乎有点棘手:我相信一个人可以做到,但似乎很难把所有细节都弄清楚。直接使用
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()

你目前的方法似乎有点棘手:我相信一个人可以做到,但似乎很难把所有细节都弄对。直接使用
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()

我不清楚您想要的输出:也许您可以将其添加到问题中。@FMc您想要的输出是取s的子字符串s1和s2,其中s1[1]和s2[2]的X,并将s2安装到其中s.find(s1)和s1到s.find(s2)的位置。您的问题包括不正确的输出示例。我的建议是添加示例所需输出。@FMc刚刚编辑您的所需输出我不清楚:也许您可以将其添加到问题中。@FMc所需输出是取s的子字符串s1和s2,其中s1[1]和s2[2]的X,并将s2安装到其中s.find(s1)和s1到s.find(s2)的位置您的问题包括示例错误输出。我的建议是添加所需的示例输出。@FMc Just Edited我已经尝试过替换函数,但在替换字符串中的fox和monkey时似乎无法使其工作。@JoelHammel我提供给您的代码不工作?我认为它符合您的要求。我已经尝试过替换函数,但似乎无法在字符串中替换fox和monkey。@JoelHammel我提供给您的代码不起作用?我想它符合你的要求。嗨,非常感谢。这实际上工作得很好,尽管我有一点困惑,那就是replacements.sort(reverse=True)到底是如何工作的。我真的不理解应用程序中的格式。@JoelHammel我不确定我是否理解您的确切问题,但
替换。排序(reverse=True)
只需对元组列表重新排序,以便将较大的
POS
值放在第一位。在排序前后添加
print(replacements)
,查看它的功能。您好,非常感谢。这实际上工作得很好,尽管我有一点困惑,那就是replacements.sort(reverse=True)到底是如何工作的。我真的不理解应用程序中的格式。@JoelHammel我不确定我是否理解您的确切问题,但
替换。排序(reverse=True)
只需对元组列表重新排序,以便将较大的
POS
值放在第一位。在排序之前和之后添加
print(replacements)
,查看它的功能。