Python Automation 3.0-------Learning path-------List

Source: Internet
Author: User
Tags python list

You can think of a list in Python as a sequence of arbitrary pairs of images, as long as you put the required parameter values into brackets [], just like this:

names = [' Ada ', ' Amy ', ' Ella ', ' Sandy ']

The list can contain different types of images, and nesting is also supported:

For example a = [' A ', 567,[' ADC ', 4,], (on)]

This list contains the elements of strings, integers, tuples, and a list of nested lists.

To modify a value in a list of lists

The list is ordered and can be modified by the Python list subscript to modify the value at a particular location. The following is an example of how to modify a list parameter, as described below:
>>>a = [1,9,9]
>>>a [0] = 9
>>>a
[9,9,9]
The modification of a list can also be viewed as an operation that assigns a value to a specific location.

Python List Delete operation

python list deletion the most commonly used methods are three: Del, remove, pop, and the use of methods and uses are not the same, here first understand the next del this is the most convenient entry-level list delete operation method.

The existing List names = [' Ada ', ' Amy ', ' Ella ', ' Sandy '], the requirement is to remove the above list of ' Amy ', the idea is: first know ' Amy ' in the list names in the index position, followed by Del this method to delete. The list del methods are used in the following ways:

>>>names = [' Ada ', ' Amy ', ' Ella ', ' Sandy ']
>>>del Names[1]
>>>names
[' Ada ', ' Ella ', ' Sandy ']

>>> list = [] #定义一个空列表
>>> list.append (1) #向列表中添加成员
>>> List.count (2) #计算2在列表中出现的次数
0
>>> List.extend ([2, 3, 5, 4]) #向列表中添加一个列表
>>> List #列表值被改变
[1, 2, 3, 5, 4]
>>> List.index (5) #获得5在列表中的位置
3
>>> List.insert (2, 6) #从0开始, that is, insert 6 in 3rd member, and the other members move back in the first place
>>> List
[1, 2, 6, 3, 5, 4]
>>> List.pop (2) #删除列表中第3个成员
6
>>> List
[1, 2, 3, 5, 4]
>>> List.remove (5) #删除列表中的5
>>> List
[1, 2, 3, 4]
>>> List.reverse () #颠倒列表的顺序
>>> List
[4, 3, 2, 1]
>>> List.sort () #将列表中的成员重新排序
>>> List
[1, 2, 3, 4]

The second piece of code:

#创建一个初始有两个字符串和一个整数的列表

>>> book = ["Python", "development", 8]

#在列表尾部添加另一个整数
>>> Book.append (2008)

#在第二个位置上插入一个字符串 (subscript 1)
>>> Book.insert (1, "web")
>>> Book
[' Python ', ' web ', ' development ', 8, 2008]

#获取头三个元素的一个切片
>>> book[: 3]
[' Python ', ' web ', ' development ']

#成员检查
>>> "Django" in the book
False

#无论元素的位置 to remove it from the list. (explicitly remove an object)
>>> Book.remove (8)

#根据位置 (i.e. subscript) remove (and return) an element
>>> Book.pop (-1)
2008
>>> Book
[' Python ', ' web ', ' development ']

How to use #展示复制操作符 *
>>> Book * 2
[' Python ', ' web ', ' development ', ' python ', ' web ', ' development ']

#用另一个列表扩展本列表
>>> Book.extend (["with", "Django"])
>>> Book
[' Python ', ' web ', ' development ', ' with ', ' Django ']

Common list Action methods

List.append (): Append member

List.count (x): Calculates the number of occurrences of a parameter X in a list

List.extend (L): Append another list to the list L

List.index (x): Gets the position of the parameter X in the list

List.insert (): Inserting data into the list

List.pop (): Remove a member from the list (deleted by subscript)

List.remove (): Remove a member from the list (delete directly)

List.reverse (): Reverses the order of the members in the list

List.sort (): Sort members in a list

Python Automation 3.0-------Learning path-------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.