Python Learning Notes (10) Python collection (i)

Source: Internet
Author: User
Tags stdin

Review

Int/float/str/list/tuple/dict

Integers and floating-point types are immutable, not sequences

The string is immutable and is a sequence

The list is mutable and is a sequence

Tuples are immutable and are sequences

Dictionaries are can become, but not sequences

Basic concepts of collections

Set is the basic mathematical concept, it is the object of set theory, refers to the general of things with a certain nature, (in the most primitive set theory-the definition of the simple set theory-the set is "a pile of things.") The Things ("things") in a collection are called elements. If x is the element of a set A, it is recorded as X∈a.

Methods for creating collections

Method 1: Use curly braces {}; The object wrapped in curly braces is a collection

Method the 2:set () function typically uses this function to create a collection

The elements of the collection are not sequential and cannot be duplicated

Collection is non-hashed

1>>> {1,"python"} #使用花括号创建集合2Set (['python', 1])3>>> type ({1,"python"})4<type'Set'>5>>> Set ("python")6Set (['h','o','N','P','T','y'])7>>> s= Set ("python") #使用set () Create a collection8>>>s9Set (['h','o','N','P','T','y'])Ten>>> S2=set (["Baidu","Google","Ali"]) One>>>type (s2) A<type'Set'> ->>>S2 -Set (['Baidu','Google','Ali']) #集合的元素没有顺序 the>>> S3=set ([2,2,2,2,2])  ->>>S3 #集合元素不可重复 -Set ([2]) ->>>

Hash and non-hash

In its lifetime, immutable objects are hashed and, conversely, mutable are non-hashes.

All immutable in Python are hashed, such as numbers, strings, tuples

Other lists, dictionaries are mutable and are non-hashed

The key in the dictionary must be a hash, that is, an immutable object

In the collection, the elements of the collection must be hashed, meaning that the elements of the collection must be immutable objects

So it's an error to use the list as an element of the collection, because the list is a non-hash object

1>>> LST =[[1,2,3],"python"] #用列表作为参数, create a collection, Error list is not hash2>>> s =Set (LST)3 Traceback (most recent):4File"<stdin>", Line 1,inch<module>5Typeerror:unhashable Type:'List'6>>> d={[1,2,3]:"python"} #创建一个字典, key is a list, error List is not hash7 Traceback (most recent):8File"<stdin>", Line 1,inch<module>9Typeerror:unhashable Type:'List' #list is non-hashedTen>>>

Conversion between a collection and a list

Set () List ()

1>>> lst=[1,2,3]2>>> s =Set (LST) #将列表转换成集合3>>>s4Set ([1, 2, 3])5>>> Lst2 =list (s) #将集合转换为列表6>>>Lst27[1, 2, 3]8>>> a =[1,2,2,3,3,6,6,8,9, 0,0] #去除列表中的重复项, set () can be used9>>> s =set (a)Ten>>>s OneSet ([0, 1, 2, 3, 6, 8, 9]) A>>> A =List (s) #去除重复后, and then convert to lists ->>>a -[0, 1, 2, 3, 6, 8, 9] the>>>s -Set ([0, 1, 2, 3, 6, 8, 9])  ->>>Hash (s) #返回hash值, can also determine whether hash, error is not hashed, otherwise return hash value - Traceback (most recent): +File"<stdin>", Line 1,inch<module> -Typeerror:unhashable Type:'Set' +>>> Hash (1) A1

Create immutable Collections

Frozenset () creates immutable collections, which are hashed

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

Python Learning Notes (10) Python collection (i)

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.