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# return value is 3# return value is#  return value is

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  Span style= "COLOR: #800000" > " "  >>> 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
[Up, ' 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',' is','She',' to']>>> X.remove (' to')>>>x['my',' is','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< c7>]>>> y[1, 2, 3, 4, 6, 7, 8]>>> x=[3,2,1]>>> 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)# empty tuples ()>>> 1, syntaxerror:invalid syntax>>> 1, (1,)#  Ganso must have commas (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

List and tuples 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.