程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning (4): multidimensional list & tuple & Dictionary & sequence unpacking

編輯:Python

Python Learn to share today :

Python What to learn today : Multidimensional list & Tuples & Dictionaries & Sequence unpacking

The notes are in the notes :

# Multidimensional list 
# 2 d list 
# One dimensional lists can help us store one dimension , Linear data 
# A two-dimensional list can help us store two-dimensional data , Table data 
a = [[10, 20, 30], [30, 40, 60]]
# The result is :20
print(a[0][1])
for x in range(2):
for n in range(3):
print(a[x][n],end="\t")
print()
# Tuples tuple
# Lists are variable sequences , You can modify any element in the list . Tuples belong to immutable sequences , Elements in tuples cannot be modified . therefore , Tuples do not add elements , Modifying elements , How to delete elements 
# therefore , We just need to learn the creation and deletion of tuples , The access and count of elements in tuples can . Tuples support the following operations :1. Index access 2. Slicing operation 3. Connection operation 
# 4. Membership operations 5. Comparison operation 6. Count : Tuple length len(), Maximum max(), minimum value min(), Sum up sun() etc. 
#
# Tuple creation :
# 1. adopt () Creating a tuple . Parentheses can be omitted 
# If the tuple has only one element , Must be followed by a comma . This is because the interpreter will put (1) Interpreted as an integer 1 (1,) Interpreted as tuples 
a = (10, 20, 30)
# The result is :<class 'tuple'>
print(type(a))
# 2. adopt tuple() Creating a tuple tuple( Objects that can be iterated )
b = tuple(range(3))
# The result is :(0, 1, 2)
print(b)
b = tuple([2, 3, 5])
# The result is :<class 'tuple'>
print(type(b))
# tuple() Can receive list , character string , Other serial models , Iterators, etc. generate tuples .
# list() Can receive tuples , character string , Other sequence types , Iterators, etc. generate lists 
# Element access and counting of tuples 
c = (1, 2)
d = (3, 4)
# The result of printing is :(1, 2, 3, 4)
print(c+d)
# The element access of tuples is the same as that of lists , It just returns the tuple object 
a = (1, 2, 4, 10, 100)
# The result of printing is :2
print(a[1])
# List about sorting methods list.sorted() Is to modify the original list object , Tuples do not have this method . If you want to sort tuples , Only built-in functions can be used sorted(tplobj), And generate a new list object 
# The result of printing is :[1, 2, 4, 10, 100]
print(sorted(a))
# zip
# zip( list 1, list 2,....) Combine the elements at the corresponding positions of multiple lists into tuples , And return this zip object .
a = [10, 20]
b = [30, 40]
c = [50, 70]
d = zip(a, b, c)
print(list(d))
# The generator deduces to create tuples 
s = (x*2 for x in range(5))
# The result is :(0, 2, 4, 6, 8)
print(tuple(s))
# The result is :()
# Only exception elements can be accessed , The second time is empty . It needs to be regenerated once 
print(tuple(s))
# Tuple summary 
# 1. The core feature of tuples is : Immutable sequence 
# 2. Tuples are accessed and processed faster than lists 
# 3. Like integers and strings , Tuples can be used as dictionary keys , The list can never be used as a dictionary key 
#
#
# Dictionaries 
# The dictionary is “ Key value pair ” Random variable sequence of , Every element in the dictionary is a “ Key value pair ”, contain :“ Key object ” and “ The value object ”.
# Can pass “ Key object ” Achieve fast access , Delete , Update the corresponding “ The value object ”.
# A typical dictionary definition :
a = {
'name': 'gao', "age": 18}
# The result of printing is :{'name': 'gao', 'age': 18}
print(a)
# Dictionary creation 
# 1. We can go through {},dict() To create a dictionary object .
b = dict(name="xs", age=18)
# The result of printing is : {'name': 'xs', 'age': 18}
print(b)
# Empty dictionary object 
c = {
}
d = dict()
# 2. adopt zip() Create a dictionary object 
k = ['name', 'age']
v = ['xs', 15]
d = dict(zip(k, v))
# The result of printing is :{'name': 'xs', 'age': 15}
print(d)
# 3. adopt fromkeys Create a dictionary with an empty value 
e = dict.fromkeys(['name', 'age'])
# The result is :{'name': None, 'age': None}
print(e)
# Access to dictionary elements 
# 1. adopt [ key ] get “ value ”. If the key doesn't exist , Throw an exception 
a = {
'name': 'gao', "age": 18}
# The result is :gao
# print(a["name"])
# 2. adopt get() Methods to get “ value ”. Recommended . Advantage is : The specified key does not exist , return node; You can also set the object returned by default when the specified key does not exist . Recommended get() obtain “ The value object ”
# The result is :gao
# print(a.get("name"))
# The result is :None non-existent 
print(a.get("123"), " non-existent ")
# 3. List all key value pairs 
# The result is : dict_items([('name', 'gao'), ('age', 18)])
print(a.items())
# 4. List all the keys , List all the values 
# The result is :dict_keys(['name', 'age'])
print(a.keys())
# The result of printing is :dict_values(['gao', 18])
print(a.values())
# 5.len() Key value to number 
# The result of printing is :2
print(a.__len__())
# 6. Test one “ key " Is it in the dictionary 
# The result of printing is :True
print("name" in a)
# Dictionary elements add , modify , Delete 
a = {
'name': 'gao', "age": 18}
# 1. Add to dictionary “ Key value pair ”. If “ key ” Already exist , Then the old key value pair is overridden ; If “ The key doesn't exist ”, Then add “ Key value pair ”
# The result is :{'name': 'gao', 'age': 17, 'cs': '123'}
a["cs"] = "123"
a['age'] = 17
print(a)
# 2. Use upate() Add all key value pairs in the new dictionary to the old dictionary , If key Repeat , Then directly cover 
b = {
"name": "da", "age": 1, "sex": " male "}
a.update(b)
print(a)
# 3. The deletion of elements in the dictionary , have access to del() Method ; perhaps clear() Delete all key value pairs ;pop() Delete the specified key value pair , And return the corresponding “ The value object ”
del(a['name'])
# The result of printing is :{'age': 1, 'cs': '123', 'sex': ' male '}
print(a)
a.pop("cs")
# The result of printing is :{'age': 1, 'sex': ' male '}
print(a)
a.clear()
# The result of printing is :{}
print(a)
# 4.popitem(): Returns and deletes the last key value pair 
a = {
'name': 'gao', "age": 18, "ces": 22}
a.popitem()
print(a)
a.popitem()
print(a)
# Sequence unpacking 
# Sequence unpacking can be used for tuples , list , Dictionaries , Sequence unpacking allows us to easily assign values to multiple variables 
x, y, z = (20, 30, 10)
# The result is :20
print(x)
# 30
print(y)
# 10
print(z)
# When sequence unpacking is used for Dictionary , The default is right “ key ” To operate ; If you need to operate on key value pairs , You need to use items(); If necessary “ value ” To operate , You need to use values();
a = {
'name': 'gao', "age": 18, "ces": 22}
# The default is to operate on the key 
name, age, ces = a
# The result of printing is :
# name
# age
# ces
print(name)
print(age)
print(ces)
name, age, ces = a.values()
# The result of printing is :
# gao
# 18
# 22
print(name)
print(age)
print(ces)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved