The difference between a list, a ganso, a dictionary in Python

Source: Internet
Author: User


Defined Method
List Can contain different types of objects, you can add or subtract elements, you can combine with other lists or split a list, using [] to define the

eg:alist=[123, ' abc ', 4.56,[' inner ', ' list '],7-9j]

1.list (str): Convert str to list type, str can make a string or tuple type

2.alist.append (' Test '): Append element to list

del alist: Delete the entire list

4.cmp (list1,list2): Compare the size of two lists

5. Len (list): Returns the number of list elements

6.sorted (list): Sort elements in a list using a dictionary order

7.reversed (list): The position of an element in the inverted list

8.list.count (obj): Returns the number of times the object obj appears in the list

9.list.extend (seq): Adds the contents of the sequence SEQ to the list 10.list.insert (index,obj): Inserts an Obj object where the index amount is index

11.list.pop (index=-1): Deletes and returns the object at the specified position, which is the last object by default

from list

Yuan zu

can contain different types of objects, but they are immutable and cannot be defined by adding or subtracting elements, using () to define

eg:atuple= (123, ' abc ', 4.56,[' inner ', ' list ') , 7-9j)

1.tuple (obj): Convert object obj to a tuple object, obj can be any string or list

2. The Del,cmp,len,max,min method for the list also applies to tuple, but because Ganso is immutable, substitution, addition, sorting, and so on are not possible

Dictionary

Key-value pairs, defined with {}

eg:adict={' name ': ' Cynthia ', ' Age ': 18}

1.dict1=dict ([' X ', 1],[' Y ', 2]) create a dictionary:d ICT ()

2.dict1={}.fromkeys ((' x ', ' y '), -1): Fromkeys () creates a default dictionary in which the elements in the dictionary have the same value

3.dict1.keys (): Gets the list of key values for the dictionary

4.dict1.has_key (' x '): Determines if the dictionary has an ' x ' key value, returns the bool type

5.dict.get (Key,default): Returns the value of the key value key, if key does not exist, returns the value of default

6.dict.items (): Returns the value of a key-value pair list

7.dict.values (): Returns a list of all values in the dictionary

8.dict.update (DICT2): Adds a Dict2 key-value pair list to the dictionary Dict

9.dict.pop (Key): Returns value of key value key

10.setdefault (): Similar to the Get method, the value of the given key can be obtained, in addition SetDefault can also be automatically weight does not contain a given key in the case of setting the corresponding Key-value

11.clear (): Clears all items in the dictionary. In-place operation, no return (or return value of None)

12.copy (): Returns a new dictionary with the same key-value as a shallow copy (shallow copy)


The difference between a list, a ganso, a dictionary 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.