Namedtuple class
Import Module
from Import Namedtuple
Method of Use and description
#pycharm Ctrl-click Collections to view the source code
# Collections Data Structure #__all__ = [' deque ', ' defaultdict ', ' namedtuple ', ' userdict ', ' userlist ', ' userstring ', ' Counter ', ' ordereddict ', ' Chainmap ']
Instructions for use:
#猜包功能
Name = ("Lijie", ' ll ') user = ("AA", 23,189, ' Boddy ')username,age,height,edu = userprint (username,age,height, Edu
Printing results:
AA 189 Boddy
#另一种方法 username,*other = userprint (username,other)
Printing results:
AA [at 189, ' Boddy ']
A tuple can be a key to a dictionary, and list is not, example:
Name_tuple = ("Test", 22,185, "Baskerball") name_list = ["Test1", 22,188, "baseball"]DD = {}dd[name_tuple] = ' Boddy ' Print ( DD) Dd[name_list] = ' body ' Print (DD)
Printing results:
{(' Test ', 185, ' Baskerball '): ' Boddy '}
Traceback (most recent):
File "d:/python-script/collections_module/chapter1/collection_module.py", line +, in <module>
Dd[name_list] = ' body '
typeerror:unhashable Type: ' List'
Python Collections Data Structure module