목록의 독해력에 대해 알고 있는데, 사전에 대한 이해는 어떨까요?파이썬에 사전 보 완성이 있습니까? (함수가 dict을 반환하는 문제)
예상 출력 :
>>> countChar('google')
{'e': 1, 'g': 2, 'l': 1, 'o': 2}
>>> countLetters('apple')
{'a': 1, 'e': 1, 'l': 1, 'p': 2}
>>> countLetters('')
{}
코드 (I 초보자이야) :
def countChar(word):
l = []
#get a list from word
for c in word: l.append(c)
sortedList = sorted(l)
uniqueSet = set(sortedList)
return {item:word.count(item) for item in uniqueSet }
이 코드의 문제는 무엇입니까? 왜 이걸 SyntaxError
에 받습니까?
return { item:word.count(item) for item in uniqueSet }
^
SyntaxError: invalid syntax
구문 오류가')'추가입니다 : –
corrected.but 여전히 문제가있어'word.count (항목))' – newbie
Y 수 실제 오류가 발생합니다. – SingleNegationElimination