0

i have to implement a stable marriage problem in python where 'n' men and 'n' women will have a preference list each and the algorithm matches them according to it. i have to input the number of participants 'n' and both male and female preferences in a single text file.

right now im inputting using two files and the algorithm works fine but i need to keep all the data in a single file.

my code is

import copy
f = open('C:\\Users\\Rakshith\\Desktop\\ADA project\\uyt.txt','r')

malepreferences = eval(f.read())
print(malepreferences)
g = open('C:\\Users\\Rakshith\\Desktop\\ADA project\\uuu.txt','r')
femalepreferences=eval(g.read())
print(femalepreferences)

one of my two input files look like

{
 'john':  ['jennifer', 'alexis', 'abby', 'megan', 'kate', 'anna', 'samantha', 'ashley', 'emma', 'jesse'],
 'harvey':  ['abby', 'emma', 'jennifer', 'anna', 'alexis', 'samantha', 'ashley', 'kate', 'megan', 'jesse'],
 'kumar':  ['emma', 'alexis', 'jennifer', 'anna', 'ashley', 'samantha', 'megan', 'jesse', 'abby', 'kate'],
 'David':  ['megan', 'samantha', 'anna', 'jesse', 'emma', 'alexis', 'kate', 'ashley', 'abby', 'jennifer'],
 'jim':   ['kate', 'anna', 'ashley', 'abby', 'samantha', 'alexis', 'jennifer', 'megan', 'emma', 'jesse'],
 'fred': ['ashley', 'jennifer', 'anna', 'jesse', 'alexis', 'megan', 'abby', 'kate', 'emma', 'samantha'],
 'peter':  ['jesse', 'alexis', 'megan', 'ashley', 'abby', 'jennifer', 'anna', 'emma', 'kate', 'samantha'],
 'patel':  ['jennifer', 'alexis', 'emma', 'samantha', 'megan', 'abby', 'kate', 'ashley', 'jesse', 'anna'],
 'mike':  ['emma', 'abby', 'anna', 'jesse', 'ashley', 'jennifer', 'samantha', 'megan', 'kate', 'alexis'],
 'randy':  ['jennifer', 'samantha', 'kate', 'jesse', 'alexis', 'ashley', 'anna', 'abby', 'megan', 'emma']}

my input file should look like this

n

m1: w11, w12, ..., w1n

mn: wn1, wn2, ..., wnn

...

...

...

w1: m11, m12, ..., m1n

wn: mn1, mn2, ..., mnn
Natalie Hedström
  • 2,607
  • 3
  • 25
  • 36
Grigor Carran
  • 89
  • 1
  • 8
  • You are loading your data in a dictionary, so when you print it you see first the key and then a list with the values associated to this key. In order to print just the values take a look at this [POST](http://stackoverflow.com/questions/4547274/convert-a-python-dict-to-a-string-and-back) –  Feb 12 '16 at 09:16

2 Answers2

0

Firstly, you shouldn't store the repr of a dictionary and eval it. You should use a proper serialization format, ie JSON, which looks very similar and can be easily and safely parsed.

Then, you can simply store a list of the two dictionaries you need, and read them back:

f = open('C:\\Users\\Rakshith\\Desktop\\ADA project\\combined.txt','r')
data = json.load(f)
males, females = data
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

You should store the data in the format of JSON as mentioned by Daniel. You can structure something like this :

{
    "Name": "john",
    "Preference":[
        {
            "Name" : "jennifer",
            "Name" : "alexis",
            ......
            "Name" : "jesse",
        }
    ]   
}

To read JSON file, you will need the built-in library: json

import json
with open('C:\\Users\\Rakshith\\Desktop\\ADA project\\uyt.txt','r') as f:
    data = json.load(f)
Alex Fung
  • 1,996
  • 13
  • 21