Python Study Notes (10) Python set (1), python Study Notes

Source: Internet
Author: User
Tags hashable

Python Study Notes (10) Python set (1), python Study Notes

Review

Int/float/str/list/tuple/dict

Integer and floating-point types are unchangeable, not sequences.

The string is immutable and is a sequence.

The list is variable and is a sequence.

Tuples are immutable and are sequences.

The dictionary is variable, but not a sequence.

 

Basic concepts of a set

A set is a basic mathematical concept. It is the object of the set theory. It refers to the general idea of things of a specific nature, A collection is a pile of things ".) A collection of things ("things") is called an element. If 'X' is an element of 'A', it is recorded as 'X' and 'A.

 

How to Create a set

Method 1: Use curly braces {}; the object wrapped with curly braces is a set

Method 2: The set () function generally uses this function to create a set.

The elements of the set are unordered and cannot be repeated.

The set cannot be hashed.

1 >>>{ 1, "python"} # Use curly brackets to create a set 2 set (['python', 1]) 3 >>> type ({1, "python"}) 4 <type 'set'> 5 >>> set ("python") 6 set (['h', 'O', 'n ', 'P', 't', 'y']) 7 >>> s = set ("python") # Use set () create a set 8 >>> s 9 set (['h', 'O', 'n', 'P', 't', 'y']) 10 >>> s2 = set (["baidu", "google", "ali"]) 11 >>> type (s2) 12 <type 'set'> 13 >>> s214 set (['baidu', 'Google ', 'ali']) # The elements of the set are not ordered. 15 >>> s3 = set ([, 2]) 16 >>> s3 # The set elements cannot be repeated. 17 set ([2]) 18 >>>

Hash and non-Hash

Objects that are unchangeable during their lifetime can be hashed. Otherwise, objects that are changeable cannot be hashed.

All unchangeable values in Python are hashable, such as numbers, strings, and tuples.

In addition, lists and dictionaries are variable and cannot be hashed.

The Key in the dictionary must be hashable, that is, an immutable object.

In a set, the elements of the set must be hashable, that is, the elements of the set must be immutable objects.

Therefore, if a list is used as a collection element, an error is returned because the list is an object that cannot be hashed.

1 >>> lst = [[1, 2, 3], "python"] # use a list as a parameter to create a set, error list is not hash 2 >>> s = set (lst) 3 Traceback (most recent call last): 4 File "<stdin>", line 1, in <module> 5 TypeError: unhashable type: 'LIST' 6 >>> d = {[1, 2, 3]: "python"} # create a dictionary with the key as the list, the Error list is a non-hash 7 Traceback (most recent call last): 8 File "<stdin>", line 1, in <module> 9 TypeError: unhashable type: 'LIST' # list is a 10>

Conversion between a set and a list

Set () list ()

1 >>> lst = [, 3] 2 >>>> s = set (lst) # convert the list to a set 3 >>> s 4 set ([1, 2, 3]) 5 >>> lst2 = list (s) # convert the set to list 6 >>> lst2 7 [1, 2, 3] 8 >>> a = [1, 2, 3, 3, 6, 6, 8, 9, 0] # Remove repeated items in the list, you can use set () set 9 >>> s = set (a) 10 >>> s11 set ([0, 1, 2, 3, 6, 8, 9]) 12 >>> a = list (s) # remove duplicates and convert them to list13> a14 [0, 1, 2, 3, 6, 8, 9] 15 >>> s16 set ([0, 1, 2, 3, 6, 8, 9]) 17 >>> hash (s) # returns the hash value, you can also determine whether hash is allowed. If an error is returned, the hash value 18 Traceback (most recent call last): 19 File "<stdin>", line 1, in <module> 20 TypeError: unhashable type: 'set' 21 >>> hash (1) 22 1

Create an unchangeable set

Frozenset () creates an immutable set, which can be hashed.

1 >>> a2 [0,1,2,3,6,8,9]3 >>> s2 =frozenset(a)4 >>> type(s2)5 <type 'frozenset'>6 >>> hash(s2)7 20963408638 >>>

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.