我有一个关于python中的字典的问题,在参考页面:http://docs.python.org/tutorial/datastructures.html#dictionaries。追加到Python的字典键值:
“你不能使用列表作为密钥,因为链表可以 代替使用索引分配,切片分配或方法进行修饰等 追加()和extend()”。
但是,追加python字典的值是什么意思?
# A simple program that keep track of the line number(s) a word appeared in the novel
myDictionary = {}
with open('novel.txt') as book:
lineNumber = 1
for line in book:
# cleaned is a helper function to clean up the line
for word in cleaned(line).split():
if word in myDictinoary:
myDictionary[word].append(lineNumber)
else:
myDiciontary[word]=[lineNumber]
lineNumber += 1
myDictionary[word].append(lineNumber)
究竟发生了什么?在我看来,列表中,我想确定,一个键的价值可以列表(或在这种情况下,它是一个元组)? 什么样的数据类型可以用作值?当我想要密钥存储多个值时。
关于无法使用列表作为字典*键*的引用与您的文章的其余部分无关,即修改字典*值*。您不能使用可变对象(包括列表)作为关键字,因为更改关键字会破坏字典查找。你可以使用任何你想要的价值。 – 2012-03-18 05:20:19
是的,我问的原因是因为我没有看到它隐含地告诉我“任何物体都可以用作价值”。它只是说什么不能用于钥匙,现在我想我明白了。 – George 2012-03-18 05:27:35