0

I am using Python 2.7 and started learning Python several days ago. I am still trying to get my head around how Python indexes arrays. The arrays start at zero instead of one. I am practicing on manipulating arrays around and could use some help.

I have 2 arrays - A and B - and I want to get the value in B that is parallel to the value in A.

Below is the code that I am messing around with to help me get proficient with arrays:

 A = [2, 8, 29, 34, 54, 61, 73, 89, 97, 101]

 B = [201, 309, 454, 540, 602, 750, 889, 973, 1003, 1120]


 B[2] = 454

 # B[2] = 201  this is what I want

 B[8] = 1003

 #B[8] = 309  this is what I want   

Both arrays are the same length, but as you can see, I am trying to work on the indexing. If I just do B[A], its an out of bounds index error code. I tried using:

 A.searchsorted(B) #but still get an error. 

I am not sure if I am making any sense, as I know what I want in my head but just can't get the correct wording out to make it easier. Hopefully the example above shows what I am wanting. The only thing I have found on the site is the searchsorted (maybe I am using it incorrectly). This method was brought to my attention from a previous question and I looked further into it, but still can't find what I am looking for.

VolAnd
  • 6,367
  • 3
  • 25
  • 43
Caroline.py
  • 293
  • 1
  • 4
  • 16
  • Your question does not make sense. What does `value in B that is parallel to the value in A` mean? 201 is not B[2], 201 is B[0]. A[0] is 2. B[A[0]] is also 454 since B[A[0]] == B[2] == 454. – alzee Aug 02 '16 at 01:36
  • I want B[2] = 201 even though B[0] = 201. When I mean by parallel, I mean having to arrays of similar length. Let's say we get them side by side. I want the value of one array corresponding to the other array. 2 and 201 are A[0] and B[0], respectively. I want to obtain the first value in B by using the first value in A, hence B[2] = 201, instead of B[0]. I am not sure if this is possible, but does this make a little more sense? – Caroline.py Aug 02 '16 at 01:51
  • You cannot get *the first value in B by using the first value in A*. The first value in A is *2*, and you can use that as an index with syntax `B[A[0]]` but this will not give you "the first value in B" but instead the 3rd value in B, since A[0] == 2, and array indexing starts at 0. **Edit: Simply, the first value in B is B[0], period. That is 201 in your example.** – alzee Aug 02 '16 at 01:53
  • Thanks. I was reading into a searchsorted example where A = [1, 4, 5, 8] and B = [ 0, 1, 3] and A.searchsorted(B) = [1,4,8]. I am trying to, I guess, see if I can use this, but with Python indexing, it is looking like it really is impossible. – Caroline.py Aug 02 '16 at 02:00
  • You may be misunderstanding searchsorted. See: http://stackoverflow.com/questions/15425962/how-does-searchsort-in-python-work – alzee Aug 02 '16 at 02:02
  • @Caroline.py: This may seem random, but multiple languages use 0 as the first index in arrays. (C, C++, PHP, Javascript, etc...) (Ok. So that was pretty random... ;) Oh, well.) – J. Allan Aug 02 '16 at 02:21
  • Are you trying to do something like this: `B[A.index(2)]`? – Will Aug 02 '16 at 03:23
  • @user866762 I think so. I am getting an error when I try that referring to the attribute for index. – Caroline.py Aug 02 '16 at 16:37
  • AttributeError: 'numpy.ndarray' object has no attribute 'index' – Caroline.py Aug 02 '16 at 17:26
  • You're mixing up lists and arrays. Never write an array like it's a list, and never use the terms like they're interchangeable. Arrays and lists are extremely different. – user2357112 Aug 02 '16 at 17:28
  • @user2357112 I renamed my arrays A and B to C and D using np.array(A) = C and np.array(B) = D. This means I am working with arrays? If so, I still get the same error. – Caroline.py Aug 02 '16 at 17:35
  • Hmm. yeah, an array is different than a list. maybe you could `toList` and then use `index`, but this seems like bad form. Maybe the real question is [what are you really trying to do](http://xyproblem.info/)? – Will Aug 02 '16 at 17:37
  • Not any project in particular. This just sparked my curiosity as I was reading about how Python indexes start at 0. I just wanted to see if there is a way to use values from another array to give me the value from another. Your answer below makes sense. I will keep playing around and see if I can get it to work. Thank you for your help. – Caroline.py Aug 02 '16 at 17:48

1 Answers1

0

It sounds like you're trying to find the index of a value from one list, and then return the value from the other list at that index.

A = [2, 8, 29, 34, 54, 61, 73, 89, 97, 101]
B = [201, 309, 454, 540, 602, 750, 889, 973, 1003, 1120]
print(B[A.index(2)])
print(B[A.index(8)])

Produces:

201
309

Note: this will cause a runtime error if the value does not exist in the first list:

print(B[A.index(42)])

will explode and destroy everything in a 5km radius. See https://ideone.com/dfVci9

Will
  • 2,163
  • 1
  • 22
  • 22