How do I add values from list lists?

1

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

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 more cases of violent crime (corresponds to index 3).

    
asked by anonymous 12.06.2018 / 19:49

2 answers

1

You can use sum() , see:

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' ])

Output:

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 ) )

Output:

ARIZONA
    
12.06.2018 / 20:13
1

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

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

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

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

Thus, estados will be an iterable grouping all cities for each state. To find the most criminal state, 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 rate is Arizona.

To know the criminality of all 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
    
12.06.2018 / 20:21