P
P
PARMEZAN12021-07-16 01:54:36
Python
PARMEZAN1, 2021-07-16 01:54:36

How to remove the error RuntimeError: dictionary changed size during iteration?

def top3(st):
st = st.lower()
sp = {}
for i1 in 'qwertyuiopasdfghjklzxcvbnm':
if st.find(i1) != -1:
sp[i1] = st.count(i1)
a = 3
while a > 0:
for i2 in sp:
m = max(sp.values())
if sp[i2] == m:
print(str(i2) + ' - ' + str(sp[i2]))
sp. pop(i2)
print(sp)
a -= 1

top3('hhhhhgggl')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question