List in Python 3

Source: Internet
Author: User

Some common operators of the list

1. Comparison operators

When there are multiple elements in the list, only the No. 0 element is compared between the lists.

>>> List1 = [1,2]>>> list2 = [2,1]>>> list1 > List2false

2. Logical operators

>>> List1 = [1,2]>>> List2 = [2,1]>>> list3 = [1,2]>>> (List1 < List2) and (List3 & Lt LIST2) True

3. Connection operator

+ Connect operator when acting on list, can only connect the same type of data, List + list, integer + integer, etc.

>>> List1 + list2[1, 2, 2, 1]

4. Repeat operator

* When acting on a list as a repeating operator, you can copy the list to the appropriate number of times.

>>> list1[1, 2]>>> list1 * 2 #将list1复制了两次 [1, 2, 1, 2]>>> list1 *= 2    #可以结合赋值操作符快速完成赋值 >&G T;> list1[1, 2, 1, 2]

  

5. Member Relationship Operators

>>> List1 = [1,2]>>> 1 in list1    #利用成员关系操作符in判断某个元素是否在列表中True >>> 3 in list1false>>& Gt List2 = [[1,2],3,4]>>> [up] in List2    #in只能判断一个层次的成员关系True >>> 1 in List2    #如果是列表中的列表的元素, In there is no way to Judge False>>> 1 in list2[0] #指定层级就能正确判断到了True

Ii. bif of some lists (built-in functions)

1.count function to calculate the number of occurrences of a parameter in the list.

>>> list4 = [1,2,3,4,4,4,5,5,6,6,6,6]>>> list4.count (4) 3

  

2.index function that lists the index values of the parameters in the list.

>>> list4 = [1,2,3,4,4,4,5,5,6,6,6,6]>>> list4.index (5)    #如果相同的元素有多个, lists only the index value of the first occurrence of the element 6 > >> List4.index (5,7,9) #index后面还有两个参数用来确定范围, here to find 7 #在第7 between elements in the 7th to 9th element, 5 first occurrence is at the index value 7 position, so return 7

  

3.reverse function to flip the list.

>>> list4 = [1,2,3,4,4,4,5,5,6,6,6,6]>>> list4.reverse () >>> list4[6, 6, 6, 6, 5, 5, 4, 4, 4, 3, 2, 1]

  

4.sort function that sorts the elements of the list in the specified way.

>>> list5 = [8,2,1,7,6,4,9,3,5]>>> list5.sort () #参数为空时默认升序排列 >>> list5[1, 2, 3, 4, 5, 6, 7, 8, 9]>>> List5.sort (reverse=true) #reverse =false is the default parameter, which indicates ascending (not descending) arrangement, where false is changed to Ttue and descending >>> list5[9 , 8, 7, 6, 5, 4, 3, 2, 1]

TIPS: Notice the meaning of the value of the assignment symbol in Python

Example by list

>>> List6 = [3,1,4,9,5]>>> list7 = list6[:]    #利用切片方式取出的数据, equivalent to the full copy of the original list >>> list8 = List6    #用赋值符号, equivalent to giving the original variable a new name >>> List6.sort (reverse=true) >>> list6[9, 5, 4, 3, 1]>>> list7    #当原变量的值发生变化时, the copied variable values do not change [3, 1, 4, 9, 5]>>> list8    #新名称指向的值会变化 (because List6 and list8 actually point to the same value) [9, 5, 4, 3, 1]
#在编写程序时, pay attention to the difference.

  

  

List in Python 3

Related Article

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.