python - structure data type (list, tuple set, dictionary)

First, a list of

It represents an ordered list of elements that can be numbers, strings, or may be another list.

---------------------------------------- # 
# list (list): a group of element sequence
# ----------------------------------------
# define an empty list
= S []
# define a list of all digital score
score = [90, 92, 88, 79, 95, 82]

# can access the list of elements by an index value, from left to right, a starting value of 0 , as last element. 1-n-
first_one Score = [0]
# can also be accessed from right to left by a negative index value, the inverse of the first element is -1, the last element of -n
last_one Score = [-1 ]

# modify the value of the element
Score [-1] = 80
Print (Score)

list element # may be different data types
student_info = [ '1001', 'Zhangxiao Xiao', 'woman', 12, [86, 88, 92]]
# microtome students basic information print
print ( "students basic information:", student_info [. 1:. 4])

# + the combination of operators on both sides of the list together
grade = [ 'seventh grade']
student_info = + Grade student_info
Print (student_info)

# = + The same can also be used
student_info + = [ 'Guangdong Province', 'Guangzhou']
Print (student_info)

# the append () to add a new element in the end of the list (you can only append an element)
student_info.append ( 'Panyu District')
Print (student_info)

# extend () at the end of the list append another list (the list is not added, but the elements in the list)
address = [ 'Southern towns', 'East Central Street']
student_info.extend (address)
Print (student_info)

# INSERT (x, Y) to the list of data insertion position x Y
student_info.insert (-1, "fun way") due East Central Street # -1, corresponding to fun way, it is not added to the end
student_info.insert ( len (student_info), "fun way") # the added fun way to the end
Print (student_info)

# * the elements of the list is repeated n times, and assembled into a list
Scores = [90, 100] *. 5
Print ( Scores)

# and not in use in judging whether there is an element in the list of
Print ( "100 in Scores:", 100 in Scores)
Print ( "100 not in Scores:",Not in Scores 100)

# len () returns the length of the list
= Score [90, 92, 88, 79, 95, 90, 82]
Print ( "% d Score list contains elements"% len (Score))

# max () returns the maximum value (data elements in the list of elements must be the same type, or an error occurs)
print ( "highest score is:", max (Score))
# min () returns a list of elements minimum
print ( "the lowest score:", min (Score))
# SUM ( ) returns a list of all the elements and
print ( "Total:", SUM (score))

# (x) a list of statistics count the number of times the same element value x appears
print ( "90 points has% d"% score.count (90))

# Sort () of the elements in the list are sorted in ascending order by default (needed is a unified single data type)
score.sort ()
Print ( "ascending order:", Score)
score.sort (Reverse = True )
Print ( "descending order:", Score)

# index (x) to obtain the specified element x first position (index) in the list of
index = score.index (90)
Print ( "90 points ranked as:" , index + 1)

# POP (index) to delete the list with an index value index of elements, remove the last element of the default
score.POP ()
Print ( "delete a minimum score:", score)

# Reverse () in the list of the elements reversed arrangement
score.reverse ()
Print ( "the elements in the list were reversed:", Score)

# the Remove (value) will be deleted from the list item element value value, and only once deleted an element
score.remove (90)
Print ( "delete 90 minutes:", Score)

# Clear () Clear list
score.clear ()
Print (Score)

Score = [90, 92, 88, 79, 95, 90, 82 ]
# del delete the element at the location, and may embodiment microtome for deletion
del Score [2]
Print (Score)
del Score [0:. 3]
Print (Score)
del Score
Print (Score)


II tuple
tuple is an ordered set of elements, once defined, can not be changed.

# ---------------------------------------- 
# tuple (tuple): a group of ordered elements. After defining the elements can not be modified
# ----------------------------------------
# create only one tuple element, the element must be a comma after
# T = ()
# = One (100)
an only_one = (100,)

# + tuple stitching, to generate a new tuple
info1 = '1001', 'Zhangxiao Xiao '
INFO2 = (' F ',' first day ')
Print ( "INFO1 + INFO2:", INFO1 + INFO2)

# * repeated many times
Print ( "INFO1 * 3:", INFO1 * 3)

# and not in use in determining whether there is an element in the tuple
print ( "Zhang Xiaoxiao in info1:", 'Zhangxiao Xiao' in the INFO1)
print ( "Zhang Xiaoxiao not in info1:", 'Zhang Xiaoxiao' Not in the INFO1)

student_info = ( '1001', ' Zhang Xiaoxiao ',' F ',' started ', 12' Canton ')
# len () returns the length of a tuple (a tuple number of elements)
Print ( "length tuple:"



= tuple1 (88, 93, 76, 90)
# max () Returns the maximum element of the tuple (a tuple of data types must match)
Print ( "maximum value of:", max (tuple1))
# min ( ) returns the minimum value tuple element (data type must be the same)
Print ( "minimum value is:", min (tuple1))

# convert tuple list
List2 = [100, 200 is, 300, 200 is, 400]
Tuple2 = tuple (list2)
print ( "the lists into tuples:", Tuple2)

# SUM () returns a tuple of all the elements and
print ( "Total:", SUM (Tuple2))

# COUNT () of specified elements the number of occurrences in a tuple
print ( "200 number of occurrences:", tuple2.count (200))

index # index () returns the first occurrence of the specified element in the tuple
print ( "200 first appears location: ", tuple2.index (200))

# convert a tuple into a list
list3 = list (student_info)
Print (" convert a tuple into a list: ", list3)

in # tuple element values can not be deleted only use the del statement to delete the entire tuple
del student_info

Third, the collection of
the collection is an unordered not repeat sequence of elements

# ---------------------------------------- 
# collection (set): a disorder not repeat sequence of elements
# ----------------------------------------
# create an empty collection, you must use the set (), you can not use {}
SET1 = the sET ()

# collections automatically remove duplicate data
cities = { 'Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Hainan', 'Guangzhou' }
Print (Cities)

# can create a list, then set into a set of function list
List2 = [100, 200 is, 300, 400, 300]
SET2 = set (List2)
Print (SET2)

# by a string create a set of characters
SET3 = the sET ( "the Hello")
Print (SET3)

# and not in use in judging whether there is an element in the collection
Print ( "200 in SET2:", 200 in SET2)
Print ( "00 not in SET2 : ", 200 is not in SET2)

a = {100, 200 is, 300}
b = {300, 400}
# ab &: difference set (returns a new set, comprising a has but b is not an element)
Print ("a-b:", a - b)
print(a.difference(b))

# A | b: union (returns a new collection that contains a and b each element)
Print ( "a | b:", a | b)
Print (a.union (b))

# a & b: Intersection (returns a new collection, comprising a and b, the common elements)
Print ( "a & b:", a & b)
Print (a.intersection (b))

# a ^ b: symmetric difference (returns a new set of , contains no elements appear in both a and b in)
Print ( "a ^ b:", a ^ b)
Print (a.symmetric_difference (b))

# the Add () to add to a collection of elements
a.add (400 )
Print ( "the Add:", a)
# update () to update elements in the collection. Single element may be added, listing, and the like to the set of tuples
a.update ([500, 600])
Print ( "Update:", a)

# a <= b: determining whether a subset of b (a check in whether each element are within b)
Print ( "a a b a subset:", a <= b)
# a> = b: determining a whether b superset (check b each element is in a in)
Print ( "b is a superset of:", a> = b)

# remove (x) would be removed from the collection element x, if the element does not exist will be given
a.


# Discard (x) x is an element removed from the set, when the element does not exist does not complain
a.discard (500)
Print ( "discard:", a)

# POP () to delete a random element in the collection
a .pop ()
print ( "POP:", a)

# len () calculates the number of elements in the set
print ( "the number of elements in the set:", len (a))

# Clear () Clear set
a.clear ()


IV dictionary

dictionary is a variable container model, objects can be stored in any type of a key - value pairs.
# ------------------------------------------------- --------- 
# dictionary (dict): a variable container model, you may store any type of object. By the key - value pairs
# ------------------------------------------- ---------------
# key must be unique and unchangeable
dic = {1: 'a quarter', '2': 'second quarter'}
# can not be accessed by the index, only button can be accessed
Print (DIC [ '2'])

DIC = {. 1: 'a quarter', '2': 'second quarter', '2': 'three quarters'}
Print (DIC)

DIC [' 2 'modified element value #'] = 'second quarter
dic [' 3 '] =' three quarters' # key to add a new
print (dic)

del DIC [ '. 3'] # element deletion dictionary
print (dic )

# len () calculates the number of elements in the dictionary, i.e., the total number of key-
print ( "Dictionary length:"









dic.clear ()
Print ( "DIC:", DIC)

dic3 = { 'a quarter ": 10000," the second quarter': 12,000, 'three quarters': 18000}
# GET (Key [, value]) Returns the specified key value. If the specified key does not exist, the return value
Print ( "sales in a quarter:", dic3.get ( 'a quarter'))
Print ( "sales in the fourth quarter was:", dic3.get ( 'fourth quarter') )
Print ( "sales in the fourth quarter was:", dic3.get ( 'the fourth quarter,' 'did not count'))

# setDefault (k [, v]) If k is present, return its value; otherwise it returns v, and adding a new element to the dictionary
print (dic3.setdefault ( 'a quarter'))
print (dic3.setdefault ( 'fourth quarter', 17,000))

# and not in use in the detection key (key) exists in the dictionary
print ( "a quarter present in the dictionary:" 'a quarter' in dic3)
print ( "fourth quarter does not exist in the dictionary:" 'four quarter' not in dic3)

# items () using a dictionary element to create a list of objects of tuples (a tuple corresponds to a key - value pair)
Print (dic3.







# Dic1.update (dic2) DIC2 the dictionary key / value pairs to update the dic1
dic4 = { 'January': 3500, "February": 3800}
dic3.update (DIC4)
Print (dic3)

# POP () the corresponding value to delete the dictionary specified key, and returns the value of the deleted
print (dic3.pop ( 'January'))

# popitem () to delete the last element of the dictionary and returns the key elements of the
print (dic3. popitem ())

Guess you like

Origin www.cnblogs.com/Teachertao/p/11210563.html