Python List List Use introduction _python

Source: Internet
Author: User
Tags stdin python list
A set of ordered items in a set of
variable data types "can be added to the search"
list can contain any data type, or can contain another list "can be combined nested"
list is a set of data surrounded by square brackets "[]", different members are separated
by "," List to access members by ordinal

Defined

>>> L = [] #空列表
>>> l = [1,2,3]
>>> l = [1,2,3,[' A ', ' B ']]
>>> l = list (' Li Nuxeye ')
>>> l
[' l ', ' I ', ' n ', ' U ', ' x ', ' e ', ' y ', ' e ']
>>> l = List (range (5))
> >> l
[0, 1, 2, 3, 4]
>>> l = ' 1;2;3;4;5 '. Split (';')
>>> l
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']

The built-in function list (a_sequence) can convert a sequence to a list

Index
The positive index is numbered from left to right, 0 begins, negative index number is from right to left,-1,-2 ... ; The range values are from left to right

>>> languagelist = ['Python', ' Shell ', ' Perl ', ' PHP ', ' Java ', ' C ', ' C + + ', ' Lisp ']
>>> Languagelist[0] #取第一个值
' Python '
>>> languagelist[3] #第四个值
' PHP '
>>> languagelist [3:]
[' PHP ', ' Java ', ' C ', ' C + + ', ' Lisp ']
>>> Languagelist[:3]
[' Python ', ' Shell ', ' Perl ']
>>> languagelist[2:5]
[' Perl ', ' PHP ', ' Java ']
>>> languagelist[-1] #倒数第一个值
' Lisp '
>>> languagelist[-2] #倒数第二个值
' C + + '
> >> languagelist[-5:]
[' PHP ', ' Java ', ' C ', ' C + + ', ' Lisp ']
>>> languagelist[-5:-2]
[' php ', ' Java ', ' C ']
>>> languagelist[:-2]
[' Python ', ' Shell ', ' Perl ', ' php ', ' Java ', ' C ']

adding elements

>>> languagelist
[' Python ', ' Shell ', ' Perl ', ' PHP ', ' Java ', ' C ', ' + + ', ' Lisp ']
>>> Languagelist.append (' Go ')
>>> languagelist
[' Python ', ' Shell ', ' Perl ', ' PHP ', ' Java ', ' C ', ' + + ', '] Lisp ', ' Go ']
>>> languagelist.insert (3, ' Python ') #往PHP元素前插入
>>> languagelist
[' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C ', ' C + + ', ' Lisp ', ' Go ' '
>>> languagelist.extend ([' Ruby ', ' C # ']
>>> languagelist
[' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C ', ' + + ', ' Lisp ' , ' Go ', ' Ruby ', ' C # '

Search

>>> languagelist = [' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C ', ' + + ', ' Lisp ', ' Go ', ' Ruby ', ' C # ']
  >>> languagelist.index (' Python ')
0
>>> languagelist.index (' C ')
6
>> > Languagelist.index (' C ')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>< C8/>valueerror:list.index (x): X not in list
>>> ' C ' languagelist
False
>>> Languagelist.index (' Python ') #统计一个元素的出现次数
2

Delete Element

>>> languagelist = [' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C ', ' + + ', ' Lisp ', ' Go ', ' Ruby ', ' C # ']
  >>> languagelist.remove (' C ')
>>> languagelist.remove (' Python ')
>>> Languagelist.remove (' C ')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError:list.remove (x): X not in List
>>> languagelist.pop ()
' C # '
>>> Languagelist
[' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' C + + ', ' Lisp ', ' Go ', ' Ruby ']
>>> Languagelist.pop ( -2)
[' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' C + + ', ' Lisp ', ' Ruby ']
>>> del Languagelist[-3:-1]
>>> languagelist
[' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' Ruby ']

Remove deletes only the first occurrence of a value from the list.
The pop does two things: delete the last element of the list, and then return the value of the deleted element.

modifying elements

>>> languagelist = [' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' Ruby ']
>>> languagelist[- 2] = ' C '
>>> languagelist
[' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' C ', ' Ruby ']
>>> Languagelist[languagelist.index (' C ')] = ' JAVA '
>>> languagelist
[' Python ', ' Shell ', ' Perl ', ' Python ', ' PHP ', ' JAVA ', ' Ruby '

Operator

>>> languagelist
[' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' C + + ', ' Lisp ', ' Go ', ' Ruby ']
>>> Languagelist = languagelist + [' python ', ' C ']
>>> languagelist
[' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C + + ', ' Lisp ', ' Go ', ' Ruby ', ' Python ', ' C ']
>>> languagelist + = [' java ']
>>> Languagelist
[' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C + + ', ' Lisp ', ' Go ', ' Ruby ', ' python ', ' C ', ' Java ']
;>> numlist = [1,2,3] * 3
>>> numlist
[1, 2, 3, 1, 2, 3, 1, 2, 3]

Sort

>>> numlist = [2,5,3,6,1,4]
>>> numlist.reverse ()
>>> numlist
[4, 1, 6, 3, 5, 2]< C5/>>>> Numlist[::-1] #和reverse效果相同
>>> numlist
[2, 5, 3, 6, 1, 4]
>>> Numlist.sort ()
>>> numlist
[1, 2, 3, 4, 5, 6]

Sort acceptable parameters

CMP, Comparison function, accept two parameters, return negative when less than return positive, equal return 0
key, specify sort key
reverse, specify whether to reverse order

List comparison, implicitly calls the CMP method, the comparison rule is to scan the elements for comparison, if you can compare, compare, if the equality scan the next element, if not equal to return the result, if two element types can not be compared, compare the ID () value of two objects. If it is always equal until a list scan is finished, then a longer list is returned larger

>>> L = [(13,54), (11,59), (15,55), (12,57), (10,56)]
>>> l.sort (Key=lambda x:x[1])
> >> L [(A), ((a), (a), (L.sort)
, (a)]
>>> (Key=lambda x:x[1],reverse=1) # or reverse=true
>>> L
[(11, 59), (12, 57), (10, 56), (15, 55), (13, 54)]

Sort and sorted function difference

Numlist = [2, 5, 3, 6, 1, 4]
>>> sorted (numlist) [1, 2, 3
, 4, 5, 6]
>>> numlist
[2, 5, 3 , 6, 1, 4]
>>> numlist.sort ()
>>> numlist
[1, 2, 3, 4, 5, 6]

Sort: Sorting on the original list, not returning sorted list
Sorted: Do not change the original list, return the sorted 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.