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

Python (VIII) - detailed use of lists

編輯:Python

List of articles

  • Python( 8、 ... and )—— Detailed use of lists
    • 1、 Additive elements
    • 2、 Remove elements
    • 3、 Modifying elements
    • 4、 Look for the element

Python( 8、 ... and )—— Detailed use of lists

1、 Additive elements

append()

append() Method : Add elements at the end of the list

  • You can append an element to the end of the list
  • You can also think of the entire list as an element , Then append to the end of the list
# Append an element to the end of the list 
my_list = ['a', 'b', 'c']
my_list.append('d')
print(my_list)
# Append the entire list to the end of the list 
my_list2 = ['d', 'e', 'f']
my_list.append(my_list2)
print(my_list)

Execution results :

['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', ['d', 'e', 'f']]

insert()

insert() Method : Add an element at the specified index position of the list

my_list3 = ['a','b','d','e']
my_list3.insert(2,'c')
print(my_list3)

Execution results :

['a', 'b', 'c', 'd', 'e']

extend()

extend() Method : Add the elements in a list from the beginning to the list one by one

my_list3 = ['a', 'b', 'c', 'd', 'e']
my_list4 = ['f','g','h']
my_list3.extend(my_list4)
print(my_list4)

Execution results :

['a', 'b', 'c', 'd', 'e','f', 'g', 'h', ]

2、 Remove elements

del Delete elements according to subscript

del keyword : Delete according to the target element index

my_list7 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19]
# Delete single element 
del my_list7[3]
print(my_list7)
# Delete the element of a section 
del my_list7[2:4]
print(my_list7)

Execution results :

[41, 63, 89, 37, 101, 77, 56, 16, 19]
[41, 63, 101, 77, 56, 16, 19]

When used directly del When receiving list name , Will delete the entire list

pop() Delete the last element of the list

pop() Method : When subscript is not specified , By default, the last element in the list will be deleted ( Similar to the stack out operation ), Specifying the subscript will delete the specified element

my_list7 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19]
my_list7.pop()
print(my_list7)

Execution results :

[41, 63, 89, 22, 37, 101, 77, 56, 16]

remove() Delete... Based on element value

remove() Method : Delete according to the value of the element , If you have two values , Only the first , If the value does not exist, an error is reported

my_list7 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19, 101]
my_list7.remove(63)
my_list7.remove(101)
print(my_list7)

Execution results :

[41, 89, 22, 37, 77, 56, 16, 19, 101]

clear() Delete all elements

clear() Method : Delete all elements in the list

my_list7 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19, 101]
my_list7.clear();
print(my_list7)

Execution results : The list is cleared

[]

3、 Modifying elements

Modify individual elements

Reassign an element , Is to modify this element :

my_list5 = [41, 63, 89, 22, 37, 101, 77, 56]
my_list5[2] = -36
my_list5[-3] = -202 # Negative subscripts start at the far right , by -1
print(my_list5)

Execution results :

[41, 63, -36, 22, 37, -202, 77, 56]

Modify a set of elements

Specify an interval ( Left closed right away ), Re assign values to the elements in this interval

my_list5 = [41, 63, 89, 22, 37, 101, 77, 56]
my_list5[2:4] = [98,33,55]
print(my_list5)

Execution results : The specified interval is [2-4), The interval has only two elements , But we assign three values , The extra value will be added to the list

[41, 63, 98, 33, 55, 37, 101, 77, 56]

But if the step size is specified in the interval , It requires the same number of interval elements and assignments , Otherwise, an error will be reported , Amendment No 1,3,5 Elements :

my_list5 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19]
my_list5[1:7:2] = [36, 33, 202]
print(my_list5)

Execution results :

[41, 36, 89, 33, 37, 202, 77, 56, 16, 19]

4、 Look for the element

in and not in

in: If the element exists in the list , The result is true, Instead of false

not in: If the element does not exist in the list , The result is true, Instead of false

hero_list = [" Berserker iron "," Li Bai "," Sun Bin "]
hero = input(" Please select a hero :")
if hero in hero_list:
print(" Common Heroes ")
else:
print(" Not in the hero pool ")

Execution results :

 Please select a hero : Li Bai
Common Heroes
Please select a hero : concubine
Not in the hero pool

index()

index() Method : Returns the index subscript of the first occurrence of an element in the list , An error is reported if the element does not exist

my_list6 = [41, 63, 89, 22, 37, 101, 77, 56, 16, 19,101]
print(my_list6.index(101))
# Find in the specified interval ( Left closed right open interval )
print(my_list6.index(37,2,7))

Execution results :

5
4

count()

count() Method : Returns the number of times an element appears in the list

my_list6 = [41, 63, 16, 22, 37, 101, 16, 56, 16, 19,101]
print(my_list6.count(16))
print(my_list6.count(1))

Execution results :

3
0

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