0

I have an 2d array of names and scores and i want remove name and score of student that have minimum score of array ,but when i add 2 or more student with minimum number continuous (fore example [['a',20],['b',10],['c',10],['d',10],['e',10]]) the program just remove first,third(odd) item's. Help me why this happens my code is:

student_list = []
for i in range(int(input())):
    student_list.append([])
    student_list[i].append(input("enter name : "))
    student_list[i].append(int(input("enter score : ")))
min_score = min(student_list,key=lambda detail: detail[1])[1]
for i in student_list:
    if (i[1] == min_score):
        student_list.remove(i)
print(student_list)
abolfazl
  • 5
  • 1
  • There are few related [existing questions](https://stackoverflow.com/questions/17217500/internal-min-function-of-python) . Please check those. –  Sep 16 '19 at 08:54
  • @abolfazi: could you please take a look at all the responses, and upvote the ones you think are helpful? – CypherX Sep 16 '19 at 16:06

4 Answers4

0

We can do this:

student_details = [['rock', 10], ['john', 20], ['jack', 10], ['tom', 15]]

min_score = min(student_details, key=lambda detail: detail[1])[1]

student_details_with_min_score = [
    student_detail for student_detail in student_details if student_detail[1] != min_score
]

print(student_details_with_min_score)

Sorry for selecting different variable names, but I think these names are more readable.

Dipen Dadhaniya
  • 4,550
  • 2
  • 16
  • 24
0

This can be easily done using pandas or numpy.

data = [['a',20],['b',10],['c',10],['d',10],['e',10]]

Solution: pandas

import pandas as pd

df = pd.DataFrame(data, columns = ['Name', 'Score'])
df_filtered = df.loc[df.Score>df.Score.min()]
df_filtered

Output:

    Name    Score
0   a       20

Solution: numpy

import numpy as np

names, scores = list(), list()
for name, score in data:
    names.append(name)
    scores.append(score)
s = np.array(scores)
d = np.array(data)
d[s>s.min(),:].tolist()

Output

[['a', '20']]
CypherX
  • 7,019
  • 3
  • 25
  • 37
0

It's never a good idea to modify a list while iterating over it. Adding some comments in your loop us shows this:

for i in student_list:
  print("Student: {}".format(i))
  if (i[1] == min_score):
    print("Removing student: {}".format(i))
    student_list.remove(i)

# Output

Student: ['a', 20]
Student: ['b', 10]
Removing student: ['b', 10]
Student: ['d', 10]
Removing student: ['d', 10]

As you can see, the list items are shifted since you had removed an item during iteration.

I would suggest just creating a new list that filters out the students that have the minimum score:

student_list = []
for i in range(int(input())):
    student_list.append([])
    student_list[i].append(input("enter name : "))
    student_list[i].append(int(input("enter score : ")))
min_score = min(student_list,key=lambda detail: detail[1])[1]

cleaned_student_list = [
  student
  for student in student_list
  if student[1] != min_score
]

print(cleaned_student_list)
fixatd
  • 1,394
  • 1
  • 11
  • 19
0

Just make the new list of your array and then iterate

for i in list(student_list):
    if (i[1] == min_score):
        student_list.remove(i)

Actually you were iterating and updating the same list by removing the item. What happen here is that python evaluate the length of list to iterate. So here you keep deleting them, shortening the list by 1. And then you move on to the next index. Hope this help.

Saleem Ali
  • 1,363
  • 11
  • 21