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
#1
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
Reply
#2
Please see the links in my signature, pay particular attention to the homework link.
Reply
#3
(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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#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
#5
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
Reply
#6
(Sep-09-2022, 07:13 AM)Yoriz Wrote: Please see the links in my signature, pay particular attention to the homework link.

ok
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,353 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  Unexpected behavior accessing list elements. tonyflute 2 2,222 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,254 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  Dealing with duplicates to an Excel sheet DistraughtMuffin 6 3,197 Oct-28-2020, 05:16 PM
Last Post: Askic
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,280 Sep-24-2020, 02:26 PM
Last Post: buran
  Loop through elements of list and include as value in the dictionary Rupini 3 2,588 Jun-13-2020, 05:43 AM
Last Post: buran
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,035 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,194 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  adding parts of a list Eric7Giants 4 2,676 Nov-17-2019, 05:53 PM
Last Post: buran
  duplicates nsx200 3 2,353 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