Home > database >  program using lists in python
program using lists in python

Time:11-15

So i have a class exercice that i have to make a program that gives me all the information about the youngest person of a group, i could do the age, it gives me the youngest age but with the names and citizen cards could not get what i've wanted.

thats the code the way i tried do make it.

persons = []
ages = []
numbers_of_citizen_card = []

i = int(input('type the number of people in the group: '))

for i in range (0, i):
    name = input('Type your name: ')
    age = int(input('Type your age: '))
    n_cc = int(input('Type the number of your citizen card: '))
    persons.append(name)
    ages.append(age)
    numbers_of_citizen_card.append(n_cc)

if (i > 999):
    print('It is not possible to sign up more people. ')
else:
    print('The youngest person in the group with {} years old, is named {} with the number of citizen card of {}.'.format(min(ages), min(persons), min(numbers_of_citizen_card)))

CodePudding user response:

min is defined differently for different types. The minimum of name is going to return the minimum value lexicographically. You want to access the same person in each of the lists, so you should find the index i of the minimum age, and then the minimum name will be persons[i], and the minimum card number will be numbers_of_citizen_card[i].


If you want to do this without lists:

min_person = None
min_age = float('inf')
min_card_number = None

i = int(input('type the number of people in the group: '))

for i in range (0, i):
    name = input('Type your name: ')
    age = int(input('Type your age: '))
    n_cc = int(input('Type the number of your citizen card: '))
    # check if `age` is smaller than `min_age`, if it is
    # then overwrite the other variables

CodePudding user response:

It would be better if you saved your data in one array/dictionary of objects and get all the data of the object that has the smallest age.

Right now, you correctly get the smallest age with min but when you do the same with names, you get the "smallest" name by alphabetic order,regardless the smallest age in a different array. Same thing with the card number.

CodePudding user response:

You want to extract the minimum by age, then extract its attributes.

You can keep three parallel lists, but often it's better to keep a single list where each member has a number of attributes.

persons = []  # list of dict

while True:
    name = input('Type your name, or an empty line to quit: ')
    if name:
        age = int(input('Type your age: '))
        n_cc = int(input('Type the number of your citizen card: '))
        persons.append({'name': name, 'age': age, 'n_cc': n_cc})
    else:
        break

youngest = min(persons, key=lambda x: x['age'])

print('The youngest person in the group with {} years old, is named {} with the number of citizen card of {}.'.format(youngest['age'], youngest['name'], youngest['n_cc']))

This still doesn't work correctly in the case of a tie; min will then simply pick the first element from those which which sort equally.

CodePudding user response:

Your code is lacking the function to even look for the youngest person. What you should be looking into is the min() function and then just find the index of instance in the list to also print out their name and number of citizen card. So something like:

youngest = min(ages)
i = index(ages)

and just return the lists all at index i like:

print(list[i])

CodePudding user response:

Straight Forward Approach

If you are not going to create a custom class and define how the sorting methods should work, the simplest way to integrate, the already suggested, min function, would be to create a multidimensional array containing the name and age of the persons. It would have a data type that would look like:

persons = [["person1", "age1"], ["person2", "age2"], etc...]

then you can use the key parameter to filter the data on the age to find the minimum. You can also sort dictionaries, you just have to define how you want to use the min function on them, like below.

youngest = min(persons, key=lambda person: int(person[1]))
print('The youngest person in the group with {} years old, is named {} with the number of citizen card of {}.'.format(youngest[1], youngest[0]))

OOP Approach

Define your class and the __lt__ method for it - and apparently thats enough to sort it.

class Person:
     def __init__(self, name, age, pid):
         self.name = name
         self.age = age
         self.pid = pid

     def __lt__(self, other):
         return self.age < other.age


l = [Person("person1", 12, 19), Person("person2", 13, 20), Person("person3", 9, 17)]
l.sort()

for p in l:
    print(f"{p.name = } {p.age = } {p.pid = }")

Free Tidbit of Advice

If you can dodge doing something like this

ages = [10, 5, 3, 9, 7, 6]
names = ["p1", "p2", "p3", "p4", "p5", "p6"]
youngest = min(names)
age_index = ages.index(youngest)
names[age_index]

, then dodge it. Its slower. Maybe not a whole lot slower, but its definitely slower than the 2 methods above, in terms of sorting - instantiating the objects is arguably a slower method, but I haven't timed it - its just useful to have for in the future.

  • Related