-2

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

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

I wrote:

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?

2
  • 1
    What exactly are you looking to count? What is your exact expected output? Is your data really just a list containing another single list? Please put together a better minimal reproducible example Commented Feb 25, 2017 at 14:45
  • 1
    What's \n doing there? Do you want to count that as one or two elements? And what's array: doing on the first line of your code? And what happened with the indentation? Please correct them, and clarify what you intend to do. Commented Feb 25, 2017 at 15:22

1 Answer 1

0

In fact, you have a multi-dimension array ([][]).

The len of array is really one, but if do this

len(array[0])

is should 95.

Hope it's help

Sign up to request clarification or add additional context in comments.

3 Comments

May be I should give out what I wrote in storing array: with open("abc.txt", "r") as ins: tmp = ins.read().split(" ") array = [i.split(",") for i in tmp]
@poonck1 modified my answer
That's not multi-dimensional array, that's just a list with a single element, which also happens to be a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.