dictionary = {
'item_1': '...',
'item_2': '1',
'item_3': 2,
}
dictionary['item_1'] # you can get item_1 from dictionary like this
# or this
item_name = 'item_1'
dictionary[item_name]
对于列表,可以使用索引获取其中的项存储
obj = object()
li = [1, '2', obj, ]
# slicing operation in list
li[0] # get the 1st item.
li[-1] # get the last item.
li[1:3] # get item from index 1 to 3 not include index 1.
li[:] # get all item. it can use to copy the list.
对于dictionary,它意味着从dictionary中获取元素
对于列表,可以使用索引获取其中的项存储
你需要访问字典才能使用它,对吗?方括号表示move的值与字典中的另一个值相关联。代码的意思是,如果字典中与字典中move值关联的值是一个空格,那么执行一些代码。在你的余生中,你会看到这个方括号符号,所以我真的会钻这个
看起来你在看this tic-tac-toe game
正如您所提到的,
theBoard
是一个字典,这里是它的初始条件:In Python dictionaries have 'keys' and 'values'(点击链接了解更多信息)。在这种情况下,键是数字1-9,并且至少在开始时,它们都等于
' '
将
[key]
添加到字典的名称后,将提取该键的值例如:
在您的示例中
move
是键,用于检查值是否为' '
如上所述,类似的语法贯穿整个Python。值得回顾一下data structures documentation以巩固概念
相关问题 更多 >
编程相关推荐