파이썬 for 반복문 질문있습니다 ㅜㅜ
글자 수 세기 문제를 풀고 있는데요
출력했을 때
text = []
sentence = input()
sentence = sentence.upper()
for a in sentence:
text.append(a)
for b in text:
if ord(b) == 32:
text.remove(b)
for c in text:
print('"',c ,'"',":",text.count(c))
hello world; Algorithm world!! " H " : 2 " E " : 1 " L " : 5 " L " : 5 " O " : 4 " W " : 2 " O " : 4 " R " : 3 " L " : 5 " D " : 2 " ; " : 1 " A " : 1 " L " : 5 " G " : 1 " O " : 4 " R " : 3 " I " : 1 " T " : 1 " H " : 2 " M " : 1 " W " : 2 " O " : 4 " R " : 3 " L " : 5 " D " : 2 " ! " : 2 " ! " : 2
같은 문자가 이렇게 중복돼서 나오는데 중복되는 건 어떻게 없애나요...?? ㅠㅠ
답변 2개
중복되지않는 값만 새로운 배열에 넣으세요
text = [] newArr = [] sentence = input()
sentence = sentence.upper()
for a in sentence: text.append(a)
for b in text:
if ord(b) == 32: text.remove(b) elif b not in newArr : newArr.append(b)
for c in newArr: print('"',c ,'"',":",text.count(c))
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인