Basic Python Learning-collections

Source: Internet
Author: User

#__author: "Feng Lin"#DATE:2018/8/26#A collection is an unordered, non-repeating collection of data in which elements are hashed (immutable types), but the collection itself is not hashed (so the collection cannot be a dictionary key)#Collection note the following points in mind#1. Element de-weight#2. Relationship testing, intersection, set, and differenceSet1=set ({})#an immutable type of element in the collection, or an error will be#set2={1,2,3,{' name ': ' Alex '},[1,2]}#set add element, add, updateSet1={'wangjinxin','Penglei','Fangren','Liuchong','Lintianyi'}Print(Set1) Set1.add ("Goddess")Print(Set1)#update to increase fall-generation objectsSet1.update (Range (10))Print(Set1)#Random DeletePrint(Set1.pop ())#Delete by ElementSet1.remove (1)Print(Set1)#empties the collection, represented by an empty collection as set ()set1.clear ()Print(Set1)#del Set1#print (Set1)#Checkset2={'haha','Hihi','AA','xx','DD'} forIinchSet2:Print(i)#ask for intersectionset1={1,2,3}Print(Set1) Set2={4,5,6,7,8}Print(Set2) Set3=set1 &Set2Print(SET3)#Negation intersectionPrint(set1^Set2)#the two methods of seeking and collectingPrint(set1|Set2)Print(Set2.union (Set1))#Finding the difference setPrint(set1-Set2)Print(set2-Set1)#determine if a bool value is returned for a subset or supersetPrint(Set1 <Set2)Print(Set1 >Set2)#questions, go to the list below to re-li=[1,2,33,44,22,2,2,1,4,5,6,61,2,3]#turn a list into a set to go heavyli1=set (LI)Print(LI1)#re-converting the collection to a listLi=list (LI1)Print(LI)#Immutable Collection FrozensetS=frozenset ('Babsddaw')Print(S,type (s)) forIinchS:Print(i)

Basic Python Learning-collections

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.