Python Forum
How to join elements from two arrays in a third one?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to join elements from two arrays in a third one?
#1
Hi guys,

I have two arrays "p" and "prec" as the following.
p:
Output:
[[  2 -30  20   4] [  2 -28   30   4] [  2  19  50   6] [  1 -27  60   3] [  2   3  71   6]]
prec:
Output:
[[ 0  0.1  0  0.1] [ 0  0.1  0  0.1] [ 0  0.1  0  0.1] [ 0  0.1  0  0.1] [ 0  0.1  0  0.1]]
I'd like to construct a third array that joins element by element from the previous arrays with the Latex symbol '$\pm$' in the middle, like this:
Output:
[[  2 ± 0  -30 ± 0.1  20 ± 0  4 ± 0.1] [  2 ± 0  -28 ± 0.1  30 ± 0  4 ± 0.1] [  2 ± 0  19 ± 0.1  50 ± 0  6 ± 0.1] [  1 ± 0  -27 ± 0.1  60 ± 0  3 ± 0.1] [  2 ± 0  3 ± 0.1  71 ± 0  6 ± 0.1]]
So every element of the new array is composed of two numbers with ± in the middle. I tried different approaches, but I still have no idea about how to do it...
Thanks for the help !!
Reply
#2
This calls for the zip function. You want to zip the two matrices, and then loop over that. that will give you two rows each time through the loop:

for row1, row2 in zip(matrix1, matrix2):
Then you want to do the same thing with the two rows: zip them and loop over the pairs of items. Then you just need the format method of strings to get the plus/minus in there. If you want the plus/minus to show in Python, you will need to find the unicode for it. Otherwise you can just put in the latex code, and let latex process it.

We are more into helping people with their code than writing code for people around here. Why don't you try to put together what I've shown you, and we'll help you with any problems you have.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-12-2017, 01:30 PM)ichabod801 Wrote: This calls for the zip function. You want to zip the two matrices, and then loop over that. that will give you two rows each time through the loop:

for row1, row2 in zip(matrix1, matrix2):
Then you want to do the same thing with the two rows: zip them and loop over the pairs of items. Then you just need the format method of strings to get the plus/minus in there. If you want the plus/minus to show in Python, you will need to find the unicode for it. Otherwise you can just put in the latex code, and let latex process it.

We are more into helping people with their code than writing code for people around here. Why don't you try to put together what I've shown you, and we'll help you with any problems you have.

First of all, thanks for your help.

Second, so sorry. That's wasn't my intention to receive a full code for my problem, but only the idea of how I could approach the question. I already tried a lot of crazy ideas to solve this, but without success until this moment. My main problem is to construct a table that's every cell is filled with the elements of the third array that I posted previously.

In the end, the solution was easier than I was trying and really close to what you propose. Bellow, I let the code that I wrote to subplot a table with the characteristics that I need. I believe that it can be easily adaptable to other problems:
# p is the array which contains the values of my variables
# prec is the array which contains the precision of the p values and has the same size

rows = ['%d' % i for i in np.arange(1,prism+1)] # numbering rows based on the number of rows of p stored in "prism"
columns = ('Mt (A)','inc ($^\circ$)','x0 (m)','z0 (m)') # headers of columns
   
cell_text = [] # list to receive the table inputs
for row in range(prism):
   cell_text.append(['%.1f $\pm$ %.1f' % item for item in zip(p[row,0:4],prec[row,0:4])]) # filling cells with data from two arrays

ax6=plt.subplot2grid((3,3), (2,2), colspan=1) # the original figure has six subplots
ax6.table(cellText=cell_text,rowLabels=rows,colLabels=columns,loc='center') # generating the table
ax6.get_xaxis().set_visible(False)
ax6.get_yaxis().set_visible(False)
ax6.set_title('f)', fontsize=12, fontweight="bold", position=(-0.05,0.85)) # index to the subplot in the figure

plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to join by stack multiple types in numpy arrays caro 1 1,110 Jun-20-2022, 05:02 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,014 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,550 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,310 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,205 Feb-14-2019, 08:34 PM
Last Post: woooee
  How to sum up the elements of an integer using python split/join? mohanraj1986 5 3,462 Aug-27-2018, 09:13 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020