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

Python - List

編輯:Python

         Sequence is a way to store data , Used to store a series of data .

         Common sequence structures are : character string ( See last time )、 list 、 Tuples 、 Dictionaries 、 aggregate

list : Used to store any number of 、 Any type of data set .

         List defined criteria Grammar format : a = [10,20,30,40] among ,10,20,30,40 These are called : list a The elements of . The elements in the list can be different , It can be Any type . such as : a = [10,20,'abc',True]

         The common methods of list objects are summarized as follows :

          List creation :

                1.[] Create a list of

>>> a = [10,20,'giao','hu']
>>> a = [] # An empty list 

                2.list() Create a list of

>>> a = list() # An empty list
>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a = list("giao,hu")
>>> a
['g', 'i', 'a', 'o', ',', 'h', 'u']

                3.range() establish Integers list :

Format :range(start,end,step)

>>> range(3,5,1)
range(3, 5) # Create a list of
>>> list(range(3,5,1))
[3, 4] #list() Print
>>> list(range(-10,-1,3))
[-10, -7, -4]
>>> list(range(10,1,-3))
[10, 7, 4]
>>> list(range(-1,-10,-3))
[-1, -4, -7]

                4. Generate list by derivation

>>> a = [x*2 for x in range(5)]
>>> a
[0, 2, 4, 6, 8]

        The addition of list elements :

                1.append()

         Modify the list object in place , Is to add a new element at the end of the real list , The fastest , Recommended .

>>> a = [20,40]
>>> a.append(60)
>>> a
[20, 40, 60]

                2.+

         Create a new list object , Copy the elements of the original list and the elements of the new list into the new list object in turn . Slow speed , Large amount of data not recommended .

>>> a = [20,40]
>>> id(a)
46016072
>>> a = a+[50]
>>> id(a)
46015432

                3.extend()

         Add all elements of the target list to the end of this list , Do not create new list objects .

>>> a = [20,40]
>>> id(a)
46016072
>>> a.extend([50])
>>> id(a)
46016072

                4.insert()

         Any element can be inserted into any position , But it will cause the whole copy and paste displacement of the subsequent elements , Low efficiency . Similar to that :remove()、pop()、del().

>>> a = [10,20,30]
>>> a.insert(2,100)
>>> a
[10, 20, 100,30]

                5. Multiplication extension

         Generate a new list .

>>> a = ['sxt',100]
>>> b = a*3
>>> a
['sxt', 100]
>>> b
['sxt', 100, 'sxt', 100, 'sxt', 100]

        List element delete :

                1.del Delete

         Delete the element at the specified position in the list .

>>> a = [100,200,888,300,400]
>>> del a[1]
>>> a
[100,200,300,400]

                2.pop() Delete

        Delete and return the specified location element , If no location is specified, the last element of the default action list .

>>> a = [10,20,30,40,50]
>>> a.pop()
50
>>> a
[10, 20, 30, 40]
>>> a.pop(1)
20
>>> a
[10, 30, 40]

                3.remove()

         Delete the first occurrence of the specified element , If the element does not exist, an exception is thrown .

>>> a = [10,20,30,40,50,20,30,20,30]
>>> a.remove(20)
>>> a
[10, 30, 40, 50, 20, 30, 20,30]

         List element access and counting :

               1.  Direct access to elements through indexes .

>>> a = [10,20,30,40,50,20,30,20,30]
>>> a[2]
30

               2.index() visit

         Gets the index position of the first occurrence of the specified element .a.index( Access elements , start , End )

>>> a = [10,20,30,40,50,20,30,20,30]
>>> a.index(20)
1
>>> a.index(20,3)
5
>>> a.index(30,5,7)
6

                3.count() visit

         Returns the number of times the specified element appears in the list .

>>> a = [10,20,30,40,50,20,30,20,30]
>>> a.count(20)
3

               4.len() Return length

        Returns the number of list elements .

>>> a = [10,20,30]
>>> len(a)
3

        Membership judgment :

        in and not in

>>> a = [10,20,30,40,50,20,30,20,30]
>>> 20 in a
True
>>> 100 not in a
True

         Slicing operation :

        slice operation ( See last time )

         Traversal of list :

>>> a=[0,1,2,3,4,5,6,7,8,9]
>>> for a in range(len(a)):
>>> print(a,end=' ')
0 1 2 3 4 5 6 7 8 9

        Sort the list :

                1. Do not create a new sort of list :

>>> a = [20,10,30,40]
>>> a.sort() # Default ascending order
>>> a
[10, 20, 30, 40]
>>> a = [10,20,30,40]
>>> a.sort(reverse=True) # Descending order
>>> a
[40, 30, 20, 10]
>>> import random
>>> random.shuffle(a) # Out of order
>>> a
[20, 40, 30,10]

                2. Create a new sort of list :

>>> a = [20,10,30,40]
>>> a = sorted(a) # Default ascending order
>>> a
[10, 20, 30, 40]
>>> a = [20,10,30,40]
>>> b = sorted(a)
>>> b
[10, 20, 30, 40]
>>> c = sorted(a,reverse=True) # Descending
>>> c
[40, 30, 20, 10]

                3.reversed() Return iterator :

         Returns an iterator object arranged in reverse order . Be careful : This object can only be used once .

>>> a = [20,10,30,40]
>>> c = reversed(a)
>>> c
<list_reverseiterator object at 0x0000000002BCCEB8>
>>> list(c)
[40, 30, 10, 20]
>>> list(c)
[]

        List related functions :

                max(),min(),sum() wait .

        2 d list :

>>> a = [
[" Gao Xiaoyi ",18,30000," Beijing "],
[" Gao Xiaoer ",19,20000," Shanghai "],
[" Gao Xiaoyi ",20,10000," Shenzhen "],
]
>>> for m in range(3):
>>> for n in range(4):
>>> print(a[m][n],end=' ')
>>> print()
Gao Xiaoyi 18 30000 Beijing
Gao Xiaoer 19 20000 Shanghai
Gao Xiaoyi 20 10000 Shenzhen


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