II. Lists, tuples, sets, dictionaries of basic application

1. The application list (List []  is very suitable may vary during program operation for storing data sets )

1.1 range () function

Experiments = []
for Experiment in range(1,10):
   Experiments.append(Experiment**2)
  print(Experiments)
>>[1, 4, 9, 16, 25, 36, 49, 64, 81]

1.2 List resolve the for loop and create new elements of the code merged into one line, and automatically append a new element

Experiments =[value**2 for value in range(1,10)]
print(Experiments)
>>[1, 4, 9, 16, 25, 36, 49, 64, 81]

1.3 slices and traverse sections

players = ['football','basketball','tennis']
print("I like playing:")
for player in players:
    print(player.title())

1.4 Copy List

players_one = players[:]

1.5 format()

name = '{n} like playing football'.format(n='alex')
print(name)
>>alex like playing football

 

2. tuple (tuple () can not be changed)

2.1 traversal

 

name = ( 'liu','li')
for i in name:
    print(i)
>>liu
>>li

 

2.2 Index

 

name = ( 'liu','li')
print(name[0])

2.3 operator may (add, multiply, and len ())

name = ( 'liu','li')
number =(1,2)
print(name + number)
>>('liu', 'li', 1, 2)

3. The set (set {}, deduplication, relationship testing, random)

3.1 Relationship Test

set_one = { 1,4,6}
set_two = {1, 5,"a" }
print(set_one.intersection(set_two))#交集
print(set_one & set_two)#进阶
>>{1}
>>{1}
print(set_one.difference(set_two))#差集
print(set_one - set_two)
>>{4, 6}
>>{4, 6}
print(set_one.union(set_two))#并集
print(set_one | set_two)
>>{1, 'a', 4, 5, 6}
{. 1 >>, ' A ' ,. 4,. 5,. 6 }
 Print (set_ont ^ set_two) # deduplication and set

 3.2 Adding the collection

= {. 1 set_two,. 5, " A " } 
set_two.add ( 13 is) # can only add single 
Print (set_two)
 >> { ' A ' ,. 1, 13 is,. 5 } 
set_two.update ([ 14, " Apple " ] ) # plurality add 
Print (set_two)
 >> {. 1,. 5, ' Apple ' , 13 is, 14, ' A ' } 
set_two.update ({ 23 is}, [ " JIU " ]) # test 
Print (set_two)
 >> {1, 5, 23, 'jiu', 'a', 13}

3.3 Delete collection

= {. 1 set_two,. 5, " A " } 
set_two.remove ( . 5) # Specify remove elements, given the absence of 
Print (set_two)
 >> {. 1, ' A ' } 
set_two.discard ( . 5) # also specify deleted element, does not exist is not being given 

set_two.pop () # random delete a

4. dictionary (dict () is disordered, and the key unique to the natural weight)

 Print and Replace 4.1

= {info " key1 " : " VALUE1 " ,
         " key2 " : " value2 " }
 Print (info [ " key1 " ]) # print the value of key1, then no error 
>> VALUE1 
info [ " key1 " ] = " Value 1 " # replace or add 
Print (info)
 >> { ' key1 ' : ' value. 1 ' , ' key2 ': 'value2'}

4.2 Delete key-value pairs

= {info " key1 " : " VALUE1 " ,
         " key2 " : " value2 " } 
info.pop ( " key1 " ) # Delete key to 
Print (info)
 >> { ' key2 ' : ' value2 ' } 

del info [ " key1 " ] # del delete key-value pairs 
Print (info) 

info.popitm () # random deleted

4.3

= {info " key1 " : " VALUE1 " ,
         " key2 " : " value2 " }
 for I in   info:
     Print (I, info [I])
 >> key1 VALUE1
 >> key2 value2 

for K, V in info.items ( ): # info.items, first read the list and then assign info 
    Print (k, v)
 >> key1 value1
 >> key2 value2

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/huiguizhe/p/11988302.html