이 프로그램은 파이썬의 기본 엔코더이며 정의 된 변수의 이름을 변경하지 않고도 더 효율적으로 만들 수 있는지 알고 싶습니다. 누군가 제안 해 줄 수 있습니까?이 프로그램을보다 효율적으로 만들 수있는 방법
def encode(pattern, filename):
f = open(filename, "rt")
contents = f.read()
f.close()
printNow(contents)
changes = pattern.split("|")
for str in changes:
printNow("Change "+ str[0] + " to " + str[1])
newMsg = ""
for char in contents:
for change in changes:
if char == change [0]:
char = change[1]
newMsg += char
f = open(filename + "encoded", "wt")
f.write(newMsg)
f.close()
f = open(filename + "encoded", "rt")
printNow(f.read())
f.close()
encode("ae|ga|s3", "C:\\Users\\Shaun\\Desktop\\Test.txt")
당신이해야한다고 생각하는만큼 성과가 좋지 않은 곳. – Jordan
보다 효율적으로 무엇을 의미합니까? 좀 더 구체적이어야합니다. Efficient의 정의는 사람마다 다른데, 실행 시간, 메모리 소비 또는 출력입니까 ?? – Zeina