How to add list values?

Asked

Viewed 697 times

1

I have a list of lists, and I wanted to add up the various values for the Indice 3 of each list, but only if there is a certain string in this list. How can I do?

For example I have this list:

[['ALABAMA', 'Abbeville', 2645, 11, 63], ['ALABAMA', 'Adamsville', 4481, 19, 321], ['ALABAMA', 'Addison', 744, 1, 25], ['ALABAMA', 'Alabaster', 31170, 44, 640], ['ALABAMA', 'Alexander City', 14692, 119, 661], ['ALABAMA', 'Aliceville', 2419, 7, 48], ['ALABAMA', 'Andalusia', 9079, 34, 491], ['ALABAMA', 'Anniston', 22648, 461, 1988], ['ALABAMA', 'Arab', 8295, 32, 640], ['ALABAMA', 'Ardmore', 1304, 2, 31], ['ALABAMA', 'Arley', 353, 3, 27], ['ALABAMA', 'Ashford', 2177, 2, 54], ['ALABAMA', 'Ashland', 1926, 9, 63],['ARIZONA', 'Chandler', 248718, 575, 5900], ['ARIZONA', 'Chino Valley', 10850, 50, 207], ['ARIZONA', 'Clarkdale', 4090, 5, 51], ['ARIZONA', 'Clifton', 3499, 11, 42], ['ARIZONA', 'Coolidge', 11820, 66, 662], ['ARIZONA', 'Cottonwood', 11285, 44, 401], ['ARIZONA', 'Eagar', 5034, 15, 106]]

And the statement says: Determine the name of the state with the most violent crime cases (corresponds to Dice 3).

  • And what was your difficulty? The answer "I couldn’t even begin" is not valid, as this only shows that you should review the content studied before trying again. Please edit your question, add the code you tried to do and explain what didn’t work.

2 answers

1


You can use the function sum(), look at you:

lst = [['ALABAMA', 'Abbeville', 2645, 11, 63], ['ALABAMA', 'Adamsville', 4481, 19, 321], ['ALABAMA', 'Addison', 744, 1, 25], ['ALABAMA', 'Alabaster', 31170, 44, 640], ['ALABAMA', 'Alexander City', 14692, 119, 661], ['ALABAMA', 'Aliceville', 2419, 7, 48], ['ALABAMA', 'Andalusia', 9079, 34, 491], ['ALABAMA', 'Anniston', 22648, 461, 1988], ['ALABAMA', 'Arab', 8295, 32, 640], ['ALABAMA', 'Ardmore', 1304, 2, 31], ['ALABAMA', 'Arley', 353, 3, 27], ['ALABAMA', 'Ashford', 2177, 2, 54], ['ALABAMA', 'Ashland', 1926, 9, 63],['ARIZONA', 'Chandler', 248718, 575, 5900], ['ARIZONA', 'Chino Valley', 10850, 50, 207], ['ARIZONA', 'Clarkdale', 4090, 5, 51], ['ARIZONA', 'Clifton', 3499, 11, 42], ['ARIZONA', 'Coolidge', 11820, 66, 662], ['ARIZONA', 'Cottonwood', 11285, 44, 401], ['ARIZONA', 'Eagar', 5034, 15, 106]]

# Todos os Estados
print sum([ item[3] for item in lst ])

# Alabama
print sum([ item[3] for item in lst if item[0] == 'ALABAMA' ])

# Arizona
print sum([ item[3] for item in lst if item[0] == 'ARIZONA' ])

Exit:

1510
744
766

To determine which is the most violent state, you can implement something like:

lst = [['ALABAMA', 'Abbeville', 2645, 11, 63], ['ALABAMA', 'Adamsville', 4481, 19, 321], ['ALABAMA', 'Addison', 744, 1, 25], ['ALABAMA', 'Alabaster', 31170, 44, 640], ['ALABAMA', 'Alexander City', 14692, 119, 661], ['ALABAMA', 'Aliceville', 2419, 7, 48], ['ALABAMA', 'Andalusia', 9079, 34, 491], ['ALABAMA', 'Anniston', 22648, 461, 1988], ['ALABAMA', 'Arab', 8295, 32, 640], ['ALABAMA', 'Ardmore', 1304, 2, 31], ['ALABAMA', 'Arley', 353, 3, 27], ['ALABAMA', 'Ashford', 2177, 2, 54], ['ALABAMA', 'Ashland', 1926, 9, 63],['ARIZONA', 'Chandler', 248718, 575, 5900], ['ARIZONA', 'Chino Valley', 10850, 50, 207], ['ARIZONA', 'Clarkdale', 4090, 5, 51], ['ARIZONA', 'Clifton', 3499, 11, 42], ['ARIZONA', 'Coolidge', 11820, 66, 662], ['ARIZONA', 'Cottonwood', 11285, 44, 401], ['ARIZONA', 'Eagar', 5034, 15, 106]]

def obterEstadoMaisViolento( lst ):
    d = {}
    for i in lst:
        if( i[0] in d ):
            d[ i[0] ] = d[ i[0] ] + i[3]
        else:
            d[ i[0] ] = i[3]
    return max( d, key=d.get )

print( obterEstadoMaisViolento( lst ) )

Exit:

ARIZONA

1

Assuming you already have the data separated by state, we can define a function that sums the crime rates:

def soma_criminalidade(estado):
    return sum(cidade[3] for cidade in estado[1])

But since the data are not grouped by state, we have to group them. For this, we use the itertools.groupby:

estados = itertools.groupby(data, key=itemgetter(0))

Thus, estados will be an eternal that groups all cities for each state. To find the state with greater crime, just do:

maior_criminalidade = max(estados, key=soma_criminalidade)

That generates the result:

>>> print(maior_criminalidade)
('ARIZONA', <itertools._grouper object at 0x7fec56788128>)

Indicating that for the data presented, the state with the highest crime is Arizona.

To know the crime of all the states it would be enough to do:

for estado in estados:
    criminalidade = soma_criminalidade(estado)
    print(f'O estado {estado[0]} possui criminalidade {criminalidade}')

That would generate the result:

O estado ALABAMA possui criminalidade 744
O estado ARIZONA possui criminalidade 766

Browser other questions tagged

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