Python Learning lists and dictionaries

Source: Internet
Author: User


List

Basic Operations
>>>len ([1,3,4])
3

>>>[1,2,3]+[4,5,6] + number must be of the same type
[1,2,3,4,5,6]

>>>[' ni! '] *
[' ni! ', ' ni! ', ' ni! ', ' ni! ']

>>>STR ([up]) + "34"
' [1,2]34 '

>>>[1,2]+list ("34")
[1,2,3,4]


list Iteration and parsing
>>>3 in [+]
True

>>>for x in [£ º]
Print (x,end= ");
The
>>> res = [C-for-C in ' spam '];
>>>res
[' SSSs ', ' pppp ', ' aaaa ', ' mmmm ']

index shards and matrices

>>>l = [' AA ', ' BB ', ' CC ']
>>>L[2]
' CC '
>>>l[-2]
' BB '
>>>l[1:]
[' BB ', ' CC ']


lists are mutable, they support the manipulation of the list object in place

index vs. Shard Assignment

>>>l = [' AA ', ' BB ', ' CC ']
>>>l[1]= ' DD '
>>>l
[' AA ', ' DD ', ' CC ']

>>>l[0:2]=[' xx ', ' yy ']
>>>l
[' xx ', ' yy ', ' CC ']

invocation of a list method

>>>l.append (' AA ')
>>>l
[' xx ', ' yy ', ' cc ', ' AA ']
>>>l.sort ()
>>>l
[' AA ', ' cc ', ' xx ', ' yy ']
>>>l.sort (key=str.lower) sorted by lowercase in string
[' AA ', ' cc ', ' xx ', ' yy ']

>>>l.sort (Key=str.lower,reverse = True) flip
>>>l
[' yy ', ' xx ', ' cc ', ' AA ']


>>>l = [up]
>>>l.extend ([3,4,5]) extension
>>>l
[1,2,3,4,5]

>>>l.pop () launch last element
>>>l
[1,2,3,4]

>>>l.reverse () Flip
>>>l
[4,3,2,1]


>>>l = []
>>>l.append (1) Add data
>>>l.append (2)
>>>l
[Up]

>>>l = [' AA ', ' BB ', ' CC ']
>>>l.index (' AA ') index
0

>>>l.insert (1, ' xx ') insert
>>>l
[' AA ', ' xx ', ' BB ', ' CC ']


>>>l.remove (' xx ') removal
>>>l
[' AA ', ' BB ', ' CC ']

>>>l.pop (1) removing an indexed element
>>>l
[' AA ', ' CC ']



Other common list operations

>>>l = [' AA ', ' BB ', ' CC ']
>>>del l[0] Delete list element
>>>l
[' BB ', ' CC ']

>>>del l[1:] Delete list shards
>>>l
[' AA ']

>>>l = [' AA ', ' BB ', ' CC ']
>>>l[1:] = [] Delete list shards
>>>l
[' AA ']




Dictionary

In addition to the list, the dictionary is perhaps the most flexible data structure in Python.
basic operation of the dictionary

>>>d = {' spam ': 2, ' Ham ': 1, ' Eggs ': 3}
>>>d[' spam '] value via key
2

>>>d
{' spam ': 2, ' Ham ': 1, ' Eggs ': 3}

>>>len (d) Length
3
>>> ' Ham ' in D members
True

>>>list (D.keys ()) converts the dictionary key to list
[' Eggs ', ' ham ', ' spam ']


Modify dictionary in situ

>>>d[' ham '] = [' Grill ', ' bake ', ' fry '] assignment
>>>d
{' spam ': 2, ' Ham ': [' grill ', ' bake ', ' fry '], ' Eggs ': 3}

>>>del d[' eggs '] Delete
>>>d
{' spam ': 2, ' Ham ': [' grill ', ' bake ', ' fry ']}

Other Dictionary methods
>>>d = {' spam ': 2, ' Ham ': 1, ' Eggs ': 3}
>>>list (D.values ())
[3,1,2]


>>>list (D.items ())
[(' Eggs ', 3), (' Ham ', 1), (' Spam ', 2)]

>>>d.get (' spam ')
2

>>>print (D.get (' toast '))
None

>>>d.get (' Toast ', 88) default value
88

the Update method of the dictionary is similar to merging the same overlay
>>>d2 = {' Toast ': 4, ' Muffin ': 5, ' Eggs ': 3}
>>>d.update (D2)
>>>d
{' spam ': 2, ' Ham ': 1, ' eggs ': 5, ' toast ': 4, ' Muffin ': 5}

>>>d.pop (' Muffin ')
{' spam ': 2, ' Ham ': 1, ' eggs ': 5, ' Toast ': 4}



Python Learning lists and dictionaries

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.