Python list, tuple, collection, dictionary differences, and mutual conversions

Source: Internet
Author: User
Tags python list

Differences in lists, tuples, collections, dictionaries set
list tuple dictionary
English List Tuple Set Dict
can read and write Write Read-only Write Write
Can I repeat Is Is Whether Is
Storage mode Value Value Key (cannot be duplicated) Key-value pairs (keys cannot be duplicated)
is ordered Ordered Ordered Disordered Unordered, automatic positive order
Initialization [1,‘a‘] (‘a‘, 1) set([1,2])Or{1,2} {‘a‘:1,‘b‘:2}
Add to append Read-only add d[‘key‘] = ‘value‘
Read elements l[2:] t[0] No d[‘a‘]
Lists, tuples, collections, dictionaries convert list tuples to other
‘‘‘#列表转集合 (out of weight)
List1 = [6,7,8,8,9]
A=set (List1)
Print (a)
‘‘‘
‘‘‘
#两个列表转字典
List1 = [' Key1 ', ' key2 ', ' Key3 ']
List2 = [' 1 ', ' 2 ', ' 3 ']B
=dict (Zip (list1,list2))
Print (b) #嵌套列表转字典
LIST3 = [[' Key1 ', ' value1 '],[' key2 ', ' value2 '],[' Key3 ', ' value3 ']
C=dict (LIST3)Print (c)
‘‘‘
‘‘‘
# list, tuple to string
List2 = [' A ', ' a ', ' B ']
D= '. Join (LIST2)
Print (d)
‘‘‘
‘‘‘T
Up1 = (' A ', ' a ', ' B ')
D= '. Join (TUP1)
Print (d)
‘‘‘
‘‘‘
#字典转其他
# Dictionary converted to string
Dic1 = {' A ': 1, ' B ': 2}
E=str (DIC1)
Print (e)
‘‘‘
‘‘‘
# Dictionary key and value cross-transfer
Dic2 = {' A ': 1, ' B ': 2, ' C ': 3}
Dic3={value:key for key, value in Dic2.items ()}
Print (DIC3)
‘‘‘
#字符串转其他
# string Goto List
s =' AABBCC '
f=List (s)
Print (f)
# String to tuples
g=tuple (s)
Print (g)
# string to go to collection
H=Set (s)
Print (h)
# String to Dictionary
Dic2 = eval ("{' name ': ' Ljq ', ' Age ': 24}")
Print (DIC2)
# Slicing strings
A = ' a b C '
I=a.split (")
print (i)

Python list, tuple, collection, dictionary differences, and mutual conversions

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.