I can’t find the highest value in a flip-book dictionary

Asked

Viewed 56 times

-2

I need to find the highest dictionary value using a function running with the 'for' loop I was able to find the values of all the other sets, except the dictionary values. To generate the dictionary values I use:

a = dict()
a['A'] = sample(range(0, 200), 50)

and to go through the values I use:

for v in a.values():
    if v == 0:
        mai = v
    else:
        if v > mai:
            mai = v

print(f'O maior valor do dicionário é {mai}')

But it always returns the same error:

if v > mai:
TypeError: '>' not supported between instances of 'list' and 'int'

I have already tried to make a list receive only the values of the dictionary but still the error persists, if anyone can help I would be grateful.

2 answers

1


This is happening because your dictionary has a list on A:

a['A'] = sample(range(0, 200), 50)

You can opt for some changes to your code, such as iterating the position A dictionary:

for v in a['A']:
    if v == 0:
        mai = v
    else:
        if v > mai:
            mai = v

Or who knows, check the type of data present in v with the function type within the loop and thus make a decision on how to treat:

for v in a.values():
    if type(v) == list:
        for x in v:
            if x > mai:
                mai = x
    elif type(v) == int:
      if v > mai:
          mai = v

print(f'O maior valor do dicionário é {mai}')

1

There are some problems with your code, but it is very simple to solve. I will try to explain well. First let’s look at the form of the dictionary you created.

a = dict() 
a['A'] = sample(range(0, 200), 50)

Printing your dictionary we see that it has only one 'A' key whose value is the list of random numbers created with the sample:

>>> print(a)
>>> {'A': [163, 126, 71, 15, 51, 120, 80, 182, 67, 169, 64, 161, 72, 21, 11, 118, 198, 134, 45, 141, 63, 199, 176, 168, 106, 65, 103, 123, 79, 1, 87, 159, 160, 155, 184, 151, 181, 99, 128, 111, 186, 101, 68, 75, 36, 147, 13, 50, 10, 135]}

When printing the dictionary values we have the result below. Note that the value is a list within a list with the values.

>>> print(a.values())
>>> dict_values([[163, 126, 71, 15, 51, 120, 80, 182, 67, 169, 64, 161, 72, 21, 11, 118, 198, 134, 45, 141, 63, 199, 176, 168, 106, 65, 103, 123, 79, 1, 87, 159, 160, 155, 184, 151, 181, 99, 128, 111, 186, 101, 68, 75, 36, 147, 13, 50, 10, 135]])

Now that we understand the structure of the dictionary it is easier to work. The code below brings the correct result.

for value in a.values():
    maior = value[0] # Assuma que o primeiro valor é o maior e compare com os outros
    for x in value: # Nesse laço todos os valores vão ser comparados
        if x > maior:
            maior = x

print(f'O maior valor do dicionário é {maior}')

Alternative code

We can simply use the function max():

a = dict() 
a['A'] = sample(range(0, 200), 50)
values = a['A']
maior = max(values)

print(f'O maior valor do dicionário é {maior}')

Browser other questions tagged

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