Python 试图从列表中删除数字字符,字符将不会删除,我不知道为什么
我编写了一个程序,从命令行上的一个文件中获取输入,然后它将接受来自该文件的输入作为变量。最后3行的一个问题是,我无法从列表中删除字符“1”、“2”和“3”,因为即使我编写了一个函数来删除字符,它仍然无法工作 我得到的输出是['1bbb\n','2aab\n','3abbba\n'] 我需要的输出是['bb b','aab','abbb bb'] 有人能帮我弄清楚吗Python 试图从列表中删除数字字符,字符将不会删除,我不知道为什么,python,Python,我编写了一个程序,从命令行上的一个文件中获取输入,然后它将接受来自该文件的输入作为变量。最后3行的一个问题是,我无法从列表中删除字符“1”、“2”和“3”,因为即使我编写了一个函数来删除字符,它仍然无法工作 我得到的输出是['1bbb\n','2aab\n','3abbba\n'] 我需要的输出是['bb b','aab','abbb bb'] 有人能帮我弄清楚吗 import sys import re #The input of the file needs to take the maxi
import sys
import re
#The input of the file needs to take the maximum size of the queue
#The input needs to choose a maximum number of states or depth
#The input needs to choose the set of dominoes
maxQueueSize = 0
maxStates = 0
outPutToken = 0;
numberOfDominoes = 0
dominoesFile = 0
def remove(list):
pattern =r'[0-9]\n'
list = [re.sub(pattern, '', i) for i in list]
return list
def input_words(file_name):
f = open(file_name, 'r')
f = f.readlines()
j = 0
maxQueueSize = f[0]
maxStates = f[1]
outPutToken = bool(f[2])
numberOfDominoes = f[3]
dominoesFile = f[4: 7]
for i in dominoesFile:
dominesFile = remove(dominoesFile)
#dominoesFile[j] = i.split()
j+=1
print(dominoesFile)
if __name__ == '__main__':
input_words(sys.argv[1])
# print(maxQueueSize)
else :
print("Please enter a file!")
class Domino:
def __init__(self, top, bottom):
self.top = ""
self.bottom = ""
试试这个:
list = ['1 bb b\n', '2 a aab\n', '3 abbba bb\n']
list = [i.strip('0123456789\n \t') for i in list]
print(list)
在本文中,我们提供了要从字符串中剥离的字符
上述各项的产出:
['bb b', 'a aab', 'abbba bb']
但是您的正则表达式是错误的,模式[0-9]\n说,查找单个出现的数字字符后跟换行符这是我从您的解决方案中得到的输出:['b','ab','bba bb']。因此请尝试[I.strip for I in list]它不起作用。不过还是要谢谢你的时间。@MATT_BOOTY,你介意详细说明一下吗?它抛出了一个错误吗?还是输出不正确?我已经在上面的答案中添加了我的输出,它符合您在问题中的要求好吧,你们得到的输出与我的不同,我不知道为什么。请分享您得到的输出得到了,它是固定的。
['bb b', 'a aab', 'abbba bb']