0

I have to perform the same operation on a number of arrays. Is there a way to use a loop in Python to carry out this repetitive task?

For example, I have 5 arrays: A, B, C, D, and E.

I want to carry out the following:

A = A[0]
B = B[0]
C = C[0]
D = D[0]
E = E[0]

Is there any way to do this using a loop or some other technique to avoid typing almost the same thing multiple times?

My question has been marked as a duplicate of this question. There, the person is asking how to extract the first element from each list (or in my case array). I am not simply trying to extract the first element. I actually want to replace each array with it's first element -- literally A = A[0].

Some are saying this is not a good idea, but this is actually what I want to do. To give some context, I have code that leaves me with a number of 2D arrays, with shapes n x m. When n = 1, the first dimension is irrelevant, and I would like to dispense with that dimension, which is what A = A[0] does. My code needs to handle both cases where n = 1 and cases when n > 1.

In other words, when n = 1, my code results in an array A that is of the following form: A = array([[a]]), and I want to change it so that A = array([a]). And to reiterate, I need the flexibility of allowing n > 1, in which case A = array([[a1],[a2],...]). In that case, my code will not execute A = A[0].

The solution by Mark White below works, if you change the last line to:

A,B,C,D,E = [x[0] for x in [A,B,C,D,E]]

What's interesting is that this solution makes the code more compact, but actually involves as many characters as (an technically more than) the brute force approach.

profj
  • 311
  • 3
  • 10
  • 3
    This specific task can be done as `A, B, C, D, E = next(zip(A, B, C, D, E))` – Paul Panzer Sep 30 '18 at 03:20
  • So you want to convert an array to a scalar which equals its first value? I do not think it is a good idea. – yann Sep 30 '18 at 08:29
  • @PaulPanzer In my case, A, B, etc. are arrays. When I try your solution, I get `TypeError: list object is not an iterator`. – profj Sep 30 '18 at 20:22
  • @yann The array is a 2D array. I've provided further context, but this is what I want to do. So, that makes it a good idea. – profj Sep 30 '18 at 20:23
  • 1
    @profj Sounds like you are on Python2. You can do `from itertools import izip` and then use `izip` instead of `zip`. – Paul Panzer Sep 30 '18 at 20:29

2 Answers2

1

I do think it is a pretty easy problem ,in my case ,I use three array ,but I think you can do five , my dear friend!

A = [1,3,4]
B = [2,4,5]
C = [54,5,6]
A,B,C = [x[0] for x in [A,B,C]]
Mark White
  • 640
  • 1
  • 5
  • 12
  • I wasn't just trying to extract the values, but replace the A with A[0]. Your solution works if I change the last line to `A,B,C = [x[0] for x in [A,B,C]]`. Thanks. – profj Sep 30 '18 at 20:26
  • @profj I do update answer because of your comment. – Mark White Oct 11 '18 at 14:31
0

Simply create a list of lists (or, specifically, references to A, B, C, etc.)

l = [A, B, C, D, E]

Then, you can iterate over l however you choose.

for i in range(len(l)):
    l[i] = l[i][0]

Note that this will update l[i] rather than the lists (A, B, C, ...) themselves.

Neil
  • 1,275
  • 11
  • 25
  • I actually want to update the A, B, C, etc. I don't want to create a new list with their values. – profj Sep 30 '18 at 20:08