(Self-developed artificial intelligence) pythony list, artificial intelligence pythony

Source: Internet
Author: User

(Self-developed artificial intelligence) pythony list, artificial intelligence pythony

How to Create a list:

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

In this way, the elements in the element enclosed by brackets are separated by commas. This form represents a list.

Common Operations in the list:

1. Element assignment:

A [0] = 10

Add a 10 value at the position where the index of the List is 0 and overwrite the original 1.

Result: a = [10, 2, 3, 4, 5]

2. Add the following elements:

A. append (6)

Here we use the append () method to add a new object at the end of the list.

Result: a = [,]

3. Delete elements:

Del a [0]

Here, we use del to delete the element whose index is 0.

Result: a = [2, 3, 4, 5, 6]

Common methods in the list:

1. append:

A. append (7) # Add a new object to the end of the list

 

2. count:

A. count (2) # count the number of times 2 appears in List

 

3. pop:

A. pop () # remove the last element from the list by default, and return the value of this element. a. pop (3) # Remove the element from the specified position and return the value of this element.

 

4. insert:

A. insert () # insert 10 at the position where the index value is 2. Add 1 to the element on the original index 2 and the element index after the index.

 

5. remove:

A. remove (10) # remove the first match from index 0

 

6. sort:

A. sort () # sort in ascending order by default

 

Use the reverse parameter:

A. sort (reverse = True) # sort in reverse order

 

The string can use key = len:

A. sore (key = len) # sort by string from short to long a. sore (key = len, reverse = True) # sort by string from long to short

 

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.