Look at the picture first
What is a list? I think the list is the list we often see in our daily life. For example, count the things we've bought in the past week, and make a list of these things. Since we may buy one thing more than once, there are duplicates allowed in the list. If we expand the scope of the list and count all our expenses in the past week, then this is also a list, but there will be different categories of items in this list. For example, we buy things as a kind of expense, and pay water and electricity as a kind of expense. The types of these items can be different. The reason of python's list is the same as that of the list. The features are: repeatable, different types. Different types are the most essential differences with arrays. The list in python is represented by "[]":
lst = ['arwen',123] print(lst[0]) print(st[1]) lst[0] = 'weiwen'
(2) Tuple
tup = ('arwen',123) print(tup[0]) print(tup[1])
There is no difference between tuples and lists in structure. The only difference is that tuples are read-only and cannot be modified. Tuples are represented by "()", such as:
(3) Set is the set of our mathematics, there is no special definition. The best application of aggregation is de duplication. There is no special representation for a set. Instead, it is transformed into a set by a set function, such as:
lst = [ 1, 1, 0] lst_set = set( lst ) #lst_set For 1 , 0 tup = (2, 2, 1) tup_set = set( tup) # tup_set For 2 , 1 for item in lst_set: print(item)
(4) The last one is the dictionary. The dictionary stores key value pair data, such as:
lists = {1:'a',2:'b',3:'c'} #The outside of the dictionary is in braces. Each group is connected by colons, and then each group is separated by commas. #The greatest value of a dictionary is to query, find values through keys.
Example:
1. List tuple to other
# List to collection(Duplicate removal) list1 = [6, 7, 7, 8, 8, 9] set(list1) # {6, 7, 8, 9} #Two list to dictionary list1 = ['key1','key2','key3'] list2 = ['1','2','3'] dict(zip(list1,list2)) # {'key1': '1', 'key2': '2', 'key3': '3'} #Nested list to dictionary list3 = [['key1','value1'],['key2','value2'],['key3','value3']] dict(list3) # {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} # List, tuple to string list2 = ['a', 'a', 'b'] ''.join(list2) # 'aab' tup1 = ('a', 'a', 'b') ''.join(tup1) # 'aab
2. Dictionary to others
# Dictionary to string dic1 = {'a':1,'b':2} str = str(dic1) print(str) # "{'a': 1, 'b': 2}" # Dictionaries key and value Interturn dic2 = {'a': 1, 'b': 2, 'c': 3} dic3 = {value:key for key, value in dic2.items()} print(dic3) # {1: 'a', 2: 'b', 3: 'c'}
3. String to other
# String to list s = 'aabbcc' list(s) # ['a', 'a', 'b', 'b', 'c', 'c'] # String tuple tuple(s) # ('a', 'a', 'b', 'b', 'c', 'c') # String conversion set(s) # {'a', 'b', 'c'} # String to dictionary dic2 = eval("{'name':'ljq', 'age':24}") # Segmentation string a = 'a b c' a.split(' ') # ['a', 'b', 'c']
2, Division and combination of dictionaries
#Division: base = {'A':1, 'B':2, 'C':3, 'D':4, 'E':5} subkey = ['C', 'E'] subdict=dict([(key, base[key]) for key in subkey]) print(subdict) #{'C': 3, 'E': 5} #Merger: #Method 1: d1={'user':'root','pwd':'1234'} d2={'ip':'127.0.0.1','port':8080} d3=dict(d1, **d2) print(d3) #Mode two: d1={'user':'root','pwd':'1234'} d2={'ip':'127.0.0.1','port':8080} d3={} for k,v in d1.items(): d3[k] = v for k,v in d2.items(): d3[k] = v print(d3)
3, list splitting and merging
a=[1,2,3,4,5,6] b=['a','b','c','d'] print(a+b) a=[1,2,3,4,5,6] b=['a','b','c','d'] a+=b print(a) a=[1,2,3,4,5,6] b=['a','b','c','d'] a.extend(b) print(a) a=[1,2,3,4,5,6] b=['a','b','c','d'] a[0:0]=b print(a) li = ['a','b','c'] res1=';'.join(li) res2=''.join(res1).split(';')