-1

So I have this list and a function that calculates the scores of my teams. i then put the team name and the score in a separate dictionary but the problem is that i have a few duplicate teams in this list. theres a second item which is whether or not the team response was valid if the result was this: team1 - score 100 - validresponse 0 i just want to get rid of the team even if its a duplicate, however of theres two duplicates of the SAME team and both their submissions were valid then i want to add their scores together and set it as one thing in the dictionary. the only problem is that when doing this, the dictionary automatically disregards the other duplicates.

Here's my code:

import numpy as np
import pandas as pd

mylist = []
with open("input1.txt", "r") as input:
    for line in input:
        items = line.split()
        mylist.append([int(item) for item in items[0:]])

amountOfTestCases = mylist[0][0]
amountOfTeams = mylist[1][0]
amountOfLogs = mylist[1][1]

count = 1

count2 = 1

mydict = {}

teamlist = []

for i in mylist[2:]:
    count2 += 1
    teamlist.append(mylist[count2][1])

def find_repeating(lst, count=2):
    ret = []
    counts = [None] * len(lst)
    for i in lst:
        if counts[i] is None:
            counts[i] = i
        elif i == counts[i]:
            ret += [i]
            if len(ret) == count:
                return ret

rep_indexes = np.where(pd.DataFrame(teamlist).duplicated(keep=False))

print(teamlist)
print(rep_indexes)

duplicate = find_repeating(teamlist)


def calculate_points(row):
    points = mylist[row][3] * 100
    points -= mylist[row][0]
    return points

for i in teamlist:
    count += 1
    mydict['team%s' % mylist[count][1]] = calculate_points(count)

print(mydict)

the teamlist = [5, 4, 1, 2, 5, 4]

vapouryh
  • 41
  • 6
  • 1
    Your *question* looks like it is too broad - it doesn't even look like you have asked a question. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. Have you winnowed down your [mre] to just the part that is giving you trouble? – wwii Feb 25 '21 at 15:07
  • When you stepped through the code by hand or using the debug features of your IDE was there some point in the execution that things started going awry? If you are using an IDE **now** is a good time to learn its debugging features . [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Feb 25 '21 at 15:11
  • I posted the code for context, I don't understand how the question is broad. I'm simply asking how to solve my problem of python ignoring my other duplicates after putting the first team in a dictionary – vapouryh Feb 25 '21 at 15:12
  • It's a bug but we know the problem, code runs fine. If you look at my last for loop you can see how it assigns teams to the dictionary but since duplicates in the list, it only takes the first duplicate and ignores the other – vapouryh Feb 25 '21 at 15:13
  • Why aren't you checking to see if the *new* item is a duplicate and taking the appropriate action if it is? – wwii Feb 25 '21 at 15:15
  • I'm managing a scoreboard sheet in a textfile, there can be multiple people on a team. If there response isn't valid (aka 0) then get rid of them. If there response is 1, calculate their scores and add them together – vapouryh Feb 25 '21 at 15:17
  • Related: [Check if a given key already exists in a dictionary](https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary), [Python update a key in dict if it doesn't exist](https://stackoverflow.com/questions/42315072/python-update-a-key-in-dict-if-it-doesnt-exist), – wwii Feb 25 '21 at 16:04

1 Answers1

0

validresponse 0 i just want to get rid of the team even if its a duplicate

  • check if the response is valid
    • if invalid continue without doing anything else

duplicates of the SAME team and both their submissions were valid then i want to add their scores together

  • check if the key/team already exists (a duplicate)
    • if it exists
      • get its value
      • add the new value
      • assign the result to that dictionary key
    • if it is not a duplicate
      • make a new key with that value
wwii
  • 23,232
  • 7
  • 37
  • 77