Python built-in container (1)--list, tuple, collection

Source: Internet
Author: User
Tags set set

1. Columns Table List

1.1 Defining and initializing a list

# define an empty list

In [1]: Li = []

In [2]: Type (LI)

OUT[2]: List

# initialize elements in List,list without type requirements, can be of any type

In [3]: Li = [up, ' a ', [' a ', 4]]

In [4]: Li

OUT[4]: [1, 2, ' a ', [' a ', 4]]

1.2 Subscript of the list

The subscript for the list in Python starts at 0.

In [4]: Li

OUT[4]: [1, 2, ' a ', [' a ', 4]]

In [5]: li[2]

OUT[5]: ' A '

In [6]: li[4]

---------------------------------------------------------------------------

Indexerror

Traceback (most recent)

<ipython-input-6-5889debca501> in <module> ()

----> 1 Li[4]

Indexerror:list index out of range

# Get list length

In [7]: Len (LI)

OUT[7]: 4

1.3 in keyword

Use the representation in the for loop to traverse all elements in the list:

In [8]: for x in Li:

...:

Print X

...:

1

2

A

[' A ', 4]

In can also be used as a two-dollar operator to find out whether an element exists in the list:

In [9]: ' A ' in Li

OUT[9]: True

Del deletes an element from the list:

In [ten]: Li

OUT[10]: [1, 2, ' a ', [' a ', 4]]

In [all]: del li[0]

In []: Li

OUT[12]: [2, ' a ', [' a ', 4]]


1.4 In Python everything is an object, and it is clear that list is also an object that is commonly used for list objects:

in [+]: Dir (LI)

OUT[13]:

[' __add__ ',

' __class__ ',

.........

' __str__ ',

' __subclasshook__ ',

' Append ',

' Count ',

' Extend ',

' Index ',

' Insert ',

' Pop ',

' Remove ',

' Reverse ',

' Sort ']

1.4.1 to view the Help information for the list object Append method:

in [+]: Help (Li.append)

in [+]: Li

OUT[15]: [2, ' a ', [' a ', 4]]

in [+]: li.append (666)

in [+]: Li

OUT[17]: [2, ' a ', [' a ', 4], 666]

1.4.2 the Extend method, accepts an iterator that appends all the elements of the iterator to the list:

in [+]: Li

OUT[17]: [2, ' a ', [' a ', 4], 666]

In []: Li.extend ([' ABC ', ' CBA '])

in [+]: Li

OUT[19]: [2, ' a ', [' a ', 4], 666, ' abc ', ' CBA ']

1.4.3 Insert Method

Li.insert (2, ' X '), Li.insert ( -1, ' mm '), inserts a data before an index is subscript.

1.4.4 Remove method

Li.remove (' a ') to remove an element from the list.

1.4.5 Pop method

Accept an optional parameter index,remove the element pointed to by the subscript, and return the calcium element, Li.pop (), Li.pop (2):

in [+]: Li

OUT[19]: [2, ' a ', [' a ', 4], 666, ' abc ', ' CBA ']in []: Li.pop ()

OUT[20]: ' CBA '

in [+]: Li

OUT[21]: [2, ' a ', [' a ', 4], 666, ' abc ']

In []: Li.pop (2)

OUT[22]: [' A ', 4]

In [P]: Li

OUT[23]: [2, ' a ', 666, ' abc ']

1.4.6 Count Method

Returns the number of times a value appears in the list, Li.count (' a ')

1.4.7 The index method, returns the first subscript that matches the value:

in [+]: Li

OUT[28]: [2, ' a ', 666]

In []: Li.index (' a ')

OUT[29]: 1

in [+]: Li.index (' B ')

---------------------------------------------------------------------------

ValueError

Traceback (most recent)

<ipython-input-30-4639cb7d3bae> in <module> ()

----> 1 li.index (' B ')

ValueError: ' B ' isn't in list

# You can also specify the range parameters for start and end lookups Li.index (' A ', 2,6)

in [+]: Li

OUT[33]: [2, ' a ', 666, ' A ', ' a ', ' B ']

In [the]: Li.index (' A ', 2)

OUT[34]: 3

1.4.8 Sort Method

Directly modifies the list contents without returning a value. Li.sort (), you can also accept three optional parameters. L.sort (Cmp=none, Key=none, Reverse=false)

1.4.9 Reverse Method . Li.reverse ()


1.5 List of slices

li[i],li[a:b],li[a:e:b]--The third parameter is step, index increments each time, the default is 1

In [max]: Li

OUT[40]: [' B ', ' A ', ' a ', ' a ', 666, 2]

in [inch]: Li[2:4]

OUT[41]: [' A ', ' a ']

####

In []: Li[:2]

OUT[43]: [' B ', ' a ']

###

In []: li[4:]

OUT[44]: [666, 2]

# # Make a deep copy of the list

In []: li[:]

OUT[45]: [' B ', ' A ', ' a ', ' a ', 666, 2]

####

In []: Li[2:4:2]

OUT[46]: [' a ']

# # using slices for list flipping

In [P]: Li

OUT[47]: [' B ', ' A ', ' a ', ' a ', 666, 2]

In []: Li[::-1]

OUT[48]: [2, 666, ' A ', ' a ', ' a ', ' B ']

# get the list labeled as even

In []: Li

OUT[50]: [' B ', ' A ', ' a ', ' a ', 666, 2]

In [Wuyi]: Li[::2]

OUT[51]: [' B ', ' A ', 666]

# Get the values that are labeled as odd

In []: Li[1::2]

OUT[52]: [' A ', ' a ', 2] the slice operation of the list is a copy operation and does not modify the original list.


2. Tuple tuples

2.1 Defining and initializing tuples

In [1]: T = ()

In [2]: type (t)

OUT[2]: Tuple

In [3]: T = (all in all)

In [4]: t

OUT[4]: (1, 2, 3)

The 2.2-tuple also supports subscript and slicing operations.

Tuples are immutable objects and cannot be modified for Ganso content

In [4]: t

OUT[4]: (1, 2, 3)

In [5]: t[1]

OUT[5]: 2

In [6]: t[1] = 100

---------------------------------------------------------------------------

TypeError

Traceback (most recent)

<ipython-input-6-4f066cd5e53f> in <module> ()

----> 1 t[1] = 100

TypeError: ' Tuple ' object does not support item assignment

The 2.3-tuple immutable is relative, because the contents of the meta-ancestor can be of various types, such as the tuple's element value as a list:

In [7]: T1 = ([2,3,4],[19,23,45])

In [8]: T1

OUT[8]: ([2, 3, 4], [19, 23, 45])

In [9]: t1[0][2]

OUT[9]: 4

In []: t1[0][2] = the value of #list is variable and can be modified

In [all]: T1

OUT[11]: ([2, 3, 100], [19, 23, 45])

2.4 Count, Index action

in []: t

OUT[12]: (1, 2, 3)

in [+]: T.count (2)

OUT[13]: 1

In []: T.index (3)

OUT[14]: 2

the tuple support slicing operation is the same as the slice operation of the list .


3. Set Set

3.1 Defining and initializing collections

Lists and tuples are ordered, but collections are unordered.

in [+]: s = {-)

in [+]: type (s)

OUT[18]: Set

3.2 Elements in the set are not duplicated

in [+]: s = {1,1,1,12,2,2,3,4}in []: s

OUT[20]: {1, 2, 3, 4, 12}


3.3python Determines whether duplicates are based on the hash value of each element in the collection, so each element in the collection must be a hash object. In Python, if an object has a __hash__ method,

Indicates that the object can be hashed.

in [+]: 1.__hash__ # integer 1 object has this method, but the __hash__ method cannot be called directly.

1.__hash__

The hash () function returns the hash value of an object directly. such as hash (1)

The collection does not support slicing operations.

3.4 Integrated Operation:

3.4.1 Add operation

in [+]: s

OUT[21]: {1, 2, 3, 4, 12}

In []: S.add (100)

In [all]: s

OUT[23]: {1, 2, 3, 4, 12, 100}

3.4.2 Update operation, iterator as parameter, appends all elements in the iterator to the collection

In [all]: s

OUT[23]: {1, 2, 3, 4, 12, 100}

In []: S.update ([101,102,103])

in [+]: s

OUT[25]: {1, 2, 3, 4, 12, 100, 101, 102, 103}

3.4.3 Remove an element if the element does not exist the error

3.4.4 Discard deletes an element and does nothing if the element does not exist.

3.4.5 pop () randomly deletes an element and returns the element

3.4.6 clear clears the collection. S.clear ()


3.5 operation of the set

3.5.1 Difference two sets of differences, does not modify the original two sets

in [+]: S1 = {1,2,3,4,5, ' a '}

in [+]: S2 = {4,5,7,8, ' B ', ' C '}

In []: S1.diff

S1.difference

S1.difference_update

In []: s1.difference (S2)

OUT[28]: {1, 2, 3, ' a '}

# minus sign can directly find the difference set of two sets

In []: S1-S2

OUT[29]: {1, 2, 3, ' a '}

3.5.2 Difference_update Two sets of differences, but modifies the original collection, does not return a value

in [+]: S1

OUT[30]: {1, 2, 3, 4, 5, ' a '}

In [to]: S2

OUT[31]: {4, 5, 7, 8, ' B ', ' C '}

in [+]: s1.difference_update (S2)

in [[]: S1

OUT[33]: {1, 2, 3, ' a '}

In [S2]:

OUT[34]: {4, 5, 7, 8, ' B ', ' C '}intersection two sets of intersection, return value, do not modify the original collection

3.5.3 Intersection_update two sets of intersection, no return value, modify the original collection

3.5.4 &, S1 & S2 Direct intersection of two sets

3.5.5 Union of two assemblies

3.5.6 | , S1 | S2 is also a collection of two sets

3.5.7 Isdisjoint See if two collections have intersections, return bool, s1.isdisjoint (S2)

3.5.8 Issubset to see if it is a subset


4. Conversions between lists, tuples, and collections

4.1 List function

in [+]: List ()

OUT[35]: []

in [+]: list (' Hello ')

OUT[36]: [' h ', ' e ', ' l ', ' l ', ' O ']

4.2 Tuple function

4.3 Set function

In [PNS]: t = (All-in-all)

in [+]: type (t)

OUT[38]: Tuple

In [All]: List (t)

OUT[39]: [1, 2, 3]

When a list is converted to set, duplicate elements are removed if there are duplicates.


This article from the "Technology life, Simple not simple" blog, please be sure to keep this source http://willis.blog.51cto.com/11907152/1854915

Python built-in container (1)--list, tuple, collection

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.