Python Learning Note 2 Python Common collection types: list,tuple,set,dict Introduction to use

Source: Internet
Author: User
Tags set set

In Python programming, the most common types of collections we use are: list,tuple,set,dict; let's talk about these types of collections in a few simple ways;

List lists: Equivalent to an array, but the length of the list is automatically changed and the elements are free, without having to be the same type for each element. Its concise definition is a=[]. is an ordered combination of elements that can be deleted and added;

The basic operations of the list are as follows:

# #生成一个list, and assign values to list_ex>>> list_ex=[1,3,4, "list", "Tuple", "dict", ' Set ']>>> list1_ex>>> List_ex[1, 3, 4, ' list ', ' Tuple ', ' dict ', ' Set ']>>> # #获取list中的元素个数 >>> len (LIST_EX) 7 #用索引访问list中的位置, Access range:-n ' ~n-1; When the index is out of range, a indexerror error >>> list_ex[0]1>>> list_ex[-1] ' set ' >>> list_ex[is reported Len (LIST_EX)-1] ' Set ' #在list尾部增加一个元素 >>> list_ex.append (' over ') >>> list_ex[1, 3, 4, ' list ', ' Tuple ', ' Dict ', ' Set ', ' over '] #插入元素到指定位置list_ex. Insert (1, ' 2 ') >>> list_ex[1, ' 2 ', 3, 4, ' list ', ' Tuple ', ' dict ', ' Set ', ' O ' Ver ']>>> list_ex.insert >>> list_ex[1, 2, ' 2 ', 3, 4, ' list ', ' Tuple ', ' dict ', ' Set ', ' over ' #删除一个元素 >>> List_ex.pop () ' over ' >>> list_ex[1, 2, ' 2 ', 3, 4, ' list ', ' Tuple ', ' dict ', ' Set '] #删除指定的元素 >> > List_ex.pop (2) ' 2 ' >>> list_ex[1, 2, 3, 4, ' list ', ' Tuple ', ' dict ', ' Set '] #一个list的元素可以说另一个list >>> l ist_exs=[' Python ', list_ex, ' over ']>>> list_exs[' Python ', [1, 2, 3, 4, ' list ', ' Tuple ', ' dict ', ' Set '], ' over '] #list_exs的长度应该是3 >>> len (list_exs) 3 #list_exs可以认为是一个二维数组 >>> list_exs[2][0] ' o ' >>> list_exs[1][3]4 #设置一个空list, length 0>>> l=[]>>> l[]>> > Len (L) 0

Tuple tuple: It is also an ordered list, very similar to list, but the tuple can no longer be modified once initialized, and cannot be added to and removed from the tuple. It is because the tuple is immutable, so the code is more secure, in some special case, to modify the non-list elements of a tuple, you can first convert to list and then in the modification, and finally back to Tuple;tuple basic operation as follows:

1 #Define tuple2>>>tp= ('List','tuple','Dict','Set')  3>>>TP4('List','tuple','Dict','Set')  5 #remove elements from a tuple in the same way as List6>>>tp[2]  7 'Dict'  8>>>tp[-1]  9 'Set'  Ten #define only one element at the end of the tuple to be added, (1,) instead of directly (1) One>>>tp= (1,)   A>>>TP -(1,)   ->>>tp= (2)   the>>>TP -2 - #A tuple contains the list ->>>tp= (1,py,3)   +>>>TP -(1, ['1','tuple','Dict','Set'], 3)   +>>>tp[1][1]=2 A>>>TP at(1, ['1', 2,'Dict','Set'], 3)

Dict Dictionary: The key value pair type, the key name is not repeatable, and can not be changed, the simple definition is: a={}, is an unordered combination, its principle and the reality of the dictionary, through the dictionary index list to find the number of the desired word, and then directly to the page to find the value of the word you need ; it looks very fast and does not change with the size of the dictionary.

The specific operation of the dictionary is as follows:

1 The order in which a dict,dict is built is not related to the order in which the key is placed.2dt={'a': 1,'b': 2,'C': 3,'D': 4}  3>>>DT4{'D': 4,'C': 3,'b': 2,'a': 1}  5 #remove value from key6>>>dt['C']  738 #new key and value9>>>dt['e']=5Ten>>>DT One{'e'75A'D': 4,'C': 3,'b': 2,'a': 1}   A #value is re-assigned by key - #since a key can only correspond to one value, the value is placed on a key more than once, and subsequent values will flush out the previous value . ->>>dt['e']=10 the>>>DT -{'e': 10,'D': 4,'C': 3,'b': 2,'a': 1}   - #If key does not exist, Dict will error ->>>dt['F] +      -Syntaxerror:eol whileScanning string literal +>>>dt['F']   A Traceback (most recent): atFile"<pyshell#1093>", Line1,inch<module> -dt['F']   -Keyerror:'F'   - #determine if key is in Dict by in ->>>'b' inchDT - True in #returns the specified value when a key corresponding value is not present by the Get method ->>>dt.get ('F',-1)   to-1 +>>>dt.get ('a',-1)   -1 the #use Pop (key) to delete the specified key and the corresponding value *>>>dt.pop ('a')   $1Panax Notoginseng>>>DT -{'e': 10,'D': 4,'C': 3,'b': 2}

Set set: Similar to Dict, but a set of key sets that do not store value values; is an unordered combination in which the key value is non-repeatable (int,float,bool,str,tuple), as follows:

#produce a set>>> St=set ([1,2,3,4,5])>>>st{1, 2, 3, 4, 5}#duplicate key values are not present in set>>> St=set ('23455432')>>>st{'3','4','5','2'}#Add a key value via Add (key)>>> St.add ('1')>>>st{'3','4','1','5','2'} #Remove the key value to set with Remove (key)>>> St.remove ('3')>>>st{'4','1','5','2'}

Python Learning Note 2 Python Common collection types: list,tuple,set,dict Introduction to use

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.