【发布时间】:2019-10-14 05:12:40
【问题描述】:
我编写了一个程序来在命令行上从文件中获取输入,然后它将文件中的输入作为变量接受。最后 3 行的一个问题是我无法从列表中删除字符 '1' '2' 和 '3',因为即使我编写了一个函数来这样做,它仍然不起作用。
我得到的输出是 ['1 bb b\n', '2 a aab\n', '3 abbba bb\n']
我需要的输出是 ['bb b', 'a aab', 'abbba bb']
谁能帮我解决这个问题?
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 = ""
【问题讨论】:
-
但是你的正则表达式是错误的,模式
[0-9]\n说,“找到单个出现的数字字符后跟换行符”
标签: python