如图,字典增加项目不就是dict[letter]=word 这个样的输入吗,问什么把word的中括号去掉,就会显示错误呢?words = ['apple', 'bat', 'bar', 'atom', 'book']dict={}for word in words: letter=word[0] if letter not in dict: dict[letter]=[word] else: dict[letter].append(word)
因为使用了append()方法,相当于需要指定字典A中A[letter]对应的值是列表。所以如果不加[]的话,是直接对字符串使用append(),会报错。未明学院python课程助你入门数据分析和数据挖掘。