简体   繁体   中英

Add numbers of a column within an array based of value from another column

I asked this question once but was very inconsistent in my wording. Here is my full code. I have a dataArray and wish to add numbers within the 5th column but only if within the same row, column 7 has a 0.

#!/usr/bin/python

#Date: 4.24.18


#importing necessary modules
import csv
import collections
import sys
from array import array

#variables for ease of use in script
fileName = 'medicaldata.tsv'
filePath = '/home/pjvaglic/Desktop/scripts/pythonScripts/final/data/'
dataURL = 'http://pages.mtu.edu/~toarney/sat3310/final/'
dataArray = []

sumBeds = 0
count = 0
countFac = 0
sumNSal = 0
sumNSalR = 0

#download file from MTU
downloadFile = urllib2.urlopen(dataURL + fileName)

#opening the file
with open(filePath + fileName, 'w') as output:
    output.write(downloadFile.read())
output.close()

#count number of lines in the data file, take off the header, print results to screen
count = open(filePath + fileName).readlines()
print "There are", len(count)-1, "facilities accounted for in", filePath + fileName
#keep track of number of facilities
countFac = len(count)-1

#open data file, put everything in an array, cut everything at the tab delimiter
with open(filePath + fileName, 'rt')  as inputfile:
    next(inputfile)
    dataArray = csv.reader(inputfile, delimiter='\t')
#sum the amount of beds are in the first column
    for row in dataArray:
        sumBeds += int(row[0])
    print "There are ", sumBeds, "in the medical file."
    print "There are about", sumBeds/countFac, "beds per facility."

    #this line does not work for my purposes.
    #list = [[row[4] for row in dataArray if row[6] == '1']]
    #print list

Here is the dataArray. The last column has 0's and 1's. I believe they are strings. For example, in the first row it has a 0, so I want to take 5230 and add that to 6304 and then 6590, so forth and so on. Just rows that include a 0 in the last column.

['244', '128', '385', '23521', '5230', '5334', '0']
['59', '155', '203', '9160', '2459', '493', '1']
['120', '281', '392', '21900', '6304', '6115', '0']
['120', '291', '419', '22354', '6590', '6346', '0']
['120', '238', '363', '17421', '5362', '6225', '0']
['65', '180', '234', '10531', '3622', '449', '1']
['120', '306', '372', '22147', '4406', '4998', '1']
['90', '214', '305', '14025', '4173', '966', '1']
['96', '155', '169', '8812', '1955', '1260', '0']
['120', '133', '188', '11729', '3224', '6442', '1']
['62', '148', '192', '8896', '2409', '1236', '0']
['120', '274', '426', '20987', '2066', '3360', '1']
['116', '154', '321', '17655', '5946', '4231', '0']
['59', '120', '164', '7085', '1925', '1280', '1']
['80', '261', '284', '13089', '4166', '1123', '1']
['120', '338', '375', '21453', '5257', '5206', '1']
['80', '77', '133', '7790', '1988', '4443', '1']
['100', '204', '318', '18309', '4156', '4585', '1']
['60', '97', '213', '8872', '1914', '1675', '1']
['110', '178', '280', '17881', '5173', '5686', '1']
['120', '232', '336', '17004', '4630', '907', '0']
['135', '316', '442', '23829', '7489', '3351', '0']
['59', '163', '191', '9424', '2051', '1756', '1']
['60', '96', '202', '12474', '3803', '2123', '0']
['25', '74', '83', '4078', '2008', '4531', '1']
['221', '514', '776', '36029', '1288', '2543', '1']
['64', '91', '214', '8782', '4729', '4446', '1']
['62', '146', '204', '8951', '2367', '1064', '0']
['108', '255', '366', '17446', '5933', '2987', '1']
['62', '144', '220', '6164', '2782', '411', '1']
['90', '151', '286', '2853', '4651', '4197', '0']
['146', '100', '375', '21334', '6857', '1198', '0']
['62', '174', '189', '8082', '2143', '1209', '1']
['30', '54', '88', '3948', '3025', '137', '1']
['79', '213', '278', '11649', '2905', '1279', '0']
['44', '127', '158', '7850', '1498', '1273', '1']
['120', '208', '423', '29035', '6236', '3524', '0']
['100', '255', '300', '17532', '3547', '2561', '1']
['49', '110', '177', '8197', '2810', '3874', '1']
['123', '208', '336', '22555', '6059', '6402', '1']
['82', '114', '136', '8459', '1995', '1911', '1']
['58', '166', '205', '10412', '2245', '1122', '1']
['110', '228', '323', '16661', '4029', '3893', '1']
['62', '183', '222', '12406', '2784', '2212', '1']
['86', '62', '200', '11312', '3720', '2959', '1']
['102', '326', '355', '14499', '3866', '3006', '1']
['135', '157', '471', '24274', '7485', '1344', '0']
['78', '154', '203', '9327', '3672', '1242', '1']
['83', '224', '390', '12362', '3995', '1484', '1']
['60', '48', '213', '10644', '2820', '1154', '0']
['54', '119', '144', '7556', '2088', '245', '1']
['120', '217', '327', '20182', '4432', '6274', '0']

I know there is a short hand way of placing all those numbers within a list and use a sum function to add them up. I'm just not sure of how to go about it.

There are 2 ways. Below I use only an extract of your data.

Setup

We assume you begin with a list of lists of strings.

lst = [['244', '128', '385', '23521', '5230', '5334', '0'],
       ['59', '155', '203', '9160', '2459', '493', '1'],
       ['120', '281', '392', '21900', '6304', '6115', '0'],
       ['120', '291', '419', '22354', '6590', '6346', '0'],
       ['120', '238', '363', '17421', '5362', '6225', '0'],
       ['65', '180', '234', '10531', '3622', '449', '1'],
       ['120', '306', '372', '22147', '4406', '4998', '1'],
       ['90', '214', '305', '14025', '4173', '966', '1'],
       ['96', '155', '169', '8812', '1955', '1260', '0']]

Pure Python

A = [[int(i) for i in row] for row in lst]

res = sum(row[4] for row in A if row[6] == 0)
# 25441

Vectorised solution

You can use a 3rd party library such as numpy :

import numpy as np

A = np.array(lst, dtype=int)

res = A[np.where(A[:, 6] == 0), 4].sum()
# 25441

Turn your data file into an array of arrays.

['244', '128', '385', '23521', '5230', '5334', '0']
['59', '155', '203', '9160', '2459', '493', '1']
['120', '281', '392', '21900', '6304', '6115', '0']

Instead:

[['244', '128', '385', '23521', '5230', '5334', '0'],
['59', '155', '203', '9160', '2459', '493', '1'],
['120', '281', '392', '21900', '6304', '6115', '0']]

Then iterate over the elements in the array of arrays looking for the string '0' then adding the element [i][4] to your total sum. You'll need to convert the strings to a number value to add them though, otherwise you'll get one long string of numbers instead of a sum.

    var sum = 0;

    for (i = 0; while i < dataArray.length; i ++) {

        if (dataArray[i][7] === '0') {
            var sum += Number(dataArray[i][4])
      }
    };

At the end of the loop you'll have your total in var sum and can do with it as you please.

Just realized your working in python, my answer is in javascript. Whoops. Might not be the best answer but if you find the python version of the above solution it should get you on the right track. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM