Introduction to Python list, pythonlist

Source: Internet
Author: User
Tags python list

Introduction to Python list, pythonlist

The variable data type [add, delete, modify, and query can be performed] list of a set of Ordered items can contain any data type, you can also include another list [any combination of nesting]. The list is a data set surrounded by square brackets ([]). Different members are separated by commas (,) and the list can be accessed by serial numbers.

Definition

>>> L = [] # Empty list >>> l = [1, 2, 3] >>> l = [1, 2, 3, ['A ', 'B'] >>> l = list ('linuxey') >>> 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
Positive index numbers start from left to right, and negative index numbers start from right to left,-1,-2 ...; The value range is from left to right.

>>> Export agelist = ['python', 'shell', 'perl ', 'php', 'java', 'C', "C ++ ", "Lisp"] >>> LanguageList [0] # obtain the first value 'python' >>> LanguageList [3] # Fourth value 'php' >>> LanguageList [3:] ['php', 'java', 'C', 'c ++ ', 'lisp']> extends agelist [: 3] ['python', 'shell ', 'perl ']> extends agelist [] ['perl', 'php', 'java']
>>> Export agelist [-1] # The first and last values of 'lisp '> export agelist [-2] # The second and last values of 'c ++' >>> export agelist [- 5:] ['php', 'java', 'C', 'c ++ ', 'lisp']> LanguageList [-5:-2] ['php ', 'java', 'C'] >>> extends agelist [:-2] ['python', 'shell', 'perl ', 'php', 'java ', 'C']

Add Element

>>> Extends agelist ['python', 'shell', 'perl ', 'php', 'java', 'C', 'c ++ ', 'lisp ']> extends agelist. append ('Go') >>> extends agelist ['python', 'shell', 'perl ', 'php', 'java', 'C ', 'C ++ ', 'lisp', 'Go']> LanguageList. insert (3, 'python') # insert to the front of the PHP element >>> extends agelist ['python', 'shell', 'perl ', 'python', 'php ', 'java', 'C', 'c ++ ', 'lisp', 'Go'] >>> extends agelist. extend (['Ruby ', 'c #']) >>> extends agelist ['python', 'shell', 'perl ', 'python', 'php ', 'java', 'C', 'c ++ ', 'lisp', 'Go ', 'Ruby', 'c # ']

Search

>>> Export agelist = ['python', 'shell', 'perl ', 'python', 'php', 'java', 'C', 'c ++ ', 'lisp ', 'Go', 'Ruby', 'c # ']> extends agelist. index ('python') 0 >>> extends agelist. index ('C') 6 >>> your agelist. index ('C') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list. index (x): x not in list >>> 'C' in your agelistfalse >>> your agelist. index ('python') # count the number of occurrences of an element 2

Delete Element

>>> LanguageList = ['Python', 'Shell', 'Perl', 'Python', 'PHP', 'java', 'C', '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 removes the first appearance of only one value from the list.
Pop deletes the last element of the list and returns the value of the deleted element.

Modify Element

>>> 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 = [,] >>> NumList. reverse () >>> NumList [4, 1, 6, 3, 5, 2] >>> NumList [:: -1] # the same effect as reverse >>> NumList [2, 5, 3, 6, 1, 4] >>> NumList. sort () >>> NumList [1, 2, 3, 4, 5, 6]

Acceptable sort Parameters

Cmp, comparison function. Two parameters are accepted. If the value is less than 0, negative is returned. If the value is greater than positive, zero key is returned. The sorting key reverse is specified, and whether reverse order is returned.

List comparison operation. The cmp method is called implicitly. The comparison rule is to scan elements one by one for comparison. If comparison can be performed, comparison is performed. If the comparison rule is equal, the next element is scanned. If the comparison rule is not equal, the result is returned, if the two element types cannot be compared, the IDS () values of the two objects are compared. If it remains equal until a list scan ends, a large list is returned.

>>> L = [(13, 54), (11, 59), (15, 55), (12, 57), (10, 56)] >>> L. sort (key = lambda x: x [1]) >>> L [(13, 54), (15, 55), (10, 56), (12, 57), (11, 59)] >>> L. sort (key = lambda x: x [1], reverse = 1) # or reverse = True >>> L [(11, 59), (12, 57 ), (10, 56), (15, 55), (13, 54)]

Differences between sort and sorted Functions

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 without returning the sorted list
Sorted: returns the sorted list without changing the original 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.