Python Learning notes-built-in data structures

Source: Internet
Author: User

1, List--is a sequence, for sequential storage of data

Definition and initialization of a list

In [374]: LST = list () in [375]: lstout[375]: []in [376]: LST = []in [377]: LST = [1,2,3]in [378]: lstout[378]: [1, 2, 3]in [379]: LST = list (range (1,10)) in [380]: lstout[380]: [1, 2, 3, 4, 5, 6, 7, 8, 9]


Accessing list elements

in [380]: lstout[380]: [1, 2, 3, 4,  5, 6, 7, 8, 9]in [381]: in [381]: in [381]: lst[0]out[ 381]: 1in [382]: lst[-1]out[382]: 9in [383]: lst.index (5) Out[383]: 4In  [384]: in [384]: lst = [1,2,3,2,4,3,5]in [385]: lst.index (2)   The    # index  method returns the first index found Out[385]: 1in [386]: lst.index (2,2) out[386]:  3in [387]: lst.index (2,2,5) out[387]: 3in [388]: lst.index (2,-4,-1) Out[388]:  3in [389]: lst.count (2) Out[389]: 2in [390]: lst.count (3) Out[390]: 2 
    • Accessing elements by index

    • The index method returns the first index based on a value

    • The Count method returns the number of elements in the list


Modification of list elements

In [391]: lstout[391]: [1, 2, 3, 2, 4, 3, 5]in [392]: lst[2]out[392]: 3In [393]: lst[2] = 5In [394]: lstout[394]: [1, 2, 5 , 2, 4, 3, 5]in [395]: lst[2] out[395]: 5


Increase of list elements

in [396]: lstout[396]: [1, 2, 5, 2, 4, 3, 5]in [397]:  Lst.append (in [398]: lstout[398]: [1, 2, 5, 2, 4, 3, 5, ) 19]in [399]: lst.insert (0,20) in [400]: lstout[400]: [20, 1, 2, 5,  2, 4, 3, 5, 19]in [401]: lst.insert ( -1, ' B ') in [402]: lstout[402] : [20, 1, 2, 5, 2, 4, 3, 5,  ' B ', 19]in [403]:  Lst.insert (+, ' a ') in [404]: lstout[404]: [20, 1, 2, 5, 2, 4, 3,  5,  ' B ', 19,  ' A ']in [405]: lst.insert ( -100, ' a ') in [406]: lstout[406]:  [' A ', 20, 1, 2, 5, 2, 4, 3, 5,  ' B ', 19,  ' a ']In  [407]:  #insert   When an index is out of range, the index is negative   It is inserted before the No. 0 element, the index is positive   It is inserted after the last element in [407]:  Lst.extend ([i]) in [408]:  lstout[408]: [' A ', 20, 1, 2, 5, 2, 4, 3, 5,  ' B ',  19,  ' A ',  1, 2, 3] #append操作单个元素, extend operations iterate objects


Deletion of list elements

In [410]: lstout[410]: [1, 2, 3, 2, 4, 3, 5, 3, 4]in [411]: Lst.remove (1) in [412]: lstout[412]: [2, 3, 2, 4, 3, 5, 3, 4]in [413]: Lst.pop () out[413]: 4In [414]: lstout[414]: [2, 3, 2, 4, 3, 5, 3]in [415]: Lst.pop (4) out[415]: 3In [416]: lstout[41 6]: [2, 3, 2, 4, 5, 3]in [417]: Lst.clear () in [418]: lstout[418]: []


Other operations

In [419]: lst = list (Range (4)) in [420]: lstout[420]: [0, 1, 2,  3]in [421]: len (LST) out[421]: 4in [422]: in [422]: lstout[422]:  [0, 1, 2, 3] In [423]: lst.reverse () in [424]: lstout[424]: [3, 2, 1, 0]in [425 ]: in [425]: lst.sort () in [426]: lstout[426]: [0, 1, 2, 3]in  [427]: lst.sort (reverse=true) in [428]: lstout[428]: [3, 2, 1, 0]in  [429]: in [429]: lstout[429]: [3, 2, 1, 0]in [430]: lst2  = LSTIN [431]: LST2OUT[431]: [3, 2, 1, 0]IN [432]: LST2[1]  = 5 In [433]: lst2Out[433]: [3, 5, 1, 0]In [434]:  Lstout[434]: [3, 5, 1, 0]in [435]: in [435]: lst2 = lst.copy ()In [436]: lst2out[436]: [3, 5, 1, 0]in [437]: lstout[437]: [3,  5, 1, 0]in [438]: lst2[1] = 7in [439]: lst2out[439]: [3,  7, 1, 0]IN [440]: LSTOUT[440]: [3, 5, 1, 0]


2, meta-group

Definition and initialization

In [441]: t = tuple () in [442]: T out[442]: () in [443]: T = () in [444]: T-out[444]: () in [445]: T = (all in all) in [446]: tout[4 ": (1, 2, 3) in [447]: t = tuple (range (3)) in [448]: tout[448]: (0, 1, 2) tuples are immutable


Inquire

In [449]: T-out[449]: (0, 1, 2) in []: t[0]out[450]: 0In [451]: t[-1]out[451]: 2In [452]: T.index (2) out[452]: 2In [453]: T.count (2) out[453]: 1


Named tuples

In [454]: From collections import Namedtuplein [455]: User1 = namedtuple (' A ', [' name ', ' age ']) in [456]: me = User (' Martin ', 1 8) in [457]: meout[457]: A (name= ' Martin ', age=18) in [458]: me.ageout[458]: 18In [459]: me.nameout[459]: ' Martin ' in [460]: M E[0]OUT[460]: ' Martin ' in [461]: me[1]out[461]: 18


This article from "Thick tak" blog, declined reprint!

Python Learning notes-built-in data structures

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.