Python中的list()可以将字典转换为列表。
>>> abbreviation ={'WAN':'Wide Area Network', 'CU':'Control Unit', 'LAN':'Local Area Network', 'GUI':'Graphical User Interface'}
>>> list(abbreviation) #默认将键转化为列表
['WAN', 'CU', 'LAN', 'GUI']
>>> list(abbreviation.keys())
['WAN', 'CU', 'LAN', 'GUI']
>>> list(abbreviation.values())
['Wide Area Network', 'Control Unit', 'Local Area Network', 'Graphical User Interface']
>>> list(abbreviation.items())
[('WAN', 'Wide Area Network'), ('CU', 'Control Unit'), ('LAN', 'Local Area Network'), ('GUI', 'Graphical User Interface')]
Python中的tuple()可以将字典转换为元组。
>>> abbreviation ={'WAN':'Wide Area Network', 'CU':'Control Unit', 'LAN':'Local Area Network', 'GUI':'Graphical User Interface'}
>>> tuple(abbreviation) #默认将键转化为元组
('WAN', 'CU', 'LAN', 'GUI')
>>> tuple(abbreviation.keys())
('WAN', 'CU', 'LAN', 'GUI')
>>> tuple(abbreviation.values())
('Wide Area Network', 'Control Unit', 'Local Area Network', 'Graphical User Interface')
>>> tuple(abbreviation.items())
(('WAN', 'Wide Area Network'), ('CU', 'Control Unit'), ('LAN', 'Local Area Network'), ('GUI', 'Graphical User Interface'))
Python中的tuple()函数可以接受一个列表,并返回一个包含同样元素的元组。从结果上看,tuple()函数冻结了列表。
>>> vehicle = ['train', 'bus', 'car', 'ship', 'subway', 'bicycle']
>>> tuple(vehicle)
('train', 'bus', 'car', 'ship', 'subway', 'bicycle')
Python中的list()函数接受一个元组,并返回一个包含同样元素的列表。从结果上看, list()函数融化了元组。
>>> vehicle=('train','bus','car','ship','subway','bicycle')
>>> list(vehicle)
['train', 'bus', 'car', 'ship', 'subway', 'bicycle']