There are some strange things about Python dictionary generation.

Asked 1 years ago, Updated 1 years ago, 108 views

Hello, I'm a beginner at Python.

The data in the csv file are in the first line, Rank, Major_code, and so on There are data from the next line.

I'm making a dictionary by combining the first line data and the underline data.

But the pre-production works fine, but.. If there are eight pieces of data, all eight of them are the same. The items and data should be in the order of Rank and Major_code The order is also mixed up.But looking at the order carefully, it seems to be in alphabetical order.

I'll show you the code and when I run it.

import os

def read_major_file(filenames):
    os.chdir('../Data/Day4-Dictionaries/College-Majors/')
    text = open(filenames,'r')
    linetext = text.readlines()
    d = []
    for line in linetext:
        f = line.rstrip('\n').split(',')
        d.append(f)
    data_dict = {}
    final = []
    For i in range(1,len(d): // The reason why the first index is set to 1 is because the data starts from the second line.
        for r in range(len(d)):
            data_dict[d[0][r]]=d[i][r]
        final.append(data_dict)
    return final
In [168]: read_major_file('recent_Arts.csv')
Out[168]: 
[{'Major': 'DRAMA AND THEATER ARTS',
  'Major_category': 'Arts',
  'Major_code': '6001',
  'Men': '7022',
  'Rank': '167',
  'Sample_size': '357',
  'ShareWomen': '0.629504564',
  'Total': '43249',
  'Women': '11931'},
 {'Major': 'DRAMA AND THEATER ARTS',
  'Major_category': 'Arts',
  'Major_code': '6001',
  'Men': '7022',
  'Rank': '167',
  'Sample_size': '357',
  'ShareWomen': '0.629504564',
  'Total': '43249',
  'Women': '11931'},
 {'Major': 'DRAMA AND THEATER ARTS',
  'Major_category': 'Arts',
  'Major_code': '6001',
  'Men': '7022',
  'Rank': '167',
  'Sample_size': '357',
  'ShareWomen': '0.629504564',
  'Total': '43249',
  'Women': '11931'},

When I turned the for door and asked for a break, the first value came out well The dictionary value seems to be continuously initialized after turning the whole thing around. Now those are the last data.

And the items should be in the order of Rank, Major_code, Major, Total It's mixed up as if it's aligned. There is no problem specifying the array. When I printed it out separately, it came out well.

To summarize the question

Help me!

python dictionary

2022-09-22 21:10

1 Answers

When I finish turning the 1 for statement, I think the 1st to nth final will be included in the code.

2 Values entered in dictionary are not sorted by Key. The only way is to print it out in order using the key that you want to print. If you want to specify the order, make a list in the order of the keys you want to print, and use the list to load and print the values.For example, you can use print_order as follows.

dict = {'Major': 'DRAMA AND THEATER ARTS',
  'Major_category': 'Arts',
  'Major_code': '6001',
  'Men': '7022',
  'Rank': '167',
  'Sample_size': '357',
  'ShareWomen': '0.629504564',
  'Total': '43249',
  'Women': '11931'}

print_order = ['Major','Major_category','Major_code','Men','Rank','Sample_size','ShareWomen','Total','Women']

for i in range(len(print_order)):
    print(print_order[i]+": "+dict[print_order[i]])

3. Since a decimal number is included in the number, data_dict[d[0][r]=d[i][r] If only the number is included, we will check the value of d[i][r]int(d[i][r]) and if the number and . are included, flat[d]

Alternatively, you can check if it is an integer. We can check if it's an integer or a float, or we can just put it in.

def intTryParse(value):
    try:
        return int(value), True
    except ValueError:
        return value, False


2022-09-22 21:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.