一次更换一个列表中的项目

时间:2016-04-12 06:16:46

标签: python list python-2.7 function

我正在尝试为我正在考虑的python类写一个“填空”。我只是刚刚开始,我遇到了一些我不太了解的事情。

以下是主要代码块:

easy = ['In ','___(1)___',' if you want to pass the W3 validator, make sure you close your ','___(2)___','!']

easy_list = ['HTML','tags']
blank_list = ['___(1)___','___(2)___','___(3)___','___(4)___','___(5)___','___(6)___']

def num_replace(level,word):
    new_level = []
    for i in level:
        if i == blank_list[word]:
            new_level[word] = easy_list[word]
        else:
            new_level.append(i)
    return new_level

我尝试将此功能输出为如下的简单打印:

print ''.join(num_replace(easy,0))
print ''.join(num_replace(easy,1))

但我得到的是:

HTML if you want to pass the W3 validator, make sure you close your ___(2)___!

然后......

In tags if you want to pass the W3 validator, make sure you close your !

所以我尝试输出它:

happy_list = ''.join(num_replace(easy,0))
print happy_list
happy_list = ''.join(num_replace(easy,1))
print happy_list

但我得到同样的东西。

澄清我想要得到的是:

在致电num_replace之前:In ___(1)___ if you want to pass the W3 validator, make sure you close your ___(2)___!

num_replace(easy,0)应输出:In HTML if you want to pass the W3 validator, make sure you close your ___(2)___!

num_replace(简单,1):In HTML if you want to pass the W3 validator, make sure you close your tags!

4 个答案:

答案 0 :(得分:1)

这里有两个问题:

1 - 您在word位置附加新单词。 new_level[word] = append(easy_list[word]) 2 - 您没有将更改保存到新字符串,因为字符串不可变。 所以这是改变:

>>> def num_replace(level, word):
        new_level = []
        for i in level:
            if i == blank_list[word]:
                new_level.append(easy_list[word])
            else:
                new_level.append(i)
        return new_level

>>> 
>>> 
>>> ''.join(num_replace(easy,0))
'In HTML if you want to pass the W3 validator, make sure you close your ___(2)___!'
>>> ''.join(num_replace(easy,1))
'In ___(1)___ if you want to pass the W3 validator, make sure you close your tags!'
>>> 

现在,您必须以这种方式保存您所做的更改:

>>> l = num_replace(easy,0)
>>> l
['In ', 'HTML', ' if you want to pass the W3 validator, make sure you close your ', '___(2)___', '!']
>>> 
>>> ''.join(num_replace(l,1))
'In HTML if you want to pass the W3 validator, make sure you close your tags!'
>>> 

或者如果你想要它在一行:

>>> ''.join(num_replace(num_replace(easy,0),1))
'In HTML if you want to pass the W3 validator, make sure you close your tags!'

答案 1 :(得分:0)

关键是你试图从一个函数内部修改一个全局变量,同时返回一个新的单词列表。

如果仔细查看循环,您会看到不匹配的空白条目为.append(..)。因此,您也应该使用替换词。

我认为你的循环应该是这样的:

for i in level:
        if i == blank_list[word]:
            new_level.append( easy_list[word] )
        else:
            new_level.append(i)

也许更好

replacements = { '__(1)__': 'HTML', '__(2)__': 'tags' }

def replace(wordlist, subs):
   res = []
   for word in wordlist:
      res.append( subs[word] if word in subs else word )
   return res

答案 2 :(得分:0)

问题是,当(num_replace(easy,0))。您在此步骤中覆盖了new_level的0th位置new_level[word] = easy_list[word]所以您没有获得In

第1步:

enter image description here

第2步:

enter image description here

new_level [0]被HTML替换

将此更改为

if i == blank_list[word]:
            new_level.append( easy_list[word] )

答案 3 :(得分:0)

这是我的建议:

easy = ['In ','___(1)___',' if you want to pass the W3 validator, make sure you close your ','___(2)___','!']

easy_list = ['HTML','tags']
blank_list = ['___(1)___','___(2)___','___(3)___','___(4)___','___(5)___','___(6)___']

def num_replace(level, replacement_list, *words):
    new_level = level[:]
    for word in words:
        level_pos = level.index('___(%d)___' % (word+1))
        new_level[level_pos] = replacement_list[word]

    return new_level

print ''.join(num_replace(easy,easy_list,0,1))

所以我们将位置(0,1)的元组传递给* words。我也在传递替换列表(而不是使用全局)以获得灵活性。

但如果你只是使用字符串而不是列表,那会更简单。

easy = 'In ___(1)___ if you want to pass the W3 validator, make sure you close your ___(2)___!'

easy_list = ['HTML','tags']

def num_replace(level, replacement_list, *words):
    new_level = level
    for word in words:
        new_level = new_level.replace('___(%d)___' % (word+1), replacement_list[word])

    return new_level

print num_replace(easy,easy_list,0,1)

当然,你在这里为每个“单词”创建一个新的字符串对象,但是涉及到的卷(2)那么这几乎不值得担心。