为什么从上一个元素分配的列表中求出字典?

时间:2018-09-05 06:39:38

标签: python list dictionary

>>> from string import ascii_lowercase
>>> from random import shuffle
>>> shuffle(alp)
>>> alp
['n', 'c', 'q', 'l', 's', 't', 'w', 'a', 'o', 'r', 'g', 'u', 'd', 'm', 'k', 
 'p', 'x', 'v', 'z', 'y', 'j', 'h', 'i', 'f', 'e', 'b']
>>> ck = {x:alp.pop() for x in ascii_lowercase}
>>> ck
{'a': 'b', 'b': 'e', 'c': 'f', 'd': 'i', 'e': 'h', 'f': 'j', 'g': 'y', 'h': 
 'z', 'i': 'v', 'j': 'x', 'k': 'p', 'l': 'k', 'm': 'm', 'n': 'd', 'o': 'u', 
 'p': 'g', 'q': 'r', 'r': 'o', 's': 'a', 't': 'w', 'u': 't', 'v': 's', 'w': 
 'l', 'x': 'q', 'y': 'c', 'z': 'n'}

什么构成,为什么从alp的最后一个列表元素分配给ck的alp列表?就像“ a:b b:e ...”

1 个答案:

答案 0 :(得分:0)

因为list.pop()返回并删除列表的最后一个元素。如果要返回并删除第一个元素,可以使用list.pop(0)

有关其他信息,请参见文档:https://docs.python.org/3/tutorial/datastructures.html

相关问题