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

Python Development -- 13 list type built-in methods

編輯:Python

List of articles

    • One . Define the way
    • Two . Type conversion
    • 3、 ... and . Common operations + Built-in methods
    • Priority operation (*********)
      • 1、 By index ( just + reverse )+ You can take a value or change it
      • 2、 section , Head and tail + step
      • 3、 length
      • 4、 Members of the operation in and not in
      • 5、 Additional .append()
      • 6、 Insert .insert()
      • 7、 Delete del, remove, pop
      • 7、 Value cycle
    • Operations that need to be mastered (****)
      • 1. Count the number of characters : .count()
      • 2. Expand , Append multiple values : .extend()
      • 3. clear list : .clear()
      • 4. Shallow copy list : .copy()
      • 5. Calculate the number of : .count()
      • 6. Search index : .index()
      • 7. Sort : .sort()
      • 8. trans : .reverse()
    • Four . List comparison rules
    • 5、 ... and . Analog data structure
      • 1. queue : FIFO ( fifo )
      • 2. Stack : LIFO ( Last in, first out )
    • 6、 ... and . summary
      • There can be multiple values
      • Orderly , Depends on index values
    • 6、 ... and . summary
      • There can be multiple values
      • Orderly , Depends on index values
      • Variable type ---> must not hash type

One . Define the way

  • stay **“[ ]”** Separate multiple elements with commas inside , No type restrictions
  • List type “ factory ” Namely list
li = [111,222]
# The operation behind it is 
li = list([111,222])

Two . Type conversion

  • Anything that can be for The type of loop traversal can be passed to the list as a parameter to make its element
# Convert string to list type 
x = "aasdasd"
li = list(x)
print(li) #['a', 'a', 's', 'd', 'a', 's', 'd']
# Translate the dictionary into English , It's a dictionary "key"
# working principle 
li = []
x = "aasdasd"
for i in x:
li.append(i)
print(li)

3、 ... and . Common operations + Built-in methods

Priority operation (*********)

1、 By index ( just + reverse )+ You can take a value or change it

l=['a','b','c']
# positive 
print(l[1]) #b
# reverse 
print(l[-1]) #c
# Change value , Change the case 
l[2]='d'
print(l) #['a', 'b', 'd']
# If the index does not exist, an error is reported 
l[8]='d'
print(l) # Report errors 

2、 section , Head and tail + step

li=['song','hai','xing','hhh','lihai']
# Look at the head and ignore the tail 、 step 
print(li[0:3]) # ['song','hai','xing']
print(li[0:5:2]) # ['song','xing','lihai']
# All slices ( Shallow copy )
print(li[:len(li)])
print(li[0:])
print(li[:])
# Reverse the list 
print(li[::-1]) # ['lihai', 'hhh', 'xing', 'hai', 'song']

ps : Depth copy https://www.cnblogs.com/songhaixing/p/14015669.html

3、 length

li=['song','hai','xing','hhh','lihai']
print(len(l)) #5

4、 Members of the operation in and not in

li = [111,222,333,[344,33]]
print(111 in l) #True
print(099 in l) #False

5、 Additional .append()

  • Append a character to the end of the list
l=["aaa",2222,"cccc"]
res=l.append("ddd")
l.append("eee")
print(l) # ['aaa', 2222, 'cccc', 'ddd', 'eee']
print(res) # None ( No return value )

6、 Insert .insert()

  • stay " who " Insert a character in front of
l=["aaa",2222,"cccc"]
res=l.insert(1,"xxx")
print(l) #['aaa', 'xxx', 2222, 'cccc']
print(res) #None ( No return value )

7、 Delete del, remove, pop

  • del : Delete directly " who ", Universal universal delete , It does not support assignment Syntax , The assignment will report an error
  • .remove() : Delete directly by member
  • .pop () : Delete by index
msg=['song','hai','xing','hhh','lihai']
# Delete... By index 
del msg[1]
del msg[1:5:2] #del Add slices (hai,hhh)
print(msg)
# Delete directly through the element , no return value 
msg.remove('hai')
print(msg)
# Delete... By index , And you can get this value 
msg.pop(1)
msg.pop() # Do not place index, delete end by default 
print(msg)
# "pop" Unique functions of : There is a return value 
print(msg.pop(1)) # You can pop up this value 
print(msg.remove('hai')) # Just delete , It doesn't return this value 

7、 Value cycle

l=[111,222,333,444,555,666,777]
for i in l:
print(i)

Operations that need to be mastered (****)

1. Count the number of characters : .count()

l=[111,222,333,444,111,555,666,777]
print(l.count(111)) #2

2. Expand , Append multiple values : .extend()

l=[111,222,]
# character string 
l.extend("hello")
print(l) #[111, 222, 'h', 'e', 'l', 'l', 'o']
# list 
l.extend(["xxx","yyy"])
print(l) #[111, 222, 'h', 'e', 'l', 'l', 'o', 'xxx', 'yyy']
# If you add a dictionary, you add "key"

3. clear list : .clear()

msg=['song','hai','xing','song','lihai']
msg.clear()
print(msg) # []

4. Shallow copy list : .copy()

msg=['song','hai','xing','song','lihai']
l=msg.copy()
print(l) # ['song','hai','xing','song','lihai']

5. Calculate the number of : .count()

msg=['song','hai','xing','hhh','lihai']
print(msg.count('hai')) # 1

6. Search index : .index()

  • Find the index of the specified character in the list , You can specify a range to find
  • There's nothing in the list like... In the string : find、rfind、rindex These methods of
msg=['song','hai','xing','song','lihai']
print(msg.index('song',2,4)) # 3 ( I can't find a mistake )

7. Sort : .sort()

l=[5,7,9,3,1,4]
l.sort() # Sort 
print(l)
# Parameters "reverse" trans 
l.sort(reverse=True) # Reverse sorting 
print(l)

8. trans : .reverse()

  • Reverse the order of the list , The change is the source list
li = [1, 'egon', 'alex', 'lxx']
li.reverse()
print(li) # ['lxx', 'alex', 'egon', 1]

Four . List comparison rules

# according to ascll Compare the decimal system corresponding to the code table 
l1='hello'
l2='k'
print(l1 < l2) #abcd.... From small to large 
l1=[3,'a','g','j',]
l2=[1,'c','g',]
print(l1 > l2) # One by one , Otherwise an error 
print('Z'>'a') # Upper case is less than lower case A-Z,a-z
print('a'>'G') # Larger than any capital letter 

5、 ... and . Analog data structure

1. queue : FIFO ( fifo )

  • It's like a mall escalator , First up first down
l=[]
# Queue entry 
l.append("first")
l.append("second")
l.append("third")
print(l) #['first', 'second', 'third']
# Out of the team 
print(l.pop(0)) #first
print(l.pop(0)) #second
print(l.pop(0)) #third

2. Stack : LIFO ( Last in, first out )

  • It's like folding clothes in a box
l=[]
# Push 
l.append("first")
l.append("second")
l.append("third")
print(l) #['first', 'second', 'third']
# Out of the stack 
print(l.pop()) #third
print(l.pop()) #second
print(l.pop()) #first

6、 ... and . summary

  • There can be multiple values

  • Orderly , Depends on index values

( Last in, first out )

  • It's like folding clothes in a box
l=[]
# Push 
l.append("first")
l.append("second")
l.append("third")
print(l) #['first', 'second', 'third']
# Out of the stack 
print(l.pop()) #third
print(l.pop()) #second
print(l.pop()) #first

6、 ... and . summary

  • There can be multiple values

  • Orderly , Depends on index values

  • Variable type —> must not hash type


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