많은 단어를 사용하여 각 단어를 문자로 분리하고 큰 목록에 첨부하는 루프가 있습니다. 발생루프 실행 시간 단축 및 효율성 향상
list[0]
= 편지 :
그때 가장 발생 문자를 확인하고, 이미 문자열에 표시되지 않는 경우에, 나는 두 개의 공백이 목록에 저장됩니다 이 루프는 매우 비효율적이다
발생 횟수를 가장
list[1]
는 =. 작동하지만 값을 반환하는 데 약 25 ~ 30 초가 걸립니다. 그 전에는 계속 될 것이고 어떤 값도 반환하지 않을 것입니다.
내가 작성한 코드의 효율성을 높이려면 어떻게해야합니까?
def choose_letter(words, pattern):
list_of_letters = []
first_letter = [] # first spot is the letter, second is how many times it appears
second_letter =[] # first spot is letter, second how many times it appears
max_appearances = ["letter", 0]
for i in range(len(words)): # splits up every word into letters
list_of_letters.append(list(words[i]))
list_of_letters = sum(list_of_letters, []) # concatenates the lists within the list
first_letter = list_of_letters.count(0)
for j in list_of_letters:
second_letter = list_of_letters.count(j)
if second_letter >= max_appearances[1] and j not in pattern:
max_appearances[0] = j
max_appearances[1] = second_letter
else:
list_of_letters.remove(j)
return max_appearances[0]
이것은 http : //codereview.stackexchange.com/ – Blorgbeard
의 더 나은 후보가 될 수 있으며 codereview로 이동하면이 코드에 대해 실행 한 프로필러의 출력을 확인하게됩니다. –
['collections.Counter'] (https://docs.python.org/3.5/library/collections.html#collections.Counter)에 대한 작업처럼 보입니다. – bereal