Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly eliminate elements in dictionary until one string remains
    primarykey
    data
    text
    <p>I really need help on this</p> <pre><code>def get_winner (dict_winner): new_dict = {} for winner in dict_winner: first_letter = winner[0] value = dict_winner[winner] if first_letter in new_dict: new_dict[first_letter] += value else: new_dict[first_letter] = value return (new_dict) get_winner({ ('C', 'A', 'B', 'D') :3, ('D', 'B', 'C', 'A') :2, ('C', 'D', 'A', 'B') :1, ('A', 'D', 'B', 'C') :2, ('A', 'D', 'C', 'B') :4, ('A', 'C', 'D', 'B') :2 }) #Outputs {'A': 8, 'D': 2, 'C': 4} </code></pre> <p>Now I want the result be a tuple of str, NoneType..</p> <p>Also, it is eliminating only the letter with the smallest value in first place only one time. I want it to repeat this process until I get one winner in the end. So in this case all the B's will be eliminated in the dict itself, not in the output. for example:</p> <pre><code>first time = [8, 0, 4, 2] second time = { ('C', 'A', 'D') :3, ('D', 'C', 'A') :2, ('C', 'D', 'A') :1, ('A', 'D', 'C') :2, ('A', 'D', 'C') :4, ('A', 'C', 'D') :2 }) #Outputs C = 4 D = 2 A = 8 third time= { ('C', 'A') :3, ('C', 'A') :2, ('C', 'A') :1, ('A', 'C') :2, ('A', 'C') :4, ('A', 'C') :2 }) #Outputs C = 6 A = 8 </code></pre> <p>8/ 14 > 50%, I know that should have been the case since the beginning because <code>A</code> already had the majority value. But i am assuming <code>A</code> has a value of 40% which is when elimination should begin. So, could you point out where I went wrong in coding this? In the example <code>A</code> should be the winner! So the output shopuld be ('A', None)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload