Python Learning--list

Source: Internet
Author: User

Definitions: 1. Lists are composed of elements arranged in a series of specific order, which can contain letters, numbers, or anything added to a list.

2. The identification symbol for the list is [], and the function name is List

3. The list is ordered

Related concepts:

Elements: Values in a list are called elements, and elements are separated by commas

Subscript: A subscript for each element in the list, starting from left to right from 0, or starting from right to left-1

I. Simple operation of the list

1. Create an empty list

Lists = [] or lists = List ()

2. Create a list with several elements

Lists = [1,2,3,4,5,6,7,8] #创建一个包含数字1-9 of the list called lists

3. Accessing list elements

Print (lists[4]) #lists列表中包含八个元素, subscript from left to right is 0-7, prints the list element labeled 4

4. Modifying list elements

LISTS[8] = 9 #将下标为8 element changed to value 9

Two. List simple operation

1. Adding between the lists

List_1 = [' A ', ' B ', ' C ', ' d ']

List_2 = [' E ', ' f ', ' g ', ' H ']

#定义俩个列表list_1和list_2

#将俩个列表相加

Print (list_1 + list_2)

#结果显示为[' A ', ' B ', ' C ', ' d ',' e ', ' f ', ' g ', ' H ']

2. Multiplying the list

Print (List_1 * 2)

#代表列表相乘2次, the essence of the list list_1 assigned a copy, at this time the list_1 element becomes twice times the original

3. Shard operation of the list

Print (lists[::]) #显示列表中所有元素

Print (Lists[:4]) #显示下标为0-3 element, note that the last number is not in the Python value process

Print (List[::2]) #代表间隔俩个取一个数值

Print (Lists[1:5]) #代表取值下标1-5 element with subscript 5 Not on, please note

4. List member detection

If 3 in lists:

Print (' 3 in list ')

5. Sequence functions

Print (len (lists)) #答应列表的长度或者说列表有多少个元素

Print (max (lists)) #打印列表中最大的元素

Print (min (lists)) #打印列表中值最小的元素

Three. Traversal of the list

#定义几个列表

List_1 = [1,2,3,4,5,6,7,8,9]

List_2 = [

[1,2,3,4,5],

[' A ', ' B ', ' C ', ' d ', ' e ']

]

#进行列表的遍历

All elements of the #此次遍历得到列表list_1

For x in list_1:

Print (x)

#遍历列表list-2 of all elements, both methods

#第一种

For a, B in list_2:

Print (A, B)

#第二种

For x in list_2:

For y in x:

Print (y)

Four. List of commonly used function expressions

#定义一个本次操作所需列表nums

Nums = [' Guan Yu ', ' Zhang Fei ', ' Zhao Yun ', ' ma Teng ', ' Jack Huang ']

1.append () Add elements to the end of the list and modify the original list directly

EX:nums.append (' Xu Yu ')--the result shows an element added to the nums list: [' Guan Yu ', ' Zhang Fei ', ' Zhao Yun ', ' ma Teng ', ' Jack Huang ',' Xu Yu ']

2.insert () Add an element at the specified position in the list

EX:nums.insert (2, ' Code Wei ')--the result shows that the Nums list adds the ' Code Wei ' element to the Subscript 2:[' Guan Yu ', ' Zhang Fei ', 'Dian Wei ', ' Zhao Yun ', ' ma Teng ', ' Jack Huang ',  ' Xu Yu ']

3.pop () Deletes the value of the specified element in the list, and returns the deleted element

EX:nums.pop (2)--the result shows that the subscript 2 position element is removed, nums = [' Guan Yu ', ' Zhang Fei ', ' Zhao Yun ', ' ma Teng ', ' Jack Huang ',' Xu Yu ']

If Print (Nums.pop (2)) can get the value of the element deleted by the pop, you can use this value again

4.remove () removes the specified element from the list

EX:nums.remove (' Xu Yu ')--and operations Delete Xu Yu, the original list becomes nums = [' Guan Yu ', ' Zhang Fei ', ' Zhao Yun ', ' ma Teng ', ' Jack Huang ']

5.clear () empty list

EX:nums.clear ()--note that this operation will clear all the elements inside the list nums

6.copy () copy list

Ex:new_nums = Nums.copy ()--This action copies the list nums the elements inside the New_nums list to get a new list

7.count () counts the number of occurrences of an element in a list

EX:var.count (' Zhao Yun ')--this operation gets the number of times that ' Zhao Yun ' appears in the list nums

8.extend () Merge a list into another list

EX:NUMS_EX = Nums.extend ()-This action adds the values from the Nums list to the list NUMS_EX list, and the elements in the NUMS_EX list are the same as the elements in the Nums

9.index () to view the subscript of an element in a list

Ex:print (Nums.index (' Guan Yu '))---print element ' Guan Yu ' in the list of what is the subscript

Reverse operation of the 10.reverse () list

EX:var.reverse ()--and the operation represents the replacement of elements from the Nums list from head to toe, with the effect of: Nums = [' Jack Huang ',' ma Teng ', ' Zhao Yun ',' Zhang Fei ' ,' Guan Yu ')

11.sort () List sorting

Ex:1. Nums.sort () #默认将列表中的值升序, default sort parameter is False

2. Nums.sort (reverse = True) #将列表中的数值降序排列, reverse take the opposite value

3. Nums.sort (key= Lambda x:x%5) #自定义排序

Five. List-derived

#定义俩个列表

Nums = [1,2,3,4,5,6,7,8]

words = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ']

1. The most basic list derivation

Print ([' * ' + i + ' * ' for I in Nums])

2. List derivation with judging criteria

Print ([A for a in nums if a%2 = = 0])

3. Multi-loop List derivation

print ([Str (s) + '--+ ' + C for S "Nums for C in words])

4. Multi-loop list derivation with judging conditions

Print ([M + '---C for M in Nums for L in words if nums.index[m] = = Words.index[l]])

#注: This is the knowledge I encountered in the study, if the leakage of the wrong solution please point out, thank you very much! But do not like to spray, are to learn!!!

#或者有愿意交流者可邮件往来 ([email protected])

Python Learning--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.