python combination of data types and data structures

//2019.12-07
. 1, which composition Pyhton There are three main types of data: a collection (set), the sequence (string str, list and a list of tuples tuple) and mapping (dictionary DIC)
2, collection type generally used braces {} It is expressed, and the inside of the collection element and positions the index almost no concept, is unique and random elements can be used to filter the same elements.
3, the dictionary is defined to be defined using {}, may be used dict () to dictionary definitions, definitions set {} not combined data, the combined data is set () function to be defined.
4, the dictionary delete key-value pairs may be used del reserved word be achieved, del S [Key]
#python inside combined data type
5, a set of tuples, dictionaries and lists and associated operator Daquan Introduction:

#python inside combined data type 

associated operators # set
S = {1,2, "Hello World", 1,2, "yanjiangyi"}
Print (S) # can automatically filter out the same elements, there is no concept of sequential ,
Print (len (S)) # number of elements in a unique output
print (type (s)) # type of the output data set
S = {l, 2,3, "YJY", ". 11", 123,24}
T = {l, 3, "YJY", 1243 of}
Print (ST) # difference set
print (s & t) # intersection
print (s ^ t) # complement
print (s | t) # union
common operating functions # set and methods: first, re-export operations
s.add ( "AI") # adding elements
Print (S)
s.remove ( "YJY") # remove elements
Print (S)
s.clear () # empty elements
Print (S)
Print (len (s)) # length of the output set of
print (1 in s) # s to check whether the element is located inside the
print ( "yjy"
#set variable function can be defined an empty set
A = SET ()
Print (A)
s = "known as know know that they do not know, is knowledge!"
Print (SET (S)) #set function may be other combinations of data into a set of data types, clear very filtered same parts

# list type and associated operating
s = [ 1,2,3,4, "yjy", "sweet circle", "pearl milk tea", 1,2] # store a variety of data types and independent, does not filter the same time between the data elements, there is a number of
Print (S)
Print (of the type (S))
Print (list ( "my life is no dress rehearsal, live every day!"))
# a list of methods of operation and the function
print (3 * s) # copy list element n times, then spliced together
T = [1,2,3,4]
Print (S + T) # directly spliced more lists
print (s.index (2)) # index of the first element in the list value of the output
print (s [1: 5: 2]) # list slice and the index
print (s.count (2)) # output; an element of the list number
s = [1010, "1010" , 1010, [1,2,3,4], "12 is"]
Print (S)
Print (type (S [2]))
Print (S [-2]) # lists indexing
for I in S:
Print (I) # cycle through each element of the list to find
Print (S [0:. 4: 2])
Print (S [-5:-1:2])
print (s [1: 4] ) # operation common sections list
related operation list #
s.append (1) #append refers to an increase in a last element of the list
Print (S)
s.insert (2, "YJY ") #insert refers to an index position i in the list to add an element X
Print (S)
s.pop (2) #pop method refers to the deletion list element at index i
Print (S)
s.remove (" 1010 " ) #remove removal list refers to the first occurrence of the element X
Print (S)
s.reverse ()
Print (S) #reverse refers to the elements in the list directly into reverse
t = [1,2,3, 5,113,67,89,546799]
t.sort () #sort elements of the list in ascending order are arranged distributed
Print (t)
p = t = # is an address to t p, so p and t are consistent the
p = t.copy () #copy refers to a list of the copy operation, opening up new data, change does not cause a change in t p of
print (t)
t.clear () clear the list of elements #clear
print ( T)
Print (P)
# tuple data type and associated operating
s = (1,2,3,4,5,57,6,87, "yjy" , [1,2,2, "yjy"])
Print (S)
Print (type (S)) # output type tuple
Print (s.count (. 1))
Print (s.index ( "YJY")) # tuple not be arbitrarily modified elements inside information can not be modified, with a very good safety guarantee can not be modified any tuple inside
print (s [3]) # tuple element index
print (s * 2) # tuples copied data type
s = [1,2,3,3,6576,34, "YJY"]
S = tuple (S) # lists into tuples can protect data
Print (S)
Print (tuple ( "I suggest you go ")) #tuple tuple functions and operations can be switched between the data

# combination of dictionary data: and set as yet order to achieve the mapping between the value-Key
S = {" YJY ": 1234, "artemisinin": "Tu Yo Yo", "father of hybrid rice": "Longping". 1: 123}
Print (S)
Print (S [ "YJY"]) # key value query dictionary, Dictionary the indexing methods, can only be indexed by the associated key
Print (S [. 1])
S [. 1] = "Deng Jiaxian"Value for the dictionary modification value #
Print (S)
T} = {# define an empty dictionary
t [2019] = "tsinghua university " # for dictionary expansion element
Print (T)
S = { "201801": "Le Blanc", "201802": "Derrick Rose"}
Print (len (S))
Print (max (S))
Print (min (S)) output key # the maximum and minimum
X = dict ()
print (X)
# dictionary method of operating
print (type (s.keys ()))
print (s.keys ()) # output dictionary key, so as to output a list of
print All keys (s.items ()) # dictionary to output: output in the form of tuples
print (s.values ()) # value of all the output values of the dictionary
print (s.get ( "201801") ) # output key value corresponding to the value, if there is no default value is returned back their definition
Print (S)
Print (s.get ( "201803"), "the absence of the data")
Print (s.popitem ()) # output random value pairs in the dictionary, then delete
print (s.pop ( "201801") , " the key data does not exist") # output value corresponding to the key value, if there exists the delete this key-value pair does not exist will return the default value
Print (S)
Print ( "201801"in s) # can only be determined whether there is the dictionary key, the value can not be determined value
s = { "201801": "Le Blanc", "201802": "Derrick Rose"}
for K in S:
print ( "the dictionary keys and values are: {} and {}". format (k, s.get (k)))
dictionary print (k) # traversal cycle output when the key

 

Guess you like

Origin www.cnblogs.com/Yanjy-OnlyOne/p/12001188.html