. 1 with Open ( 'X- : /XXX/XXX/XXX/data.txt ' , encoding = " UTF--SIG. 8 " ) AS File: 2 # on line parameters! Illegal multibyte sequence, the input encoding = 'utf-8' resolves: occurs only 'r' given: 'GBK' CODEC CAN Not 0xB7 in decode byte position. 8 . 3 # appears only when "utf-8" Output the first line more than a hex-encoded with '-sig' resolve. . 4 data_line = FILE.readline () . 5 data_line = data_line.replace ( ' \ T ' , ' , ' ) . 6 data_line = data_line.strip ( ' \ n- ' = data_line.split Keys ( ' , ' ) . 8 . 9 10 values_wet = SET () # SET () is the only way to create empty set . 11 values_dry = SET () 12 is values_harm = SET () 13 is values_re = SET () 14 15 16 the while True: 17 data_line = FILE.readline () 18 IF data_line =! '' : # solve the problem the last line, pay attention to add else: break, otherwise infinite loop 19 data_line = data_line.strip ( ' \ the n-') 20 data_line = data_line.replace('\t',',') 21 values = data_line.split(',') 22 values_wet.add(values[1]) #添加值到集合,".add()"方法 23 values_dry.add(values[2]) 24 values_harm.add(values[3]) 25 values_re.add(values[4]) 26 else: 27 break 28 29 30DIC = {Keys [. 1]: values_wet, Keys [2]: values_dry, Keys [. 3]: values_harm, Keys [. 4 ]: values_re} 31 is 32 33 is # -defined function is determined values belongs Key 34 is DEF get_key (value, dict = DIC): 35 return [K for K, V in dict.items () IF value in V] # listing analytic formula 36 37 [ 38 is # output 39 VALUE1 = INPUT ( ' the Enter The Thing: ' ) 40 Print (get_key (VALUE1) )
- The refuse is input effect you can get what it should be put in the trash:
-
RESTART ========: E: \ xxxx \ xxxx \ xxxx \ xxx.py ======== the Enter at The Thing: wound paste [ ' hazardous waste ' ] >>>
- The idea is to build a large dictionary, there are four key under the dictionary, each key is a set of corresponding value, the value of the collection of all kinds of garbage. Want to add a new future as long as the garbage to the collection .add () can be.
- Get_key main function is to find from the Internet, and finally for their own needs slight modification to the function content. We use it on a list comprehension type of strange, need more practice.
- Finally, attach the data.txt
No. wet garbage dry products hazardous waste recyclables 1 leaves the old tub Bucket plastic bottles 2 Orange NiCd canned food bowl 3 onions bad toilet lighter bottles 4 biscuit cans old bandage tank 5 ketchup shell alcohol newspaper 6 eggs Brushes palette old bag housing 7 rind jar old paint bag 8 potato sponge capsule pharmaceutical expired old shoes 9 fishbone peanut shells thermometer milk cartridge 10 sugarcane old plastic cutting board expired tablet basket 11 maize old brick fluorescent doll 12 bones (duck and goose) toilet paper battery jug 13 shrimp basketball medical cotton old iron pot 14 cake Peach gas tank garbage stabbed 15 Medical gloves, old bread cup mirror 16 Strawberry ceramic bowl pesticides toothbrush 17 tomatoes disposable chopsticks watercolor brush plastic comb 18 pear tile pesticide old hat 19 shells prunes nuclear medical gauze old clip 20 banana peels bad oral pots waste bottle lock 21 pepper perfume bottles toothpaste broom 22 chocolate wood copious umbrella frame comb 23 eggplant soiled clothes cosmetic expired old sacks 24 pea skin hair spray cigarette cartons 25 Apple sediment old toys syringe 26 leaves wet bulb waste garbage bags NULL