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

Python list related methods

編輯:Python

List + and *

# coding:utf-8
if __name__ == '__main__':
''' Lists can be merged using + The same list merge can use * '''
a = ['a']
b = ['b']
print(a + b) # ['a','b']
print(a * 3) # ['a','a','a']

Last but not least

# coding:utf-8
if __name__ == '__main__':
''' append(item): 1. Is to modify directly on the original list , No new list will be generated 2. Only one element can be appended 3. Append to the end '''
a = ['a']
a.append('b')
print(a) # ['a', 'b']

Append the element at the specified position

# coding:utf-8
if __name__ == '__main__':
''' insert(index,item): 1. At a designated location index Insert new elements item, The element in the occupied position and the element behind it move one position to the right in turn 2. Operate directly on the list , No new list will be generated 3. When specified location index >= List the current number of elements , It is equivalent to the last element (append()) '''
a = ['a', 'b', 'd', 'e']
a.insert(1, 'f')
print(a) # ['a', 'f', 'b', 'd', 'e']

Count the number of a member in the list

# coding:utf-8
if __name__ == '__main__':
''' count(item): Count the members in the list item The number of Return on no 0 '''
a = ['a', 'b', 'c', 'b', 'd']
print(a.count('b')) # 2

Delete the specified member

# coding:utf-8
if __name__ == '__main__':
''' remove(item): 1. Delete list elements item(1 individual , Not all , From left to right ), If item Does not exist in the list , False report 2. You can only delete one by one '''
a = ['a', 'b', 'b', 'c']
a.remove('b')
print(a) # ['a', 'b', 'c']

Data inversion

# coding:utf-8
if __name__ == '__main__':
''' reverse(): 1. Reverse the order of the elements of the list , From left to right to right 2. Directly change the original list '''
a = ['a', 'b', 'c']
a.reverse()
print(a) # ['c', 'b', 'a']

List is empty

# coding:utf-8
if __name__ == '__main__':
''' clear(): Clear the list element data , Become an empty list What's always useful : The empty list obtained by clearing the list is less than the empty list directly created by creating a memory address ( A hint of efficiency ) '''
a = ['a', 'b', 'c']
a.clear()
print(a) # []

Sort the list

# coding:utf-8
if __name__ == '__main__':
''' sort(reverse=Flase): 1.sort() Of reverse The default value is False 2.sort() Of reverse=False positive sequence 3.sort() Of reverse=True In reverse order '''
a = ['a', 'c', 'b']
a.sort(reverse=False)
print(a) # ['a', 'b', 'c']
a.sort()
print(a) # ['a', 'b', 'c']
a.sort(reverse=True)
print(a) # ['c', 'b', 'a']

Shallow copy of list

# coding:utf-8
if __name__ == '__main__':
''' copy(): A shallow copy of the list What is shallow copy : Copy only the first layer of data , Data of other layers are still shared '''
# example 1 Shallow copy Layer 1 data changes do not affect each other 
a = ['a', 'b', 'c']
b = a.copy()
a.append('e')
print(a) # ['a', 'b', 'c', 'e']
print(b) # ['a', 'b', 'c']
del a
print(b) # ['a', 'b', 'c']
# example 2 The second layer begins to interact ( Shared memory addresses )
a = [['a'], 'b', 'c']
b = a.copy()
a[0].append('b')
print(a) # [['a', 'b'], 'b', 'c']
print(b) # [['a', 'b'], 'b', 'c']
print(id(a[0])) # 140230971180928
print(id(b[0])) # 140230971180928

Import of lists

# coding:utf-8
if __name__ == '__main__':
''' extend(item): take item Import into the list 1. Only strings are supported , list , Yuan Zu , Dictionary type 2. When item Is a list or tuple , Merge its members into a list 3. When item When it's a dictionary , The dictionary key Merge into the list 4. When item When is a string , Combine strings one character at a time into the list '''
# example 1 Import list 
a = []
a.extend(['a', 'b', 'c']) # ['a', 'b', 'c']
print(a)
# example 2 Import tuple 
a.clear()
a.extend(('a', 'b', 'c')) # ['a', 'b', 'c']
print(a)
# example 3 Import string 
a.clear()
a.extend('abcd') # ['a', 'b', 'c', 'd']
print(a)
# example 4 Import Dictionary 
a.clear()
a.extend({
'name': 'xie', 'age': 27})
print(a) # ['name', 'age']

Slice of the list

# coding:utf-8
if __name__ == '__main__':
''' Slice using : 1.list[start:end:step] From the index start Start , every other step Get an element , Until index end( The retrieved value contains the index start Value on , Index not included end Value on ), Return to a new list start The default value is 0,end The default value is len(list),step The default value is 1 example 1 2. When step A negative number means to take... From right to left example 2 3. When start or end When it's negative , Means to confirm the index from right to left , The rightmost from the index -1 Start example 3 4. Using slice and assignment will affect the original list , Replace the slice position of the list with the new value Column 4 '''
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
# example 1
print(a[::]) # a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print(a[0:len(a):1]) # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print(a[1:2]) # ['b']
# example 2
print(a[::-1]) # ['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
# example 3
print(a[-2:-1]) # ['g'] -1 yes h -2 yes g
# example 4
# When step=1 when , List length that does not require assignment , String length , Tuple length , Dictionary length Equal to the number of slices 
b = a.copy()
a[::] = 'abef'
print(a) # ['a', 'b', 'e', 'f']
# When step It's not equal to 1 when , The length of the list to be assigned , String length , Tuple length , Dictionary length It must be equal to the number of slices 
print(b[::2]) # ['a', 'c', 'e', 'g']
b[::2] = 'ffff'
print(b) # ['f', 'b', 'f', 'd', 'f', 'f', 'f', 'h']
# Assignments can be tuples , character string , list , Dictionaries 
# When the assignment is a list or tuple , Replace its members with the slice contents 
# When assignment is a dictionary , The dictionary key Replace slice contents 
# When the assignment is a string , Replace the contents of the slice character by character 

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