【问题标题】:Replace every word after index k in list替换列表中索引 k 之后的每个单词
【发布时间】:2016-10-21 23:54:36
【问题描述】:

如何替换列表中 index[3] 之后的每个单词?

例如,我需要将第一个单词更改为“How's”,将第二个单词更改为“it”,将第三个单词更改为“going?”。然后,我需要将 index[3] 之后的每个单词都更改为“yo”:

input = "My name is bla bla bla?" 

output = "How's it going? Yo! Yo! Yo!"

这是我目前所拥有的:

def hellohello(input):
    if type(input) != str:
        return "Input has to be string"
    else:
        new_input = input.split(' ')
        if len(input) <= 3:
            return "How's it going?"
        else:
            new_input[0] = "How's "
            new_input[1] = "it "
            new_input[2] = "going? "
            new_input[3:] = ["Yo! "]
            output = ''.join(new_input)
            return output

print hellohello("why is this not printing more yo")

到目前为止我只得到:

How's it going? Yo!

【问题讨论】:

  • new_input[3:] = ["Yo! "] 不会将“从 3 开始的每个值”分配给字符串 "Yo! ",它只是用单个条目替换从 3 开始的所有条目,而不是您需要创建new_input[3:] = ["Yo! "] * (len(new_input)-3) 或类似的重复条目。
  • 如果您认为它可以解决您的问题,请accept 回答。它将在整个社区中识别正确的解决方案。这可以通过单击答案旁边的绿色复选标记来完成。请参阅此image 以供参考。干杯。

标签: python list python-2.7 replace


【解决方案1】:

叫我懒惰,但我可能会先创建一个长度为len(new_input) 的列表,其中包含'Yo!',然后换入["How's", "it", "going?"] ...

>>> s = "My name is bla bla bla?"
>>> new_input = s.split()
>>> output = ['Yo!'] * len(new_input)
>>> output[:3] = ["How's", "it", "going?"]
>>> output
["How's", 'it', 'going?', 'Yo!', 'Yo!', 'Yo!']

你的代码的问题是这一行:

new_input[3:] = ["Yo! "]

您正在用单个元素替换多个元素。您可能可以通过执行 new_input[3:] = ["Yo! "] * (len(new_input) - 3) 来修复代码,这将创建一个与您要替换的子列表长度相同的 "Yo! " 列表。


你可能已经注意到我使用了s.split() 而不是s.split(' ')s.split()s.split(None) 相同,它在连续运行的空格(包括换行符和制表符)上拆分。基本上,
'foo bar\tbaz'.split() 会导致['foo', 'bar', 'baz'],而
'foo bar\tbaz'.split(' ') 会导致['foo', '', 'bar\tbaz']

【讨论】:

  • 如果您能解释一下split()split(' ') 之间的区别,将会很有帮助。虽然是一个简单的问题,但许多用户并不知道这一点。
  • @BhargavRao -- 当然,添加了一个脚注,因为它有点与帖子其余部分的目的相切。
【解决方案2】:

试试这个:

def hellohello(input):
    if type(input) != str:
        return "Input has to be string"
    else:
        new_input = input.split(' ')
        if len(input) <= 3:
            return "How's it going?"
        else:
            new_input[0] = "How's"
            new_input[1] = "it"
            new_input[2] = "going?"
            for currentIndex in xrange(3, len(new_input)):
                new_input[currentIndex] = "Yo!"
            output = ' '.join(new_input)
            return output

print hellohello("why is this not printing more yo")
print hellohello("why is this")
print hellohello("why is this yep")

输出:

How's it going? Yo! Yo! Yo! Yo!
How's it going?
How's it going? Yo!

通过执行new_input[3:] = ["Yo! "],您只需将所有字符串标记的子数组(在由split 分隔的数组中)从索引 3 替换为单个字符串“Yo!”,而不是替换每个数组项目本身。基本上,您引用了数组的整个切片 (3:) 而不仅仅是单个项目。

[编辑:基于正确注释的更新解决方案,即不需要使用切片和索引演算,还添加了对之前问题的解释]

【讨论】:

  • 你知道你可以只做xrange(3, len(new_input)) 而不是切片然后每次将 3 添加到索引中。还请添加对您更改的内容以及更改原因的说明,否则这将无助于操作人员了解问题所在/下次如何自行修复。
  • 我根据@TadhgMcDonald-Jensen 的正确评论更新了解决方案,即不需要使用切片和索引演算。还添加了对之前出现问题的解释。不知道为什么投反对票 - 这符合 OP 的要求。
  • 我想@ShubhamSharma 可能被否决了,因为在没有 cmets 或解释的情况下发布一个只有 1-4 行更改的整个代码块很少有帮助。现在有了一个解释,它不再适合那个了。
  • 是的,但现在我删除了我的反对票@TadhgMcDonald-Jensen
  • 非常感谢!!
猜你喜欢
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 2015-05-18
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多