Python (vi) tuples, Groud, dictionaries

Source: Internet
Author: User
Tags set set shallow copy

? Python provides a concise way to exchange data: x, y = y,x so that the values of the × and Y are interchangeable.


? variable data type: List

? Immutable data types: numbers, strings, tuples


? sequence data type: string, list, tuple

? unordered data types: numbers, dictionaries

tuples : A list with a magic spell

Tuples and lists differ in that tuples are immutable data types, and he does not have the means to append and store data more securely

First, create:

Tu = (1,2,3,4)

Note: A single tuple definition must be appended with commas, such as Tu = (1,)

Factory Method: T= tuple ([+])

Second, the operation of the tuple

1. Tuples are also sequences that support indexing, slicing, stitching, repeating, member operators

2. Meta-Group characteristics

? Assigning values to tuples, extending to multiple variables can also be assigned by the tuple method respectively

3. Delete a tuple

Three, the tuple's method

? T.count (value)-->int returns the number of occurrences of value in the tuple;
? T.index (value) returns the offset of value in the tuple (that is, the index value)

Iv. built-in functions: Cmp,len,max,min,enumerate,zip

Set and:

Set and are unordered, non-repeating data types, so you can remove duplicate elements from a list or tuple

Can not put variable data type in set and inside

Both the set and the Remove () and discard () are deleted, but if no element in the remove is an error, and discard no element does not error

First, the relationship test operation of the set

? Intersection: List_1.intersection (list_2)
? Set: List_1.union (list_2)
? Difference set: List_1.difference (list_2)
List_2.difference (list_1)
? Equivalent differential list_1.symmetric_difference (list_2)
? Subset List_1.issubset (list_2)
? Parent set List_1.issuperset (list_2)
? With or without intersection list_1.isdisjoint (list_2)


? Intersection: List_1 & list_2
? Set: List_1 | List_2
? Difference set: list_1-list_2
List_2-list_1
? Equivalent differential: list_1 ^ list_2

Second, add the collection

? S.add (1)
Add an item to the collection
? S.update ([1,3,4])
Add multiple items to the collection, followed by a parameter that should be an iterative type

Third, set and delete

? S.remove (1)
Deletes the specified element from the collection
? S.pop ()
Randomly deletes an element in the collection and returns the deleted element

Iv. other operations for sets and

? Len (s)
Show the length of set set
? "1" in S
Detects whether an element is a member of the collection S, returns a Boolean value

? S.copy ()
The shallow copy of the collection, not in-depth study here, will be said later
? S.clear ()
Empties all elements of a collection

Dictionary :

Dictionaries do not support indexing, slicing

The key in the dictionary cannot be duplicated.

Definition of a dictionary

? Simple dictionary Creation

? Built-in method: Fromkeys
The key in the dictionary has the same value, the default is None

Second, access to the dictionary

? Access directly via key

? Looping through access

Third, the addition of dictionary Key-value

? Dic[key] = value
With this operation, we will find that the dictionary is an unordered data type

Iv. deletion of the dictionary

? Dic.pop (Key)
Deletes the element of the dictionary according to the key value;
? Dic.popitem ()
Random Delete dictionary element, return (Key,value)
? Dic.clear ()
Delete all elements in a dictionary
? del DiC
Delete the dictionary itself

V. Common methods of dictionaries

? Dict.get ()
If key exists in the dictionary, returns the corresponding value value
? Dic.keys ()
Returns all key values for the dictionary
? Dict.has_keys ()
Whether a key value exists in the dictionary

Python (vi) tuples, Groud, dictionaries

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.