user20481823
user20481823

Reputation:

Creating separate sets from file in python

Below is a sample input file:

A, B, C 
Location:London 
A, 46
B, 93
C, 32
Location:Amsterdam 
A, 83
B, 21
C, 92
Location:Paris
A, 29
B, 91
C, 10

The output should be as follows:

name_set = { A, B, C } location_set = {London, Amsterdam, Paris}

Generate a dictonary that maps name to number and calculate total

dic = {A: 46, B: 93, C:32 , A: 83, B: 21, C: 92, A:29, B: 91, C:10}
A has 158 
B has 205 
C has 134 

This is what I tried but I don't get above sets:

  name_set = set() 
    location_set = set()
    num_set = set ()
    userfile = input("Enter input file name:") 
    input_file2 = open(userfile, "r") 
    input_file = input_file2.readlines()
    name_set = input_file[0].strip().split(',')

for next_line in input_file: 
    if next_line.startswith("Location"):
        location_set = next_line.strip().split(":")[-1]
    else:
       num_set = next_line.strip().split(',')[-1]
print(name_set) 
print(location_set) 
print(num_set)

name_to_num = {} 
for k in name_set:
   for v in scores_set:
      party_to_score[k] = v
print(name_to_num) 

Upvotes: 1

Views: 55

Answers (1)

CryptoFool
CryptoFool

Reputation: 23099

It is unclear what you want as a result, as you seem to want to produce a dictionary with duplicate keys. This isn't supported by a standard Python dictionary, and usually isn't what you want anyway. Just think...how would you tell Python which key value to look up in a dictionary that had duplicate keys?

Here's code that gives you the simple sets that you want by constructing a data structure to represent the input file that is easy to query and build other results from once it has been built. NOTE: this ignores the first line of the file, and will build the list of names from whatever it finds in the values for each location.

locations = {}
current_location = None
name_set = set()

input_file = open('/tmp/data.txt', "r")

for next_line in input_file:
    next_line = next_line.strip()
    if next_line:
        if next_line.startswith("Location"):
            location = next_line.split(":")[-1]
            current_location = locations[location] = {}
        elif current_location is not None:
            key, val = next_line.split(',')
            if key not in name_set:
                name_set.add(key)
            current_location[key] = int(val)

location_set = set(locations.keys())

print(location_set)
print(name_set)
print(locations)

Result:

{'Amsterdam', 'London', 'Paris'}
{'A', 'C', 'B'}
{'London': {'A': 46, 'B': 93, 'C': 32},
 'Amsterdam': {'A': 83, 'B': 21, 'C': 92},
 'Paris': {'A': 29, 'B': 91, 'C': 10}}

Then, if you want to count to total number of As, Bs and Cs, you can do this:

import collections

...

counts = collections.Counter()
for values in locations.values():
    counts.update(values)

 print(counts)

which will give you this:

Counter({'B': 205, 'A': 158, 'C': 134})

or if you want the counts in a standard dict, you can add:

counts = dict(counts)

print(counts)

to get:

{'A': 158, 'B': 205, 'C': 134}

Upvotes: 1

Related Questions