Find the maximum and minimum number in a list in a string

Asked

Viewed 1,014 times

2

I have the following file:

a = '1 0 10 2 3'

I want to print the highest value and the lowest, so I used the max() and min() but it only counts the unit. I did it this way:

a='joao 1 0 10 2 3'
b=a.split()
c=max(b[1:])
print(c)
c=min(b[1:])
print(c)

exit:

3
0

out that I want:

10
0
  • It must be because the list is like string you’re gonna have to do something like b = list(map(int, b)) to convert to int

2 answers

7


The problem is you’re comparing strings and not numbers, so when you compare "10" to "3" the first is smaller, because analysis is done character by character, then the comparison is character "1" to character "3", and of course "3" is larger. This does not work, and to function as you will have to do strings in ints.

But there is a problem, you cannot guarantee that all items can be converted. You will have to deal with the exception that will be generated by transforming the entire text, so the code gets a little more complicated.

a = 'joao 1 0 10 2 3'
b = a.split()
c = []
for i in b:
    try:
        c.append(int(i))
    except ValueError:
        pass
print(max(c))
print(min(c))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Obviously the question does not make clear what to do with the strings, I had the understanding that should ignore and not generate error, after all the code would not be useful, and would not give the result that the AP asked in the question with input data, and I considered that the cited example of input is only to facilitate, that might or might not have texts even in the middle of the numbers in any position.

6

The problem is that when you share one string you receive a list of strings. The maximum and minimum function will use the same sort logic to identify the highest and lowest value respectively.

The point is that the ordination of strings is made alphanumerically. If you have the strings '1', '0', '10', '2' and '3', the value 3 will be the maximum, because in alphanumeric order, 3 comes after 1. Since the comparison is character by character, the Python interpreter does not treat 10 as integer, but as two characters.

Before setting the maximum and minimum, you will need to convert the values to numeric:

b = [int(n) for n in a.split()]
  • I reopened because it is not duplicate, it has a specificity, and because of this its gives error in the current form.

  • @Maniero would just change the a.split() for a.split()[1:]?

  • 1

    @Jeffersonquesado For this case I think it works, but what if it’s another database? What if it’s number one, what if it has other names? Of course, the question does not give criteria of what to do, but I think that it should not give error. Then fall into that, or close everything and only opens if the question has all the details in order, or the answers will always run the risk of being wrong when it is discovered that the question had other requirements.

  • 1

    Just to add: in such a case it would be enough to use b = [int(n) for n in a.split() if n.isdigit()], this way is only included and converted strings that contain only numbers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.