用Python在2D数组中进行计数和计算 [英] Count and calculation in a 2D array in Python
本文介绍了用Python在2D数组中进行计数和计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有47个要使用以下格式的Python进行分析的数据集,并将数据存储在2D数组中:
I have 47 set of data to be analysised using Python with the following forma t and I stored the data in 2D array:
2104,3,399900 1600,3,329900 2400,3,369000...
I使用len函数打印存储在数组中的项目。 (上一个错误,并更改为以下代码。)
I use len function to print the item stored in array. (The previous one has made some mistake and change to following code.)
array: with open("abc.txt", "r") as ins:
substrings = data.read().split()
array = [map(int, substring.split(',')) for substring in substrings]
print(len(array)[0])
我也想做的一部分对于像athos格式的数组中的每个a,b和c进行如下计算:
A part from that I also would like to do some calculation like this for each a, b and c in array like thos format:
(2104-500)**2+(1600-500)**2+...
(3-2)**2+(3-2)**2...
我写道:
for [a for a, b, c in array] in range (len(array)[0]):
calculation_1 = ([a for a, b, c in array]) - 500)**2
for [b for a, b, c in array] in range (len(array)[1]):
calculation_2 = ([b for a, b, c in array]) - 2)**2
如何改进代码以提供所需的答案?
How can I improve the code to give the answer I want?
推荐答案
实际上,您有一个多维数组([] [])。
In fact, you have a multi-dimension array ([][]).
数组的len确实是一个,但是如果这样做
The len of array is really one, but if do this
len(array[0])
应为95。
希望有帮助
这篇关于用Python在2D数组中进行计数和计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文