Home > front end >  How to return the name of the someone with the largest value
How to return the name of the someone with the largest value

Time:12-09

I am new to python and have a question about this code (and am not allowed to use pandas). I have a CSV file (called personal_info.csv) with different columns labeled full_name, weight_kg, height_m. I am trying to create a function that will find the largest number in the weight_kg column and return the corresponding name. The numbers are a float and I am trying to return a string (the name).

This is all I have so far. I am not sure how to go to a specific column in the csv file and how to find the corresponding name.

for number in heights:
    if number > largest_number:
        largest_number = number

print(largest_number)

I also saw this code but am having trouble applying it here

def max_employment(countries, employment):
    max_value_index = np.argmax(employment)
    max_value = employment[max_value_index]
    max_country = countries[max_value_index]

    return (max_country, max_value)

CodePudding user response:

I would recommend to use enumerate (or range) in your largest_number finding algorithm. Enumerate (or range) will provide you an index of the item, which you also save once you find larger number. I.e., you need to store two values: the largest value and also the index of the largest value. Then, the name at the index is the name you are looking for.

This page nicely describes the reason behind enumerate, range and indices:

https://realpython.com/python-enumerate/

Once you master this, you can try more ways such as creating a dictionare with keys (names) and values, and using max function or use numpy library. However, all these lead into the same logic as described.

CodePudding user response:

Suppose you have this csv file:

Name,Wt,Ht
Bob,50.5,60.1
Carol,49.2,55.2
Ted,51.1,59.9
Alice,48.2,54.33

You can read the file with csv module:

import csv 

with open(fn) as f:
    reader=csv.reader(f)
    header=next(reader)
    data=[[row[0]] list(map(float, row[1:])) for row in reader]

Result:

>>> data
[['Bob', 50.5, 60.1], ['Carol', 49.2, 55.2], ['Ted', 51.1, 59.9], ['Alice', 48.2, 54.33]]

Then you can get the of some value using the max function with a key function:

>>> max(data, key=lambda row: row[1])
['Ted', 51.1, 59.9]

You can use the header value in header to get the index of the column in the csv data:

>>> max(data, key=lambda row: row[header.index('Wt')])
# same row

Then subscript if you just need the name:

>>> max(data, key=lambda row: row[1])[0]
'Ted'
  • Related