python-Data structure

Source: Internet
Author: User
Tags pear

List

There is not necessarily only one type of content in list, a list may have both number and string, and possibly a child list

list is denoted by []

The basic methods of list are: Append,sort, etc.

To remove an element from a list, you can use the Del function (it is not a list method)

#!/usr/bin/pythonMyList= ['Apple','Pear','Banana']Print 'MyList is', Mylistmylist.sort ()Print 'After sort, mylist is', Mylistfirst=Mylist[0]del(mylist[0])Print 'Have bought', First,', now remains', forIteminchMyList:PrintItem

Output

MyList is [' Apple ', ' pear ', ' banana ']
After sort, mylist is [' Apple ', ' banana ', ' pear ']
Has bought Apple, now remains banana pear

Tuple

Elements inside cannot be changed, tuple can have sub-tuple

The tuple uses () to denote

Empty tuple with () represents

A tuple that contains only one element is appended with [,] e.g after the element. One_elem_tuple = (2,)

#!/usr/bin/pythonOld_zoo= ('Tiger','Lion','Elephant')Print 'All animals in the Old_zoo is', Old_zooPrint 'Number of animals in the Old_zoo is', Len (old_zoo) New_zoo= ('Monkey','Panda', Old_zoo)Print 'All animals in the New_zoo is', New_zooPrint 'Number of animals in the New_zoo is', Len (New_zoo) -1+len (new_zoo[2])Print 'Animals bought from the Old_zoo is', new_zoo[2]Print 'The last animal bought from the Old_zoo is', New_zoo[2][2]

Output

All animals in the Old_zoo is (' Tiger ', ' lion ', ' elephant ')
Number of animals in the Old_zoo is 3
All animals in the New_zoo is (' monkey ', ' Panda ', (' Tiger ', ' lion ', ' elephant ')
Number of animals in the New_zoo is 5
Animals bought from the Old_zoo is (' Tiger ', ' lion ', ' elephant ')
The last animal bought from the Old_zoo is elephant

Dictionary

Key-value pairs

Key must be unique

Key can only be used with an "immutable" object (e.g. string), value may be "immutable or can be changed" object

D={key1:value1,key2:value2,...}

Help booklet: Helping (Dict)

#!/usr/bin/pythonAB = {'John':'[email protected]',      'Bill':'[email protected]',      'Lily':'[Email protected]ohu.com'}Print 'john\ ' s address is', ab['John']delab['Bill']Print 'After del, there is {0} contacts in the Address-book'. Format (len (AB)) forName,addressinchAb.items ():Printname,addressab['Rose']='[email protected]'if 'Rose' inchAb:#OR ab.has_key (' Rose ')    Print 'rose\ ' s address is', ab['Rose']

Output

John ' s address is [email protected]
After Del, there is 2 contacts in the Address-book
John [email protected]
Lily [email protected]
Rose ' s address is [email protected]

The items method returns a list of tuple members, each containing two elements: key and value

Sequence: list,tuple,string

Key Features: Member testing (expression "whether it belongs to this sequence"), subscript operation

Slice

eg. A[1:4] Take 1 to 3
A[1:] Take from 1 to the end
A[:4] from the beginning to 3
A[::2] From the beginning to the end, take every 2 steps

A[::-1] From the end of the head to take
A[4:1:-1] from 4 to 2 upside down
A[-1] Take the last character

Set

>>> BRI = Set ([' Brazil ', ' Russia ', ' India ')
>>> ' India ' in BRI
True
>>> ' USA ' in BRI
False
>>> bric = bri.copy ()
>>> bric.add (' China ')
>>> Bric.issuperset (BRI)
True
>>> bri.remove (' Russia ')
>>> Bri & BRIC
Set ([' Brazil ', ' India '])

When you create an object and assign it to a variable, the variable name points to the memory that holds the object.

e.g.

Shoplist = [' Apple ', ' mango ', ' carrot ', ' banana ']

MyList = Shoplist #shoplist和mylist指向同一内存.

Del Shoplist[0] #此时shoplist和mylist都少了第一个元素 because they point to the same memory.

MyList = shoplist[:]

Del Shoplist[0] #此时shoplist少了第一个元素, and mylist unchanged

These rules apply to sequences or complex objects and are not applicable to simple objects (e.g. Integer).

In fact, for A=4;b=a;a=5, a points to a new memory, and b still points to the original memory, so the value of B does not change. Only changing the common memory will change the two variables at the same time.

String

All string types are objects of class Str, see more methods for that class, use Help (str)

python-Data structure

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.