1, create a list
>>> list1=[' A ', ' B ', ' C ', ' d ']>>> list2=[1,2,3,4]>>> list3=[' A ', ' B ', ' C ', and
2, access the values in the list
>>> print ' list1[0]: ', list1[0]list1[0]: a>>> print ' list2[2]: ', list2[2]list2[2]: 3
Negative index value calculation formula List[-n]==list[len (list)-n]
>>> List1[-2]==list1[len (List1) -2]true>>>
3 Modifying or updating a list
>>> print ' list1[1]: ', list1[1]list1[1]: b>>> list1[1]=3>>> print ' list1[1]: ', list1[1] LIST1[1]: 3>>>
4, deleting list elements
>>> list1[' A ', 3, ' C ', ' d ']>>> del list1[1]>>> list1[' A ', ' C ', ' d ']
5, truncation gets the list value
>>> list1[1, 2, 3, 4, 5, 6, 7]>>> list1[1:4][2, 3, 4]>>> list1[1:-1][2, 3, 4, 5, 6]>>> List1[1:][2, 3, 4, 5, 6, 7]>>> list1[:][1, 2, 3, 4, 5, 6, 7]
1, starting from 1 but not including the 3 List
2, from the second element of the list to the last element of the list, but does not include the last
3, according to the subscript from 0 to 3 , but does not include 3
the effect is as above,0 can be omitted
4, from subscript n to the last of the array, including the last one
5 . All elements of list
6
>>> list1[1, 2, 3, 4, 5, 6, 7]>>> list1+=[' A ', ' B '] >>> list1[1, 2, 3, 4, 5, 6, 7, ' A ', ' B ']>>> List1.append ("Hello") >>> list1[1, 2, 3, 4, 5, 6, 7, ' a ', ' B ', ' Hello ']>>> list1.append ([up]) >>> list1[1, 2, 3, 4 , 5, 6, 7, ' A ', ' B ', ' Hello ', [1, 2]]>>>list1.extend ([' One ', ' one ', ' Three ') >>> list1[1, 2, 3, 4, 5, 6, 7, ' a ', ' B ', ' Hello ',[1, 2], ' one ', ', ' three ']>>> list1.extend (' Jia ') >>> list1[1, 2, 3, 4, 5, 6, 7, ' A ', ' B ', ' Hello ', [1, 2], ' One ', ' II ', ' three ', ' j ', ' I ', ' a ']>>> list1.insert (0, ' a ') > >> list1.insert (2,' B ') >>> list1[' a ', 1, ' B ', 2, 3, 4, 5, 6, 7, ' a ', ' B ', ' Hello ', [1, 2], ' one ', ' II ', ' three ', ' j ', ' I ', ' a ']> >>
1, use the + operator to connect to the list to create a new list,the list has no limit, but this operation is actually created 2 list Large memory consumption
2. List can load elements of any data type, not necessarily the same type
3,append() method to add an element (any data type) to the end of the list
4,list is actually a class, creating a list is actually instantiating a class, theextend() method receives a list as a parameter and the new list Add each element to the existing list.
5,insert() method adds an element to the list , the first parameter is the index value of the position added to the list
The elements of the list can be repeated
7,list value Operation
>>> list1[' A ', 1, ' B ', 2, 3]>>> list1.count () Traceback ( Most recent call last): file "<stdin>", line 1, in < Module>typeerror: count () takes exactly oneargument (0 given) >>> list1.count (' B ') 1>>> list1.count (2) 1>>> ' B ' in list1True>> > 4 in list1false>>> list1.index (3) 4>>> list1.index (' 3 ') traceback (most recent call last): file "<stdin>", line 1, In <module>valueerror: list.index (x): x not in list>>> List1.index (' B ') 2>>> list1.index (' d ') traceback (most recent call last): File "<stdin>", line 1, in <module>valueerror: list.index (x): x not in list>&gT;>
1. The Count() method returns the number of lookup values in the list
2.If you only want to know if the value containing the lookup can use in, return True or False, this method is faster than the Count() method
3.use the index () method to return the indexed value of the value, or you can add the second parameter as the Find start position, and the third parameter as the end of the lookup
4.The Index () method returns only the location that was first found
5, because 1 in Python is a meaningful index value, so the index() method does not find the corresponding position will throw an exception
8, delete
Remove Delete
>>> list1[' A ', 1, ' B ', 2, 3]>>> list1.remove (' B ') >>> list1[' A ', 1, 2, 3]>>> List1.rem Ove (' B ') Traceback (most recent): File "<stdin>", line 1, in <module>valueerror:list.remove (x): X No T in list>>>
1.using the Remove() method,The Remove method accepts a value and removes the first one encountered, with the same subscript without gaps
2. throws an exception when a value cannot be removed using the Remove method
using the pop method to remove
>>> list1[' A ', 1, 2, 3]>>> list1.pop () 3>>> list1[' A ', 1, 2]>>> list1.pop () 2>> > List1.pop () 1>>> list1.pop () ' A ' >>> List1.pop () Traceback (most recent call last): File "<stdin& gt; ", line 1, in <module>indexerror:pop from empty list
1, call the pop() method does not give parameters, will default to delete the last element of list
2, call the pop() method and pass an index value, will delete the value of a specific location
3. calling the Pop() method in the empty list throws an exception
This article is from the "Ding classmate 1990" blog, please be sure to keep this source http://dingtongxue1990.blog.51cto.com/4959501/1675153
Python's list base operation