python--list, dictionary, tuple, collection comparison

Source: Internet
Author: User

#概括: List dict tuple set display form, add, delete, modify compare
#list the form of a collection in order
List=[]
list=[1,2,3]
list2=[
("Name1", "Y1"),
("Name2", "Y2")
]
#----------------------------------------------------------------------
#dict dictionary form unordered, key and value
dict={}
dict={"name1": "Apple", "name2": "Egg"}
#----------------------------------------------------------------------
#tuple tuple form (tuple and list are very similar, but tuple cannot be modified once initialized, its nested list contents are variable)
#定义空
Tuple ()
#定义一个元素必须加 ","
tuple= (1,)
#----------------------------------------------------------------------
#set Set Form 1 unordered 2 null with () has value {} 3, the collection main function to repeat (for example, a list into a set form, the list of repeating elements remain unique)
Set1=set ()
set2={1,2,3}
#----------------------------------Add------------------------------------
#列表增加
list=[1,2,3]
#list Add one (trailing append)
List.append ("T5")
#list increased by two (specified position insertion)
List.insert (1, "T6")
#---------------------------------------------------------------------
#字典增加
dict={"name1": "Apple", "name2": "Egg"}
dict["Name3"]= "Orange"
#---------------------------------------------------------------------
#集合增加
set={"Apple", "egg"}
#set add one (one element)
Set.add ("Orange")
#set increased by two (n elements)
Set.update (["Test1", "test2"])


#----------------------------------Delete------------------------------------
#列表删除

list=[1,2,3,4,5,6,7]
#list Delete one (specified element) An error if the element does not exist
List.remove (2)
#list Delete Two (specify index 0 Delete first empty delete last) but collection Set.pop cannot specify 0
List.pop ()
#list Delete three (specified index)
Del List[1]

#---------------------------------------------------------------------
#字典删除

dict={"name1": "Apple", "name2": "Egg"}
#dict Delete one (specify key)
Del dict["Name1"]

#---------------------------------------------------------------------
#集合删除
set={"Apple", "egg", "egg1", "EGG2"}
#set Delete one (delete any element, because it is unordered, cannot specify an index, can only be empty)
Set.pop ()
#set Delete Two (delete the specified element) error if the element does not exist
#set. Remove ("egg")
#set Delete Three (delete the specified element) if the element does not exist and does not error
Set.discard ("Egg1")

#----------------------------------Modify------------------------------------
#列表修改
list=[6,7,8]
#list Modify One (Specify location modification)
list[2]= "T"
#---------------------------------------------------------------------
#字典修改
dict={"name1": "Apple", "name2": "Egg"}
dict["name2"]= "Orange"
#---------------------------------------------------------------------
#set Modification not found


#----------------------------------Output------------------------------------
#列表输出
list=[6,7,8]
#list method One: Direct output name
Print (list)
#方法二: Cyclic output name
For I in list:
Print (i)
#---------------------------------------------------------------------
#字典输出
dict={"name1": "Apple", "name2": "Egg"}
dict["name2"]= "Orange"

#dict method One: Direct output name
Print (DICT)
#方法二: Loop output name only output value
For I in Dict:
Print (Dict[i])
#---------------------------------------------------------------------
#集合输出
set={"Apple", "egg"}
Set.update (["777", "888"])
#dict method One: Direct output name
Print (set)
#方法二: Cyclic output name
For I in set:
Print (i)

python--list, dictionary, tuple, collection comparison

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.