Python data types of essays, about List,dict and set

Source: Internet
Author: User

The concept of list:
#是一种有序数组集合
l=[' cris ', ' make ', ' lie '
#一般索引是从0开始
Len (l) can return the length of the array
To add elements, you can use
L.append ("Cris") #把元素添加到数组的末尾
L.insert (1, "Cris") #把元素插入到指定索引的位置
L.pop (index) #可以删除元素 and returns the value "Cris" does not write the index delete the last element by default
L.remove (' element ') #删除元素
Del l[index] #删除元素
L[1]= "Cris" #替换第二个元素
L.extand (list name) #将两个列表拓展为一个列表
L.reverse () #反转列表
L.sort () #排序
For i,j in Enumerate (li,100): #这个函数可以打印list元素的系列号和元素, the default is 0 start or you can specify the start sequence number, such as 100
Print (I, "---", j)

The concept of a tuple:
Tuple is also known as the ancestor, and the list is similar, but initialization can not be modified
List (tuple) can be converted to lists
Tuple (list) can be converted to Ganso


The concept of dict:
#dict我称其为键值对, also known as a dictionary, Kye must be unique, the dictionary is unordered, and its syntax is as follows (partial list syntax also applies to dict)
D = {' Michael ': Up, ' Bob: ', ' Tracy ': 85}
#可以调用相应的key (key) to get the corresponding value (value)
d[' Michael ']
95
#可以通过以下方式判断这个值是否再dict中
' Cris ' in D
Return flase if not present
#也可以通过get方法, the value does not return None (recommended for this value)
D.get (' Cris ') can return none or specify return value such as-1
D.get (' Cris ',-1)
The #append () method can also increase the element
#如果要删除一个字典元素可以用pop方法, both its key and value are removed
D.pop (' Bob ')
Returns 75
#如果元素不存在则会报错可以设定一个默认值这样就不会报错了
D.pop (' Sasasas ' None)
return None
#另一个判断是否存在语法
Print (' Sasasas ' in D)
In return true does not return flase
#查询所有的Key
Print (D.keys ())
#查询所有的value
Print (D.values ())
#合并两个字典如果有重复元素用新的取代
D.update (DICT2)
D.fromkeys ([1,2,3],{"B"})


The concept of Set:
Dir (set) command View all methods
#set和dict类似, is a collection of key sets, but does not store value.  Because key cannot be duplicated, there is no duplicate key in set. The collection also has a relationship test function
The data in set is immutable type

s = Set ([1, 1, 2, 2, 3, 3])
>>s
{A-i}
You can add elements to the set by using the Add (key) method, and you can add them repeatedly, but without effect:
>>> S.add (4)
>>> s
{1, 2, 3, 4}
>>> S.add (4)
>>> s
{1, 2, 3, 4}
You can delete an element by using the Remove (key) method:
>>> S.remove (4)
>>> s
{1, 2, 3}
#两个set合并
Set.update (Set2)

#求差集并赋值集合
Set3=set.difference (Set2)

#丢弃删除元素 (Note: If there is no error)
Set.discard (Element)

#删除元素 (if no error occurs)
Set.remove (Element)

#判断是否是另一个的子集 (that is, if one contains all elements of the former)
Set.issubset (Set2)

#判断是否是另一个的父集
Set1.issuperset (SET)

#set的特殊功能: Relationship testing (the results are not modified by the test results below)
Dir (set) command View all methods
#交集 tests, two of them.
Print (Set1.intersection (STE2))
Print (Linux & python)
#差集 's in List A, there's No in B.
Print (SET1.DIFFEREBCE (Set2))
Print (Set1-set2)
#并集 Merge The elements of the two list to remove duplicate
Print (Set1.union (Set2))
Print (Set1 | set2)
#对称差集 is to print an element that no one has.
Print (Set1.symmetric_difference (Set2))
Print (Set1^set2)

Python data types of essays, about List,dict and set

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.