How do I compare the length of a string to an integer within the same list?

David Sorrell

My assignment is to get a list from the user and print out the second largest item in said list.

The user should be able to enter strings or integers.

I'm having issues comparing the two as I get errors when using max().

Here is my code thus far:


list_input_amount = int(input('How many items are in your list?: '))

for amount in range(list_input_amount):
    list_input = input('Please enter your list item: ')
    if list_input.isnumeric():
        random_list.append(int(list_input))
    else:
        random_list.append(list_input)

print(random_list)


def second_largest():
    maximum_list = set(random_list)
    maximum_list.remove(max(maximum_list))

    print(max(maximum_list))

second_largest()

Thanks in advance for the help

azro

You can a dict {} to store the value that'll be used to compare, and then sort and take the second one.

Here is the code, a bit more generic to get the nth element :

# List comprehension
def nth_largest(values: {}, nth=0):
    return [k for k,v in sorted(values.items(), key=lambda kv:kv[1], reverse=True)][nth][0]

# Two steps
def nth_largest(values: {}, nth=0):
    sorted_x = sorted(values.items(), key=lambda kv: kv[1], reverse=True)
    return list(sorted_x)[nth][0]


if __name__ == '__main__':
    list_input_amount = int(input('How many items are in your list?: '))
    dico_values = {}
    for amount in range(list_input_amount):
        list_input = input('Please enter your list item: ')
        if list_input.isnumeric():
            dico_values[int(list_input)] = int(list_input)
        else:
            dico_values[list_input] = len(list_input)
    print(nth_largest(dico_values, 1))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I maintain the same length of a string?

How do I compare pairs within a list in Python?

How do I compare an integer with a character in a string in C?

How do I obtain a string from a list and compare it to another string?

Find the length of an integer within a string

In Python, how do I restart a for loop within a while loop? The furthest out loop is a while loop that runs while r < y(length of integer list)

How do I compare user input to an uppercase letter string in a list?

How do I compare an object in python to an integer

How can I change column names of data.frames within a list with vector of same length in R?

How do I edit a string within a list of lists?

How do I a display a list as a table within an email body string?

Compare string and integer in same if statement

Once I have the length of a string, how do I go about capturing the length number as an integer variable for use elsewhere?

How to compare values with same index position between sublists within a list?

How to compare multiple strings within in the same list in Haskell

How do I add all integers in one list to integers on another list with different length in the same order in Python?

Compare length of strings within list items

How do I declare a new string the same length of a known const string?

How do I convert a BufferedReader-Input (String) to Integer and save it in an Integer List in java?

How do I make a list item same width as others, within an unordered list?

How do I add a string array within a string array array in a string list?

How to compare two dictionaries in python with list, string and integer?

How do you compare a list with an integer in an 'if' statement? Python

How do I split individual character within a list that is within a list?

Python3 how to add string to integer within a list

How do i compare a [null] list?

How Do I Compare a List to a Database Prolog

How do I compare a Unicode string that has different bytes, but the same value?

How do I check if a string is made up exclusively of same-length character groups?