我有一个包含字符串列表的输入文件.
我从第二行开始迭代每四行.
从这些行中的每一行开始,我从第一个和最后6个字符创建一个新字符串,并且仅当新字符串是唯一的时才将其放在输出文件中.
我写的代码可以实现这一点,但是我正在使用非常大的深度排序文件,并且已经运行了一天并且没有取得多大进展.所以我正在寻找任何建议,如果可能的话,这样做会更快.谢谢.
def method():
target = open(output_file, 'w')
with open(input_file, 'r') as f:
lineCharsList = []
for line in f:
#Make string from first and last 6 characters of a line
lineChars = line[0:6]+line[145:151]
if not (lineChars in lineCharsList):
lineCharsList.append(lineChars)
target.write(lineChars + '\n') #If string is unique, write to output file
for skip in range(3): #Used to step through four lines at a time
try:
check = line #Check for additional lines in file
next(f)
except StopIteration:
break …
Run Code Online (Sandbox Code Playgroud) 我偶然发现了这个采访问题:
给出字典顺序中的元素列表(即['a','b','c','d']),找到第n个排列
我自己试了一下,花了大约30分钟才解决.(我最终在Python中使用了一个~8-9行解决方案).只是好奇 - 解决这类问题需要多长时间?我花了太长时间吗?