0

Lets say i have array like this :

 d= [[1.254,3.458,9.874][5.32,3.35,4.54][1.121,8.121,9.45][7.1215,9.454,8.14].... and etc.]

How i can divide d[0] [2] element with d[0] [2], d[1] [2],d[2] [2], d[3] [2] ....... and etc? I will explain it by using numbers:

  9.874(d[0] [2])/9.874(d[0] [2])
  9.874(d[0] [2])/4.45(d[1] [2]) 
  9.874(d[0] [2])/9.45(d[2] [2])
  9.874(d[0] [2])/8.14(d[3] [2]) ........ and etc.

And i want that my result would be in array :

  Result  = [1,2.21,1.04,1.21,... and etc.] 

Or (if its easier) result can be added to current array :

  d= [[1.254,3.458,9.874,1][5.32,3.35,4.54,2.21][1.121,8.121,9.45,1.04][7.1215,9.454,8.14,1.21].... and etc.]

As you can see result is the last array of array element:

  d[0] [3] = 1, d[1] [3] = 2.21, d[2] [3]=1.04, d[3] [3]=1.21

If anyone need more explanation please write in a comment section dont add -1 :(

4
  • 1
    Well, it's a pretty unclear question. Commented Nov 24, 2015 at 14:32
  • 1
    what section is unclear i can fix it Commented Nov 24, 2015 at 14:33
  • Are you trying to do matrix arithmetic? Some of the aspects that are unclear: division is not defined for arrays, 0 2 is a syntax error, not a valid array index, and numbers are not functions that can be called. Commented Nov 24, 2015 at 14:37
  • Please be aware that you don't have an array but a list. Commented Nov 24, 2015 at 15:06

4 Answers 4

3

This should work for your example:

result = [d[0][2]/dx[2] for dx in d]
Sign up to request clarification or add additional context in comments.

Comments

0

If you just want the last element of each inner list you could do

d= [[1.254,3.458,9.874,1],[5.32,3.35,4.54,2.21],[1.121,8.121,9.45,1.04],[7.1215,9.454,8.14,1.21]]

for i in d: print i[-1]

1
2.21
1.04
1.21

and if you want it as a list you can use list comprehension

l = [i[-1] for i in d]
print l

[1, 2.21, 1.04, 1.21]

Comments

0

If you're using Numpy, you can add a column to your array and fill it with the results of your division:

1: a = np.array([[1.254,3.458,9.874],[5.32,3.35,4.54][1.121,8.121,9.45],[7.1215,9.454,8.14]])
2: b =np.hstack((a, np.zeros((a.shape[0],1))))
3: b[:,3] = b[0,2]/b[:,2]
4: b
array([[ 1.254     ,  3.458     ,  9.874     ,  1.        ],
   [ 5.32      ,  3.35      ,  4.54      ,  2.17488987],
   [ 1.121     ,  8.121     ,  9.45      ,  1.04486772],
   [ 7.1215    ,  9.454     ,  8.14      ,  1.21302211]])

Comments

0

The simpler would be to compute a list comprehension (solution proposed by @elzell) result = [d[0][2]/dx[2] for dx in d]

You can also add the resul as last element of each sublist (array in numpy is a different animal)

for dx in d:
    dx.append(d[0][2]/dx[2])

2 Comments

Srr added answered for @elzell :) it was his idea first :)
@user5568235: I was about to suggest it to you. That's the reason why I had explicitely given him credit for his answer. Mine was to show the possibility of changing the array. I had already upvoted his answer :-)

Your Answer

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