Detailed list and tuples in Python

Source: Internet
Author: User
Common operations: Index, Shard, add, multiply, check whether an element belongs to a member of a sequence, length, minimum, maximum

Example:

NUMBERS=[100,34,67]
Len (numbers) #返回值为3max (numbers) #返回值为100min (numbers) #返回值为34

List :

1. List function

>>>list (' Hello ') [' H ', ' e ', ' l ', ' l ', ' O ']

* You can use join (for example: ". Join (Somelist)) to convert a list of components into a string

2, the operation of the list

Element Assignment Value:

>>>x=[1,2,3]>>>x[1]=4>>>x[1,4,3]

To delete an element:

>>>x=[' abc ', ' Def ', ' Ghi ', ' JK ']>>>del x[2]>>>x[' abc ', ' Def ', ' JK ']

Shard Assignment:

>>>x=list (' Happy ') >>>name[' H ', ' a ', ' P ', ' P ', ' Y ']>>>x[1:]=list (' ello ') >>>x[' H ', ' e ', ' l ', ' l ', ' O ']

3. List method:

Append

>>>x=[1,2,3]>>>x.append (4) >>>x[1,2,3,4]

Count

>>>x=[1,2,3,1,3,5,5]>>>x.count (3) 2

Extend

>>>a=[1,1]>>>b=[2,3]>>>a.extend (b) >>>a[1,1,2,3]

Index

>>>x=[' A ', ' B ', ' C ']>>>x.index (' B ') 1

Insert Insertion Element

>>>x=[1,2,3]>>>x.insert (2, ' a ') >>>x[1,2, ' A ', 3]

Pop removes the list element (the last one by default)

>>> x=[1,2,3,4]>>> x.pop () 4>>> x.pop (1) 2>>> x[1, 3]

Remove removes the first occurrence of a value in the list

>>> x=[' my ', ' to ', ' was ', ' she ', ' to ']>>> x.remove (' to ') >>> x[' My ', ' was ', ' she ', ' to ']

Reverse to store elements in the list in reverse

>>> x=[1,2,3]>>> x.reverse () >>> x[3, 2, 1]
>>> x=[1,2,3]>>> List (reversed (x)) [3, 2, 1]>>> x[1, 2, 3]

Sort for sorting the list at the original location

>>> x=[4,6,2,1,7,8,3]>>> x.sort () >>> x[1, 2, 3, 4, 6, 7, 8]>>> x=[4,6,2,1,7,8,3]> >> y=sorted (x) >>> x[4, 6, 2, 1, 7, 8, 3]>>> y[1, 2, 3, 4, 6, 7, 8]>>> X=[3,2,1]>>&G T Y=x>>> y.sort () >>> x[1, 2, 3]>>> y[1, 2, 3]
  >>> sorted (' hello ')  [' E ', ' h ', ' l ', ' l ', ' O ']

Tuples : Non-changing sequences

>>> (1, 2, 3) >>> (1, 2, 3) >>> () #空元组 () >>> 1,syntaxerror:invalid syntax& Gt;>> 1, (1,) >>> 1, #元祖必须有逗号 (1,) >>> (40+2,) (42, 42, 42)

The tuple function transforms a sequence into a narimoto group

>>> tuple ([3,2,1]) (3, 2, 1) >>> tuple (' abc ') (' A ', ' B ', ' C ')

Reasons for the non-substitution of tuples

    • Tuples can be used as keys in the map

    • Tuples exist as return values for many of the built-in functions and methods

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.