-2

I have been given this as a list of lists each containing either a number 1 2 3 and 0 (0 is repeated twice). Depending on the number and the position I would like a corresponding variable to get added 1 time for each occurrence.

    ballots = [['1', '2', '3', '0', '0'], 
    ['1', '3', '0', '2', '0'], 
    ['1', '2', '3', '0', '0'], 
    ['0', '3', '2', '0', '1'],  
    ['1', '3', '0', '2', '0'],  
    ['2', '0', '3', '1', '0'],  
    ['0', '0', '2', '1', '3'],  
    ['0', '1', '2', '3', '0'],  
    ['0', '1', '0', '2', '3'],  
    ['2', '3', '1', '0', '0'],  
    ['3', '2', '0', '0', '1'],  
    ['0', '1', '3', '2', '0'],  
    ['0', '0', '1', '2', '3'],  
    ['0', '0', '3', '2', '1'],  
    ['1', '2', '3', '0', '0'],  
    ['2', '1', '3', '0', '0'],  
    ['0', '3', '2', '1', '0'],  
    ['0', '2', '3', '0', '1'],  
    ['1', '2', '3', '0', '0'],  
    ['1', '0', '0', '3', '2'],  
    ['2', '1', '3', '0', '0'],  
    ['3', '1', '2', '0', '0'],  
    ['2', '3', '0', '1', '0'],  
    ['0', '0', '3', '1', '2'],  
    ['0', '3', '1', '0', '2'],  
    ['2', '1', '0', '0', '3'],  
    ['2', '0', '0', '1', '3'],  
    ['2', '0', '0', '1', '3'],  
    ['3', '0', '1', '0', '2']]

For example, for the first list:

  • the 1 in position 1 would mean that candidate1vote1 += 1
  • the 2 in the 2nd position would mean that candidate2vote2 += 1
  • the 3 in the 3rd position would mean that candidate3vote3 += 1

All 0's are ignored but still counted as a space. For the second list:

  • the 1 in the first position would mean that candidate1vote1 += 1
  • the 3 in the 2nd position would mean that candidate3vote2 += 1
  • the 2 in the 4th position would mean that candidate4vote2 += 1

Basically the position corresponds to candidate1/2/3/4/5 and the value corresponds to either a 1st preference vote, 2nd preference vote or a 3rd preference vote.

Does anyone know how I'd be able to sort through the lists using for/while loops so that it goes through each ballot and each individual vote doing the corresponding sum?

Tagc
  • 8,736
  • 7
  • 61
  • 114
  • 2
    What is the desired output? I'm pretty sure someone has already asked about this exact hw problem... – juanpa.arrivillaga Jan 16 '18 at 11:33
  • But, it isn't at all clear to me what output you are expecting. – juanpa.arrivillaga Jan 16 '18 at 11:38
  • Are `candidate1/2/3/4/5vote1/2/3` `int` variables with that respective name? – user2390182 Jan 16 '18 at 11:40
  • 2
    Even though you use the word "sort" and the tag `sorting`, your question doesn't seem to have anything to do with sorting. – John Coleman Jan 16 '18 at 11:47
  • 2
    The wording isn't clear. You seem to want to tabulate, for each column, the counts of 1,2 and 3 for that column. It isn't clear what the output is. It would be natural to put it in a 5x3 or 3x5 array, but your wording seems to suggest that you have 15 named variables. In any event -- if you want help on your homework then you should show your efforts. – John Coleman Jan 16 '18 at 11:59
  • The most pythonic way to do this sort of tabulation might be to use `[Counter(votes) for votes in zip(*ballots)]` (where `Counter` is from the `collections` module). – John Coleman Jan 16 '18 at 12:16

2 Answers2

0

First want to clarify.. so you intend to collect not just votes for each candidate, but the vector of preference votes (1,2,3) for each candidate?

  1. Understand you are dealing with nested list and how to index them. (you would use the term array for those types in numpy library)
  2. when you index list, you access the data from outside to inside. e.g. [outer][inner] (outer/inner as there could be more than 2 nested list)

Now that you know this, given that you don't have memory/time constraints, and since you seem to be not so comfortable with python..I'd suggest you use double for loop. Let's make a nested list of candidate with preference. Their outer index will be candidate #, inner lists with preference.

len(ballot) gives you the # of rows, (let's just say for convenience) 5 you already have for columns. work out the indentation please..

candidate = [[0]*4 for n in xrange(5)] //depends on your choice - whether you want to count for number of 0s, if you want to match position and preference..
n = len(ballot)
for i in range(0, n): //python index starts with 0, if you use range it includes the start number but not the last. google if you don't know
    for j in range(0, 5):
        if ballots[i][j] == '1':
            candidate[j][1] +=1
        elif ballots[i][j] == '2':
            candidate[j][2] +=1
        elif ballots[i][j] == '3':
            candidate[j][3] +=1
        else: //0
            candidate[j][0] +=1
dia
  • 431
  • 2
  • 7
  • 22
  • yea? but I really think the rest is up to the user to figure out.. for now when I tried the command it ain't causing an error. you may add your knowledge if you would be so kind. – dia Jan 16 '18 at 12:03
  • read [this](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) it's a logic error – juanpa.arrivillaga Jan 16 '18 at 12:05
  • 2
    It is up to the person who posts an answer to Stack Overflow to make sure that their answer doesn't contain a bug. A run-time error isn't the only type of bug. Your code has a logic error. – John Coleman Jan 16 '18 at 12:05
  • I see. well thanks for the lesson. I've seen too many examples opposite the case. but I shouldn't keep practicing them ;) and I corrected based on your link @juanpa.arrivillaga – dia Jan 16 '18 at 12:15
  • 1
    I think this is [an attempt to make us do his/her homework/assignment](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions)). Successfully, I would say. – Mr. T Jan 16 '18 at 12:53
-1

Like this you can put each answer in a list:

c1= list()
c2= list()
...

for i in ballots:
    c1.append(i[0])
    c2.append(i[1])
    ...
P. Duarte
  • 59
  • 1
  • 9