Python Forum
help with adding duplicates elements together in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with adding duplicates elements together in a list
#4
It is a pity you are not allowed to use a dictionary. This makes it difficult to create a nice script.
As stated by Yoriz, this forum requires you to produce a program yourself and then you will get help to make it work. But I can give you some hints about the data structure and algorithm you may use.
Because a dictionary is not allowed, my advice is to create an intermediate data structure, fit for further processing. It should be a list of lists where each sublist starts with the student id, followed by the scores. Like this:
intermediate = [
                [6610013121, 4],
                [6610021021, 5],
                [6610000121, 3, 2, 2, 3]
               ]
The algorithm is then:
  1. Read the given list "d" in pairs of student_id and score.
  2. Scan the intermediate list to see if student_id already exist as intermediate[i][0]
  3. If it is: append the score to the sublist; else append a new sublist to the intermediate list containing [student_id, score].
  4. When this is all done you can start handling the intermediate list. First create an empty list for the results.
  5. If there are more than 4 scores in a sublist, find and remove the lowest.
  6. Summarize the scores of the sublist and append them to the results list in pairs of student_id and summarized score.

Please try to make a program of this and show it to us. If you get errors, show the complete error message.
Reply


Messages In This Thread
RE: help with adding duplicates elements together in a list - by ibreeden - Sep-09-2022, 08:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,630 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  Unexpected behavior accessing list elements. tonyflute 2 2,311 Apr-09-2021, 02:36 PM
Last Post: tonyflute
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,444 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  Dealing with duplicates to an Excel sheet DistraughtMuffin 6 3,362 Oct-28-2020, 05:16 PM
Last Post: Askic
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,348 Sep-24-2020, 02:26 PM
Last Post: buran
  Loop through elements of list and include as value in the dictionary Rupini 3 2,710 Jun-13-2020, 05:43 AM
Last Post: buran
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,114 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,345 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  adding parts of a list Eric7Giants 4 2,823 Nov-17-2019, 05:53 PM
Last Post: buran
  duplicates nsx200 3 2,499 Nov-12-2019, 08:55 AM
Last Post: nsx200

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020