Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据另一列的值在数组中添加列的编号_Python_Arrays_List_Csv - Fatal编程技术网

Python 根据另一列的值在数组中添加列的编号

Python 根据另一列的值在数组中添加列的编号,python,arrays,list,csv,Python,Arrays,List,Csv,我问过一次这个问题,但措辞很不一致。这是我的全部代码。我有一个dataArray,希望在第5列中添加数字,但前提是在同一行中,第7列有一个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 =

我问过一次这个问题,但措辞很不一致。这是我的全部代码。我有一个dataArray,希望在第5列中添加数字,但前提是在同一行中,第7列有一个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
这是数据数组。最后一列有0和1。我相信它们是弦。例如,在第一行中,它有一个0,所以我想取5230,然后将其添加到6304,然后是6590,依此类推。仅在最后一列中包含0的行

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

我知道有一种简单的方法可以将所有这些数字放在一个列表中,然后使用求和函数将它们相加。我只是不知道该怎么做。

有两种方法。下面我仅使用您的数据摘录

设置

我们假设您从字符串列表开始

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']]
纯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
矢量化解决方案

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

res = sum(row[4] for row in A if row[6] == 0)
# 25441
您可以使用第三方库,如
numpy

import numpy as np

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

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

将数据文件转换为数组数组

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

[['244', '128', '385', '23521', '5230', '5334', '0'],
['59', '155', '203', '9160', '2459', '493', '1'],
['120', '281', '392', '21900', '6304', '6115', '0']]
然后在数组数组中的元素上迭代,查找字符串“0”,然后将元素[i][4]添加到总和中。但是,您需要将字符串转换为一个数值来进行相加,否则您将得到一个长字符串而不是一个和

    var sum = 0;

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

        if (dataArray[i][7] === '0') {
            var sum += Number(dataArray[i][4])
      }
    };
var总和=0;
对于(i=0;而i
在循环结束时,您将得到var sum中的合计值,并可以根据您的意愿进行处理


刚刚意识到你在用python工作,我的答案是javascript。哎呀。可能不是最好的答案,但如果您找到上述解决方案的python版本,它应该会让您走上正确的道路。干杯

您可以发布您的CSV吗?我如何将数组设置为列表?我试过lst=dataArray我不知道什么是
dataArray
。。尝试向我们展示
type(dataArray)
。然后按照尝试
lst=list(dataArray)
。我使用您的方法成功获得了它,感谢您的耐心和好意!