Open hug2wisdom opened 4 years ago
To loop over two or more sequences at the same time, the entries can be paired with the zip()
function.
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
print('What is your {0}? It is {1}.'.format(q, a))
# result
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like
append()
andextend()
.可以作为键的是 string number ,tuple 的话,里面只能包括:string number tuple,不能包含可以变的。
list(keys)
,sorted(keys)
默认的是对键的操作dictionary comprehensions 列表推导式(解析式)