def connect_mysql(sql, oper_type="select", data_l=None): conn = pymysql.connect(host=‘localhost‘, user="root", password="123", database="work", port=3306) cur = conn.cursor() if oper_type == "insert": cur.executemany(sql, data_l) conn.commit() else: cur.execute(sql) result = cur.fetchall() # print(type(result), "result") conn.close() return result def process_jobs(field_name): sql = "select j." + field_name + " FROM personal_jobs j" column_name = connect_mysql(sql, oper_type="select") row_total = (len(column_name)) row_category = set(column_name) # init category dict category_dict = {} for k in row_category: category_dict[k] = 0 # calculate amount cal_nmu = 0 for k in row_category: for r in column_name: if r == k: cal_nmu += 1 category_dict[k] = cal_nmu cal_nmu = 0 print(type(category_dict.items()), category_dict.items()) print(row_total, len(category_dict.items())) return row_total, category_dict process_jobs("job_salary")