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

[Python] detailed explanation of the use of lists, tuples and dictionaries (addition, deletion and modification)

編輯:Python

Catalog

One 、 list

1) The concept of list

2) List creation

3) Access to the list

4) Add elements to the list

5) List delete elements

6) List modification elements

7) List * and + operation

Two 、 Tuples

1) The concept of tuples

2) Tuple creation

3) Tuple access

4) Addition, deletion and modification of tuples

5) Of a tuple * and + operation

3、 ... and 、 Dictionaries

1) The concept of a dictionary

2) Dictionary creation

3) Access to dictionaries

4) Dictionary add key

5) Dictionary delete key

6) Dictionary modifier


One 、 list

1) The concept of list

         The list is Python Built in Ordered variable sequence , All elements of the list are placed in a pair of brackets “[ ]” in , And use commas to separate .

         When list elements are added or deleted , List objects Automatically expand or shrink memory , Make sure there's no gap between the elements .

         One The data types in the list can be different , Can be integers at the same time 、 The set of real Numbers 、 String and other basic types , Even lists 、 Tuples 、 Dictionaries 、 Collection and other custom types of objects .

2) List creation

  • The following is an example of creating a list , List elements use [ ] Cover up , Each element is separated by a comma .
  • The data types in the list can be different .
List1 = [1, 2, 3, 4, 5]
List2 = ["a", "b", "c", "d", "e"]
List3 = [1.1, 2.2, 3.3, 4.4, 5.5]
List4 = [1, "a", 1.1]

3) Access to the list

  • Use index subscripts to access

example 1.1

List1 = [1, 2, 3, 4, 5]
for i in range(len(List1)):
print(List1[i], end='\t')

Output

  

  • Use elements to iterate through

example 1.2

List1 = [1, 2, 3, 4, 5]
for item in List1:
print(item, end='\t')

Output

   

4) Add elements to the list

  • Tail add

example 1.3

List1 = [1, 2, 3, 4, 5]
List1.append(6)
List1.append(7)
for item in List1:
print(item, end='\t')

Output

  

  • A location (index) add to

example 1.4

List1 = [1, 2, 3, 4, 5]
List1.insert(0, "a") # The index location is 0 Where to add "a"
List1.insert(len(List1) - 1, "b") # The index location is 6 - 1 = 5 Add "b"
for item in List1:
print(item, end='\t')

Output

   

5) List delete elements

  • Delete elements in a certain location

example 1.5

List1 = [1, 2, 3, 4, 5]
List1.pop(0) # Delete first element
List1.pop(len(List1) - 1) # Delete the last element
for item in List1:
print(item, end='\t')

Output

   

  • Delete a known element

example 1.6

List1 = [1, 2, 3, 4, 5]
List1.remove(2) # Remove elements 2
List1.remove(4) # Remove elements 4
for item in List1:
print(item, end='\t')

Output

   

6) List modification elements

  • Modify by direct assignment of index subscript

example 1.7

List1 = [1, 2, 3, 4, 5]
List1[0] = "a" # The first element is changed to a
List1[len(List1) - 1] = "b" # The last element is changed to b
for item in List1:
print(item, end='\t')

Output

  

7) List * and + operation

  • Combination list (+)、 Duplicate list (*)

example 1.8

List1 = [1]
List2 = [2, 3, 4, 5]
List1 = List1 * 5
for item in List1:
print(item, end='\t')
print()
List3 = List1 + List2
for item in List3:
print(item, end='\t')

Output

   

Two 、 Tuples

1) The concept of tuples

         Tuples are similar to lists , But it belongs to Immutable sequence , Once the tuple is created , Its elements cannot be modified in any way .

         Tuples are defined in the same way as lists , But when defining All elements are placed in a pair of parentheses “()” in , Not square brackets .

2) Tuple creation

  • The following is an example of tuple creation , Tuple elements use () Cover up , Each element is separated by a comma .
  • Data types within tuples can be inconsistent .
  • When a tuple contains only one element , You need to add a comma after the element .
tuple1 = (1,)
tuple2 = ("a", "b", "c", "d", "e")
tuple3 = (1.1, 2.2, 3.3, 4.4, 5.5)
tuple4 = (1, "a", 1.1)
  • What data type is it if there is only one element without a comma ?

example 2.1

tuple1 = (1)
tuple2 = ("a")
tuple3 = (1,)
print(type(tuple1), type(tuple2), type(tuple3), sep=" . ") # With . For division Printout 

Output

   

3) Tuple access

  • Use index subscripts to access

example 2.2

tuple1 = (1, 2, 3, 4, 5)
for i in range(len(tuple1)):
print(tuple1[i], end='\t')

Output

   

  • Use elements to iterate through

example 2.3

tuple1 = (1, 2, 3, 4, 5)
for item in tuple1:
print(item, end='\t')

Output

   

4) Addition, deletion and modification of tuples

  Tuples belong to immutable sequences , It can't be modified !!!

5) Of a tuple * and + operation

  • Combinatorial tuple (+)、 Duplicate tuples (*)

example 2.4

tuple1 = (1,)
tuple2 = ("a", "b", "c", "d", "e")
tuple1 = tuple1 * 3
for item in tuple1:
print(item, end='\t')
print()
tuple3 = tuple1 + tuple2
for item in tuple3:
print(item, end='\t')

Output

   

3、 ... and 、 Dictionaries

1) The concept of a dictionary

         The dictionary is Out of order variable sequence , By key 、 The value of , The key must be unique , But values don't have to be .

         When defining a dictionary , Of each element Keys and values are separated by colons , The elements are separated by commas , All the elements are in a pair of braces “{}” in .

         In the dictionary The key can be any immutable data , For example, integer. 、 The set of real Numbers 、 The plural 、 character string 、 Tuples, etc. .

2) Dictionary creation

  • The following is an example of dictionary creation , All elements of the dictionary use { } Cover up .
  • Each element is separated by a comma , The key values of elements are separated by colons .
dict1 = {"name": "admin", "pwd": "123456"}
dict2 = {1: "a", 2: "b"}
dict3 = {1.1: 1, 1.2: 2}

3) Access to dictionaries

  • Accessing values by key

example 3.1

​
dict1 = {"name": "admin", "pwd": "123456", "hobby": ["play", "singing", "dancing"]}
print(dict1["name"])
print(dict1["hobby"])
​

Output

   

  • An error will be reported when accessing a key that does not exist

example 3.2

dict1 = {"name": "admin", "pwd": "123456"}
print(dict1["hobby"])

Output

   

  • You can prompt that there is no such key in the following way

example 3.3

dict1 = {"name": "admin", "pwd": "123456"}
res = dict1.get("name", " There's no such key ")
print(res) # With this key , Print the corresponding value normally
res = dict1.get("hobby", " There's no such key ")
print(res) # There's no such key , Print There's no such key 

Output

  

4) Dictionary add key

  • adopt [ ] Add... Directly

example 3.4

dict1 = {"name": "admin", "pwd": "123456"}
print(dict1)
dict1["hobby"] = ["play", "singing"]
print(dict1)

Output

  

5) Dictionary delete key

  • Can pass del Delete a key

example 3.5

dict1 = {"name": "admin", "pwd": "123456"}
print(dict1)
del dict1["pwd"]
print(dict1)

Output

   

6) Dictionary modifier

  • adopt [ ] Directly modifying

example 3.6

dict1 = {"name": "admin", "pwd": "123456"}
print(dict1)
dict1["pwd"] = "654321"
print(dict1)

Output

   

The end ……

Originality is not easy. , Reprint please indicate the source .

If it's helpful to you, you can press one key three times , It will be updated continuously ( Hee hee ).


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