List of Python3 and Python3

Source: Internet
Author: User

List of Python3 and Python3

1: List:

Python lists are much more powerful than C-language arrays. Arrays can only store the same type of data, while lists are like a large container that can store shaping, float, String, object, etc.

2: Create a list

# Create a common list1 = [1, 2, 3, 4, 5] # create an empty list empty = [] # create a hybrid list list2 = ['electronic', '123 ', [1, 2, 'info'], 'ss']

3: add an element to the list

1) append () adds a parameter to the end of the list.

Member = ['zookeeper ', 'zookeeper'] member. append ('ally') member ['zookeeper ', 'zookeeper', 'ally']

2) The extend () parameter is a list that extends the original list from the original list.

Member = ['zookeeper ', 'zookeeper'] member. extend ([1314, 'together ']) member ['zookeeper', 'zookeeper ', 1314, 'together']

3) insert (): the first parameter represents the location of the List, and the second parameter represents the element to be inserted.

Member = ['invalid reference', 'invalid reference'] member. insert (1, 'love') member ['invalid reference', 'love', 'invalid reference']

4) Comparison between append () and extend ()

>>> name = ['F', 'i', 's', 'h']>>> name.append('C')>>> name['F', 'i', 's', 'h', 'C']>>> name.extend(['.', 'c'])>>> name['F', 'i', 's', 'h', 'C', '.', 'c']>>> name.append(['o', 'm'])>>> name['F', 'i', 's', 'h', 'C', '.', 'c', ['o', 'm']]

Have you understood this example?

4: Method for retrieving elements from the list

Like arrays, you can use member [0] to change the index value to obtain the corresponding element.

>>> Member = ['zookeeper ', 'zookeeper'] >>> member [0] 'zookeeper '>>> member [1] 'zookeeper'

5. Method for deleting elements from the list

1) The remove () parameter is the name in the known list. You only need to know the name.

>>> List2 = ['4. adad', '3. li Ning ',' 2. kapa', '1. nike '] >>> list2.remove (' 3. li Ning ') >>> list2 [' 4. adads', '2. kapa', '1. nike ']

2) del statement

>>> List2 = ['4. adad', '3. li Ning ',' 2. kapa', '1. nike '] >>> del list2 [0] >>> list2 ['3. li Ning ',' 2. kapa', '1. nike ']

3) pop () stack, pull the last element, and return it to you.

>>> List2 = ['4. adad', '3. li Ning ',' 2. kapa', '1. nike ']> list2.pop ()' 1. nike'
# Retrieve an element from the list and insert it to the front of list2 = ['4. adad', '3. li Ning ',' 2. kapa', '1. nike '] list2.insert (0, list2.pop ()> list2 ['1. nike ', '4. adad', '3. li Ning ',' 2. kapa']

6: slice of the list to get another list

>>> List1 = [9,65, 34,13, 67,9, 45,12] >>> list1 [0: 3] [9, 65, 34] >>> list1 = [9,65, 34,13, 67,9, 45, 12] >>> list2 = list1 [:] >>> list2 [9, 65, 34, 13, 67, 9, 45, 12] # note that list2 is a new list. If list3 = list1list3 is not a new list, IT and list1 only point to the same position, for example
# There is also a hidden value step for list parts. The default step size cannot be 0. The value range is 1 >>> list1 = [9, 65, 34,13, 67,9, 45,12] >>> list1 [:: 2] [9, 34, 67, 45]

7: List Operators

1) when the comparison operator has multiple elements, it starts to compare from 0th.

list1=[1,8,3]list2=[4,5,6]>>> list1>list2False>>> list1<list2True

2) logical operators

list1=[1,8,3]list2=[4,5,6]>>> list1<list2 and list1>list2False

3) join Operators

List1 = [, 3] list2 = [, 6]> list1 + list2 [1, 8, 3, 4, 5, 6] # You can use '+' only when the two lists are of the same type. If you want to add an element, use the first three methods.

4) repeated operator '*'

list1=[1,8,3]>>> list1*2[1, 8, 3, 1, 8, 3]

5) in

>>> Member = ['1. Nike ', '4. adads', '3. Li Ning', '2. kapa'] >>> '1. Nike 'in memberTrue
>>> Member = [[1, 2, 3, ['abc'], '1. nike ', '4. adad', '3. li Ning ',' 2. kapa'] >>> 'abc' in member [0] False >>> 'abc' in member [0] [3] True

>>> Member = [[1, 2, 3, ['abc', ['a'], '1. nike ', '4. adad', '3. li Ning ',' 2. kapa']
>>> A in member [0] [3] [1]

True

 

If the list is in the list, you must first include a level

8: list of good friends (list type built-in functions)

1) view Method

>>> dir(list)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

 

2) regular friends (commonly used built-in functions)

Count () calculates the number of times the list element repeats.

>>> member=[1,1,1,1,1,1,1,2]>>> member.count(1)7

Index () returns the position in the list of elements.

>>> member=[1,2,3,9,23,34,6,6,7]>>> member.index(1)0>>> member=[1,2,3,9,23,34,6,6,7]>>> member.index(9,2,7)3

Reverse the list

>>> list1=[1,2,3,4]>>> list1.reverse()>>> list1[4, 3, 2, 1]

By default, sort () is sorted from small to large, and sorted by merge.

>>> list1=[9,65,34,13,67,9,45,12]>>> list1.sort()>>> list1[9, 9, 12, 13, 34, 45, 65, 67]

How can we sort data in ascending order?

>>> List name. sort () >>> list name. reverse () # Or >>> list name. sort (reverse = True)

Clear () clear the list

>>> list2.clear()>>> list2[]

Copy () to get a new list

>>> List1 = [1, [1, 2, ['boundary '], 3, 5, 8, 13, 18] >>> list2 = list1.copy () >>> list2 [1, [1, 2, [CAPTCHA], 3, 5, 8, 13, 18]

9: List Derivation

>>> List1 = [(x, y) for x in range (10) for y in range (10) if x % 2 = 0 if y % 2! = 0] list1 = [] for x in range (10): for y in range (10): if x % 2 = 0: if y % 2! = 0: list1.append (x, y) connected # connected game list1 = ['1. jost do it ', '2. you ',' 3. can ',' 4. change the word'] list2 = ['4. adad', '3. li Ning ',' 2. kapa', '1. nike '] >>> list3 = [name +': '+ slogan [2:] for slogan in list1 for name in list2 if slogan [0] = name [0]

 

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.