Jesse Pet
Jesse Pet

Reputation: 1879

Sorting dictionary keys

I am reading in from a file which has the following syntax:

Akaka D HI -1 -1 1 1 1 -1 -1 1 1 1 1 1 1 1 -1 1 1 1 -1 1 1 1 1 1 -1 1 -1 -1 1 1 1 1 1 1 0     0 1 -1 -1 1 -1 1 -1 1 1 -1
Alexander R TN 1 1 1 1 1 1 1 1 1 -1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1

I need to create a dictionary that maps the names to a list of their votes. The names in this case are Akaka and Alexander and the votes are the series of numbers from -1, 0, and 1. Example output would be:

{'Akaka': [-1 -1 1 1 1 -1 -1 1 1 1 1 1 1 1 -1 1 1 1 -1 1 1 1 1 1 -1 1 -1 -1 1 1 1 1 1 1 0 0 1 -1 -1 1 -1 1 -1 1 1 -1], 'Alexander': [1 1 1 1 1 1 1 1 1 -1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1]...,'name':[list of votes in integer form]}

So this is the code that I used:

for line in voting_data:
    myList.append(line.strip().split(' '))
names = []
votes = []
intVotes = []
votingDict = {}
for i in myList:
    names.append(i[0])
for j in names:
    votes.append(i[3:])
for k in votes:
    intVotes.append([int(v) for v in k])

votingDict = {names[i]:votes[j] for i in range(len(names)) for j in range(len(votes))}
print votingDict

The first few outputs returns the following (Pyror is another name and so is Bunning):

{'Pryor': ['-1', '-1', '1', '1', '1', '-1', '-1', '1', '1', '1', '1', '1', '-1', '1', '1', '1', '1', '1', '-1', '1', '1', '1', '1', '1', '1', '1', '-1', '-1', '1', '1', '1', '1', '1', '1', '1', '-1', '1', '-1', '1', '1', '1', '1', '-1', '1', '1', '-1'], 'Bunning': ['-1', '-1', '1', '1', '1', '-1', '-1', '1', '1', '1', '1', '1', '-1', '1', '1', '1', '1', '1', '-1', '1', '1', '1', '1', '1', '1', '1', '-1', '-1', '1', '1', '1', '1', '1', '1', '1', '-1', '1', '-1', '1', '1', '1', '1', '-1', '1', '1', '-1']

my issue is that the wrong name is getting mapped to the wrong list. In otherwords, the list mapped to Pryor is actually the list that should map to Alexander. My lists are in correct order, but the names are not. Is there a way to sort the names in this dictionary?

Upvotes: 0

Views: 104

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1123400

You are recombining your lists incorrectly:

votingDict = {names[i]:votes[j] for i in range(len(names)) for j in range(len(votes))}

This creates a nested loop, not a parallel loop. The normal solution here is to use zip():

votingDict = {name: vote for name, vote in zip(names, votes)}

However, you can greatly simplify your code by building the votingDict mapping while looping over the file:

votingDict = {}
for line in voting_data:
    name, ign1, ign2, *votes = line.split()
    votingDict[name] = [int(v) for v in votes]

str.split() without arguments will split on variable width whitespace and remove trailing and leading whitespace, all in one go.

You can then assign the list output to a series of names, including a catch-all starred name for the remaining items; I use it to capture the name, two ignored columns and then the rest in the votes list, which we then convert to integers with a list comprehension.

Upvotes: 2

Wookai
Wookai

Reputation: 21773

I think you are overcomplicating things with your three loops (that should be nested, if I read correctly). You could simply do this:

votingDict = {}
for line in voting_data:
    parts = line.strip().split(' ')
    name = parts[0]
    votes = [int(v) for v in parts[3:]]
    votingDict[name] = votes

Basically, as you read your lines, you extract the name and the votes, convert the votes to integers and store them in your dict.

Upvotes: 1

Related Questions