Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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/1/list/4.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/blackberry/2.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_List - Fatal编程技术网

Python 更改子列表中的字符

Python 更改子列表中的字符,python,list,Python,List,我已经知道这是一个非常愚蠢的问题。我试着查找我的答案,但我几乎不知道该问什么。抱歉,如果标题有点模糊。但我来了。我有一张单子。我想除掉名单上的坏角色 List = ["I?", "Can", "!Not", "Do.", "It"] BadChars = ["?", "!", "."] for word in List: for char in word: if char in BadChars: char = "" print(List)

我已经知道这是一个非常愚蠢的问题。我试着查找我的答案,但我几乎不知道该问什么。抱歉,如果标题有点模糊。但我来了。我有一张单子。我想除掉名单上的坏角色

List = ["I?", "Can", "!Not", "Do.", "It"]
BadChars = ["?", "!", "."]

for word in List:
    for char in word:
        if char in BadChars:
            char = ""

print(List)
再说一遍,我知道这很简单,但我就是搞不懂。原谅我

编辑:顺便说一句,它没有给我一个错误。它只是将列表原封不动地返回。

对于每个单词的每个字符,您可以使用替换方法:

List = ["I?", "Can", "!Not", "Do.", "It"]
BadChars = ["?", "!", "."]

for i in range(len(List)):
  for char in BadChars:
    List[i] = List[i].replace(char, "")

print(List) # ==> ['I', 'Can', 'Not', 'Do', 'It']
也可以使用正则表达式:

import re

List = ["I?", "Can", "!Not", "Do.", "It"]
BadChars = ["?", "!", "."]
rgx = '[%s]'%(''.join(BadChars))

List = [re.sub(rgx, "", word) for word in List]

print(List) # ==> ['I', 'Can', 'Not', 'Do', 'It']

您可以使用一个生成器表达式,该表达式迭代字符串中的每个字符,并保留不在BadChars中的字符:

这将返回:

['I', 'Can', 'Not', 'Do', 'It']

只需使用strip方法从列表中删除坏蛋

可能重复的@davedwards即可。我感谢您的评论。大约20分钟前我已经看过这篇文章了,它对我没有任何帮助。你就快到了!问题是,如果变量char在BadChars列表中,则将其设置为,并保持单词不变。您真正想要的是将word中的字符设置为如果它在BadChars中,则可以使用诸如word.replace?之类的方法来完成,这很有效!非常感谢你。我真的很感激。很高兴它起到了作用。@davedwards是的,我已经投了赞成票,我会在冷静下来后接受这个答案。
['I', 'Can', 'Not', 'Do', 'It']
List = ["I?", "Can", "!Not", "Do.", "It"]
l=[]
BadChars = ["?", "!", "."]
for i in List:
    for j in BadChars:
        if j in i:
            i=i.strip(j)
    l.append(i)


print(l)