List, Ganso, dictionary in Python

Source: Internet
Author: User

First, List

A set of ordered items. Variable data type "can be added and censored"

A list is a collection of data surrounded by square brackets "[]", with different members separated by ",".

The list can include no matter what the data type, but also includes a list

The list can be visited by serial number members

Use list operations frequently:

List.append () Append member. Member data

List.pop () Delete Members, delete member I

List.count (x) count the number of occurrences in the list

List.remove () removes a member from the list. Delete Member I directly

List.extend (l) Append a list to the list L

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

List.index (x) Get the number of references x position in the list

List.sort () sort the members in the list

List.insert () inserting data into the list insert (A, B) inserts data into the list

Available through

>>>help (list) View method details


Two, the meta-group

Immutable sequences

A tuple is a collection of data surrounded by parentheses "()", with different members separated by ","

Unlike lists: the data in a tuple cannot be changed once it is established


Interview by Subscript

Statement:

L= (a)

Tuples with 0 elements: L = ()

Tuples with 1 elements: l= (1,) note a comma


Third, the dictionary

A collection of key-value pairs (map)

A dictionary is a collection of data surrounded by curly braces "{}"

differs from list: Dictionaries are unordered, and access members by key in the dictionary.

Dictionaries are mutable and can include whatever other type

Statement:

M={K1:V1,K2:V2}

Interview M[k1] will get V1


Use dictionary operations frequently:

Dic.clear () Empty dictionary

Dic.keys () Get a list of keys

Dic.values () Get a list of values

Dic.copy () Copy Dictionary

Dic.pop (k) Delete key k

Dic.get (k) Gets the value of the key K

Dic.update () Update member, if member does not exist, equals increase

Dic.items () Gets a list of keys and values

Dic.popitem

To pass

>>>help (dict) View details

Iv. indexing and slicing of sequences

Lists, tuples, and strings are sequences

The two main features of a sequence are index operators and slice operators.

The index operator allows us to fetch a specific item from the sequence. The subscript operation, using square brackets and numbers to grab a position of the item, L[0] Grab the first element, L[-1] grab the last element

The slice operator allows us to get a slice of the sequence. That is part of the sequence.

Square brackets plus a pair of optional digits. Cut out a sub-slice L[1:3] returns 1-2 of the child slices. L[2:] From 2 to the end, l[:] the whole

List, Ganso, dictionary in Python

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.