Home > Mobile >  How can I search the value in list when get the input value from users? python
How can I search the value in list when get the input value from users? python

Time:11-07

I'm asking a user to input a year from the keyboard. For example users input 2008, How can I get the maximum value of the year from list that user input the value of year?

This is my code:

data_list = [(20070101, 619), (20070102, 615), (20070103, 614), (20080104, 845), (20080105, 840), (20080106, 835), (20090107, 940), (20090108, 970), (20090109, 939), (20090110, 936)]
value_year = 0
input_year = input("Enter year >>>")
for date, value in data_list:
    result = str(date)
    if result[0:4] == input_year:
        if value > value_year:
            value_year = value
print ("Maximum of this year:", result, value_year)

The output should be like this. when user input 2008. how to do that?

Maximum of this year: 20080104 845

CodePudding user response:

You can use python's built-in max() function.

https://docs.python.org/3/library/functions.html#max

data_list = [(20070101, 619), (20070102, 615), (20070103, 614), (20080104, 845), (20080105, 840), (20080106, 835), (20090107, 940), (20090108, 970), (20090109, 939), (20090110, 936)]
input_year = input("Enter year >>>")
result = max(
    (x for x in data_list if str(x[0])[:4] == input_year), 
    key=lambda x:x[1]
)
print ("Maximum of this year:", result)

CodePudding user response:

I just wanna use your way to solve the problem and do not doing this with new algorithm(You can solve this in better way) so:

you can use enumerate() function and this:

data_list = [(20070101, 619), (20070102, 615), (20070103, 614), (20080104, 845), (20080105, 840), (20080106, 835), (20090107, 940), (20090108, 970), (20090109, 939), (20090110, 936)]
value_year = 0
input_year = input("Enter year >>>")
target_index = None
for index, (date, value) in enumerate(data_list):

    if str(date)[0:4] == input_year:
        if value > value_year:
            value_year = value
            target_index = index


print ("Maximum of this year:", data_list[target_index][0], data_list[target_index][1])

Or do this:

data_list = [(20070101, 619), (20070102, 615), (20070103, 614), (20080104, 845), (20080105, 840), (20080106, 835), (20090107, 940), (20090108, 970), (20090109, 939), (20090110, 936)]
value_year = 0
target_tuple = None
input_year = input("Enter year >>>")
for data_tuple in data_list:

    if str(data_tuple[0])[0:4] == input_year:
        if data_tuple[1] > value_year:
            value_year = data_tuple[1]
            target_tuple = data_tuple

print ("Maximum of this year:", target_tuple[0], target_tuple[1])

CodePudding user response:

data_list = [(20070101, 619), (20070102, 615), (20070103, 614), (20080104, 845), (20080105, 840), (20080106, 835), (20090107, 940), (20090108, 970), (20090109, 939), (20090109, 87893),(20090110, 936),(20090110, 9360)]

ma=0
input=2009
for i,v in enumerate(data_list):
    if str(v[0])[:4]==str(input):
       ma=max(ma,v[1])
print(ma)

Using enumerate function you can easily traverse a list and get desired result

NOTE: this has time complexity is O(N) where N= len(data_list)

Better solution sort the list and the find the last occurrence of the input value

  • Related