Python Forum
help with adding duplicates elements together in a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: help with adding duplicates elements together in a list (/thread-38153.html)



help with adding duplicates elements together in a list - 2ECC3O - Sep-09-2022

given a list(just an example list, the actual test list is much MUCH longer)

d = [6610013121,4,6610021021,5, 6610021021,5,6610061121,5, 6610061121,5, 6610061121,1,6610000121,3, 6610000121,2, 6610000121,2, 6610000121,3]

where the long number is the student id number and the following number is the score(can be any positive or negative number) they receive.

The exercise is to add together the scores of each student and print them out as: list = [6610013121,4, 6610021021,10, 6610061121,11, 6610000121,8]
This requires me to find the duplicate id, add their respective points, and append that to a new list.
My problem is I can't figure out how to find duplicates id and add their scores together with these restrictions:
1. no dict() and set() allowed
2. no importing of library
3. if there are more than 4 scores remove the lowest one from the summation of that student. ex: [6610000121,3, 6610000121,2, 6610000121,2, 6610000121,3] is equal to [6610000121,8] since one of the "2" are considered lowest and removed


RE: help with adding duplicates elements together in a list - Yoriz - Sep-09-2022

Please see the links in my signature, pay particular attention to the homework link.


RE: help with adding duplicates elements together in a list - perfringo - Sep-09-2022

(Sep-09-2022, 05:10 AM)2ECC3O Wrote: 3. if there are more than 4 scores remove the lowest one from the summation of that student. ex: [6610000121,3, 6610000121,2, 6610000121,2, 6610000121,3] is equal to [6610000121,8] since one of the "2" are considered lowest and removed

Is it just me or now four and more than four are considered the same?


RE: help with adding duplicates elements together in a list - ibreeden - Sep-09-2022

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.


RE: help with adding duplicates elements together in a list - deanhystad - Sep-09-2022

If you pair the score with the student id you can sort. That may solve the entire problem. Sorting will not only group all of a student's scores, it will also sort the scores, making them easy to process.
d = [[6610013121 ,4], [6610021021 ,5], [6610021021, 5], ...]

After sorting it took very little work to prints this:
Output:
6610000121 [2, 2, 3, 3] 10 6610013121 [4] 4 6610021021 [5, 5] 10 6610061121 [1, 5, 5] 11



RE: help with adding duplicates elements together in a list - 2ECC3O - Sep-10-2022

(Sep-09-2022, 07:13 AM)Yoriz Wrote: Please see the links in my signature, pay particular attention to the homework link.

ok