(Beginner) I don't know how to declare and enter a list in Python Dictionary "T"

Asked 1 years ago, Updated 1 years ago, 83 views


Finally, the desired dictionary form is {'Code 1' : [Code 1 name, 1 or 0], 'Code 2' : [Code 2 name, 1 or 0], ...} 

You want to enter 1 or 0 in the list [1] element with a separate action
I can't do this and that in the Python writing code. I'll write it down.

Question 1: How do I declare a list to be included in a dictionary?  

Question 2: How should I read the input file (csv file) and enter it in the dictionary?

with open('ucode_file.csv', 'r') as f_ucode:

    for ucode in f_ucode:
        ucode_value = ucode.strip().split(',')
        ucode_01 = '1' #We will insert 1 or 0 later through a separate operation.

        ucodes.append(ucode_value)  
        ucode_dic[ucode_value[0]] = ucode_value[1]

        *ucode_dic[ucode_value[0]][1].append(1)
        ucode_dic[ucode_value[0]].append(ucode_01)
        ucode_dic[ucode_value[0][1]] = ucode_01*

----- input file type (1000 lines). ucode_file.csv
UCODE (code), UNAME (code name)
UNIM04, Flight Safety Zone 4
UHK100, General Logistics Complex
Permission restricted areas such as UMK400, (Han River) Construction, etc
UNE114, Control and Protection Area (Navy Base)
UQS700, Port
UQS710, Port Facilities


----- You are about to enter it as a dictionary and use it.-----
You want to enter 1 or 0 in the list [1] by doing a separate task.
The form is {'code1': [code1 name, 1 or 0], 'code2': [code2 name, 1 or 0], ...} 

Desired result: {'UNIM04' : [Flight Safety Zone 4, 1], 'UHK100' : [General Logistics Complex, 0],
 'UMK400': [(Han River) Construction Permission Restricted Area, 1], 'UNE114': [Control Protection Area (Navy Base), 1],
 'UQS700': [Port, 0], 'UQS710': [Port Facility, 0]}

python dictionary

2022-09-22 18:47

2 Answers

import csv

with open('ucode_file.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    result = {row['UCODE']:[row['UNAME'], 1] for row in reader}

result

{'UNIM04': ['Flight Safety Zone 4', 1],
 'UHK100': ['General Logistics', 1],
 'UMK400': [(Han River) Construction Permission Restricted Area', 1],
 'UNE114': [Control Reserve (Navy Base), 1],
 'UQS700': ['Port', 1],
 'UQS710': ['Port Facility', 1]}

Pandas, numpy are modules that are used as much as basic modules when processing data in Python. Various data processing is possible, so you can process data comfortably if you only learn Pandas and numpy.

The disadvantage is that it is an external module, so a separate installation is required, and there may be some overhead by the pandas. numpy has great advantages in large matrix operations, and linear algebra operations are convenient. If the matrix is small, there may be no performance advantage.

import pandas as pd

data = pd.read_csv('ucode_file.csv')
dict(zip(data['UCODE'], data[['UNAME']].values))

{'UNIM04': array (['Flight Safety Zone 4'], dtype=object),
 'UHK100': array (['general logistics complex'], dtype=object),
 'UMK400': array (('Han River) construction permit restricted area'), dtype=object),
 'UNE114': array (['control protection zone (naval base')', dtype=object),
 'UQS700': array (['port'], dtype=object),
 'UQS710': array (['port facility', dtype=object)}


d = dict(zip(data['UCODE'], data[['UNAME']].values))
d['UNIM04'] = np.append(d['UNIM04'], 1)
d

{'UNIM04': array (['Flight Safety Zone 4', 1], dtype=object),
 'UHK100': array (['general logistics complex'], dtype=object),
 'UMK400': array (('Han River) construction permit restricted area'), dtype=object),
 'UNE114': array (['control protection zone (naval base')', dtype=object),
 'UQS700': array (['port'], dtype=object),
 'UQS710': array (['port facility', dtype=object)}


2022-09-22 18:47

=> You've already used it properly.

result = {
  'Code1': [],
  'Code 2': ['Value 0', 'Value 2', '2'] ,
}
print (result['code2'])
# The result
# ["Value 0", "Value 2", "2"] 
dictobj = csv.DictReader(open('ucode_file.csv')).next() 

csv DictReaderSee the official document!


2022-09-22 18:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.