Python-list: List

Source: Internet
Author: User

A "list" is a value that contains a sequence of multiple words. The term "list value" refers to the list itself (which, as a value, can be saved in a variable, passed to a function)

list [' AA ', ' BB ', ' cc ', ' DD '] is a simple listing

1. Use list subscript to value

List =[' aa ', ' BB ', ' cc ', ' DD '

List[0]--aa

List[1]--bb

List[-1]--dd

2. Slice value (take one or more values from the list and return the result is a list)

List =[' aa ', ' BB ', ' cc ', ' DD '

list[:3]--[' AA ', ' BB ', ' CC ' #顾头不顾尾

list[:1]--[' AA ']

list[0:-1]--[' AA ', ' BB ', ' CC '

3. Get the length of the list with Len ()

list [' AA ', ' BB ', ' cc ', ' DD ']

Len (list)--4

4. Use subscript to change the values in the list

List =[' aa ', ' BB ', ' cc ', ' DD '

list[0]= ' AA '--[' AA ', ' BB ', ' cc ', ' DD ']

LIST[0]=LIST[1]----[' BB ', ' BB ', ' cc ', ' DD ']

5. List connections and list replication

list= [' AA ', ' BB ', ' cc ', ' DD ']

num=[' 11 ', ' 22 ', ' 33 ', ' 44 ']

list+num--[' AA ', ' BB ', ' cc ', ' dd ', ' 11 ', ' 22 ', ' 33 ', ' 44 ']

list+[' AA ', ' BB ']---[' aa ', ' BB ', ' cc ', ' dd ', ' AA ', ' BB ']

list*2--[' AA ', ' BB ', ' cc ', ' dd ', ' AA ', ' BB ', ' cc ', ' DD '

6. Delete the value from the list with Del

list= [' AA ', ' BB ', ' cc ', ' DD ']

del list[0]--[' BB ', ' cc ', ' DD ']

7. Use the list

Catanames = []
While True:
Print (' Enter the name of the cat ' +str (len (catanames)) + ' (or enter nothing to stop.): ')
name = str (input ())
If name = = ':
Break
Catanames = Catanames+[name]
Print (' The Cat Names is: ')
For name in Catanames:
Print (' +name ')

C:\Python27\python.exe c:/users/xiu/pycharmprojects/h5/day1/22.py
Enter the name of Cat0 (or enter nothing to stop.):
' Cat01 '
Enter the name of CAT1 (or enter nothing to stop.):
' cat02 '
Enter the name of CAT2 (or enter nothing to stop.):
' cat03 '
Enter the name of CAT3 (or enter nothing to stop.):
‘ ‘
The Cat names is:
cat01
cat02
cat03

Process finished with exit code 0

List for looping

list = [' cat001 ', ' cat002 ', ' cat003 ']
For I in range (len (list)):
Print (' index ' + str (i) + ' in list is: ' + list[i])

C:\Python27\python.exe c:/users/xiu/pycharmprojects/h5/day1/22.py
Index 0in list is:cat001
Index 1in list is:cat002
Index 2in list is:cat003

8.in and not in operators

list= [' AA ', ' BB ', ' cc ', ' DD ']

print ' AA ' in List--true

print ' Aa1 ' in List--false


9. Assignment to variable enhancement (+ 、-、 *,/,%)
AA = 10
AA +=2 #aa = AA +2 output 12
AA-=2 #aa = aa-2 Output 10
AA *=2 #aa = AA */ output 20
AA/=2 #aa = aa/2 Output 10
AA%=2 #aa = aa% 2 output 0
10. Use the index () method to find the value in the list: Check element subscript based on element value
list= [' AA ', ' BB ', ' cc ', ' DD ', ' CC ']
Print list.index (' BB ') #1
Print List.index (' cc ') #2: If the list has multiple values of the same element, the first one is returned

11. Add a value to the list using the Append ()/insert () method
list= [' AA ', ' BB ', ' cc ', ' DD ']
List.append (' ee ') #[' aa ', ' BB ', ' cc ', ' dd ', ' EE ']
List.insert (0, ' AA ') #[' AA ', ' AA ', ' BB ', ' cc ', ' dd ', ' EE ']

12.remove () Remove a value from the list
list= [' AA ', ' BB ', ' cc ', ' DD ']
List.remove (' AA ') #[' BB ', ' cc ', ' dd ') delete element value directly
Del list[0] #[' cc ', ' DD ') delete element value by subscript

13. Sort the values in the list using the sort () method (only the list of pure numbers, pure letters) can be sorted
list= [' DD ', ' cc ', ' BB ', ' AA ']
List.sort () #[' aa ', ' BB ', ' cc ', ' DD ' default ascending order
List.sort (reverse=true) #[' DD ', ' cc ', ' BB ', ' AA ']
List.sort (reverse=false) #[' aa ', ' BB ', ' cc ', ' DD ']
list= [' DD ', ' cc ', ' BB ', ' AA ', ' AA ', ' BB ', ' cc ', ' DD ']
List.sort () #[' aa ', ' BB ', ' cc ', ' dd ', ' AA ', ' BB ', ' cc ', ' DD ', ' using ' ASCII character sorting ', so that uppercase letters are preceded by lowercase letters

  
  

  

Python-list: List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.