Common methods for Python action lists sharing _python

Source: Internet
Author: User

The following list of method actions commonly used in the list as well as small examples:

1. Append
To add elements to the end of the list, add elements to the end of the list, and you need to be aware of several points:
The parameters added in a. Append are as a whole

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.append (List ("Tiger"))
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', [', ' t ', ' I ', ' g ', ' e ', ' R ']]

The resulting values are not: [' s ', ' C ', ' o ', ' t ', ' t ', ', ', ' t ', ' I ', ' g ', ' e ', ' R ']
If you want this type of append, try a slice assignment (or extend method below):

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name[len (name):] = List ("Tiger") #从末尾追加
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', ', ', ' t ', ' I ', ' g ', ' e ', ' R ']

B.append can only add one element at a one-time

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.append ("A", "B") #添加多个元素即将报错
Traceback (most recent call last):
File "<stdin>", line 1, in?
Typeerror:append () takes exactly one argument (2 given)
>>> name.append ("A")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', ' A ']

2. Count

Count the number of times an element appears in the list

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.count (' s ')
1
>>> name.count ("T")
2
>>> name.count ("A")
0
>>> name.append (List ("Python")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', [' P ', ' y ', ' t ', ' h ', ' o ', ' n ']]
>>> name.count ([' P ', ' y ', ' t ', ' h ', ' o ', ' n '])
1

3. Extend

Append multiple values from another sequence in the original list

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.extend (List ("Tiger"))
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', ', ', ' t ', ' I ', ' g ', ' e ', ' R ']

Of course, we can use piecewise assignment to achieve:

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name[len (name):] = List ("Tiger")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', ', ', ' t ', ' I ', ' g ', ' e ', ' R ']

At this time the small partners will think, we can directly use the operator "+" well, but also more convenient:

Copy Code code as follows:

>>> name = List ("Scott")
>>> pwd = list ("Tiger")
>>> name + pwd
[' s ', ' C ', ' o ', ' t ', ' t ', ', ', ' t ', ' I ', ' g ', ' e ', ' R ']
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']

The output from the three-way operation can be seen:
Extend and piecewise assignment is to modify the original list, relatively, extend readability, while the operator "+" is to generate a new list, does not affect the original list, if
We need to generate a new list without affecting the original list, we can use the operator "+".

4.Index
Find the index position of the first (note first) occurrence of a value from the list

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.index (' t ') # #第一个字母t的索引位置是3
3
>>> Name.index (' a ')
Traceback (most recent call last):
File "<stdin>", line 1, in?
ValueError:list.index (x): X not in List
>>> ' a ' in name
False
>>> ' A ' not in name
True

As you can see from the output, index is looking for the index of the first match, and if the element you are looking for is not in the list, it will be an error (return-1 Would it be better?), of course, if you want to avoid reporting
Wrong, we can first use in operation, to determine whether an element is in a list, if it is, then the index operation.

5. Insert
Used to insert an object into a list, two arguments, the first is the index position, and the second inserted element object.

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name.insert (2, ' Tiger ') # #在索引为2的地方插入字符串tiger
>>> Name
[' s ', ' C ', ' Tiger ', ' o ', ' t ', ' t ']

We can also use a fragment to assign a value:

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name[2:2] = [' Tiger ']
>>> Name
[' s ', ' C ', ' Tiger ', ' o ', ' t ', ' t ']
>>> Name[2:2] = ' Tiger '
>>> Name
[' s ', ' C ', ' t ', ' I ', ' g ', ' e ', ' r ', ' Tiger ', ' o ', ' t ', ' t ']

Note here that if you insert an element, you need to enclose it in [], otherwise, the string is inserted directly into the list of strings, added after the index position.
Of course, the readability score of the insert is strongly assigned.

6. Pop
Removes an element (the last element) from the list and returns the value of the element

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name.pop ()
' t '
>>> Name
[' s ', ' C ', ' o ', ' t ']
>>> name.append ("T")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']

Piecewise Assignment Simulation POPs:
Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name[len (name)-1:] = []
>>> Name
[' s ', ' C ', ' o ', ' t ']

This uses pop and append to simulate the stack's advanced first out LIFO.

7. Remove
Removes the first occurrence of a value in a list: If you have two equal elements, you simply remove the matching element, and if an element does not exist in a list, the error occurs, and the one-time only
Removes an element.

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.remove ("T") #去掉第一个t
>>> Name
[' s ', ' C ', ' o ', ' t ']
>>> name.remove ("A") #不存在会报错
Traceback (most recent call last):
File "<stdin>", line 1, in?
ValueError:list.remove (x): X not in List
>>> "A" not in name
True
>>> Name.remove ("s", "C") #一次只能移除一个元素
Traceback (most recent call last):
File "<stdin>", line 1, in?
Typeerror:remove () takes exactly one argument (2 given)

8.Revense

Reverse an element in a list

Copy Code code as follows:

>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name.reverse ()
>>> Name
[' t ', ' t ', ' o ', ' C ', ' s ']

9. Sort & Sorted

The sort method is used to sort the list, modify the original list, and do not return a sorted copy of the list

Copy Code code as follows:

>>> result = [8,5,5,3,9]
>>> Result.sort ()
>>> result
[3, 5, 5, 8, 9]

If we want to return a sorted copy of the list without affecting the original list, one way, we can first assign the original list (which can be copied with a fragment assignment), and then
To do a sort operation on the copied list, the other way is to use the sorted function, which returns the sorted list copy:

Copy Code code as follows:

>>> result = [8,5,5,3,9]
>>> result2 = sorted (Result)
>>> result
[8, 5, 5, 3, 9]
>>> RESULT2
[3, 5, 5, 8, 9]

On the custom comparison method, like JavaScript to do sort can pass in the Compare function, Java can pass in comparable<t> instance, Python is similar, left to follow up ~ (@ ^_^ @) ~.

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.