Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem Table Output
#1
I'm new too python, would be glad if somebody could help me with my first Table.

My Code is the following:

import numpy as np  
import math
import sys
import argparse


#print("Hallo world!")
#print("\n")





B = 0.30 # thickness

m = 3 # numbers of Layer
s = 5 # Steps for one Layer
Z = 0.30 # Z-value
z1= 0.30
z2= B*m

i=1 # variable for Lines
n=1 # Layer counter
g=2
S_ges = s * m  # lines in total

while n<= m:
	
	for g in range(1,m, 1):
		
			
		I=np.linspace(i , S_ges , S_ges, dtype=int)
	
	
		table=np.zeros((S_ges, 3) )  # np.zeros(shape, dtype=float, order='C')


		table[:,0]=I
		table[:,1]=Z 
		table[:,2]=n

		i +=1

	print (table)
		
	
	n +=1
	Z += B
	g +=n

fmt = ['%i'] + 2 * ['%.3f']
np.savetxt("Loop.txt", table, fmt = fmt )
The Output is the following:

Output:
[[ 2. 0.3 1. ] [ 2. 0.3 1. ] [ 3. 0.3 1. ] [ 4. 0.3 1. ] [ 5. 0.3 1. ] [ 6. 0.3 1. ] [ 7. 0.3 1. ] [ 8. 0.3 1. ] [ 9. 0.3 1. ] [10. 0.3 1. ] [11. 0.3 1. ] [12. 0.3 1. ] [13. 0.3 1. ] [14. 0.3 1. ] [15. 0.3 1. ]] [[ 4. 0.6 2. ] [ 4. 0.6 2. ] [ 5. 0.6 2. ] [ 6. 0.6 2. ] [ 7. 0.6 2. ] [ 7. 0.6 2. ] [ 8. 0.6 2. ] [ 9. 0.6 2. ] [10. 0.6 2. ] [11. 0.6 2. ] [11. 0.6 2. ] [12. 0.6 2. ] [13. 0.6 2. ] [14. 0.6 2. ] [15. 0.6 2. ]] [[ 6. 0.9 3. ] [ 6. 0.9 3. ] [ 7. 0.9 3. ] [ 7. 0.9 3. ] [ 8. 0.9 3. ] [ 9. 0.9 3. ] [ 9. 0.9 3. ] [10. 0.9 3. ] [11. 0.9 3. ] [11. 0.9 3. ] [12. 0.9 3. ] [13. 0.9 3. ] [13. 0.9 3. ] [14. 0.9 3. ] [15. 0.9 3. ]]
The Output the Txt.-document is only the following:

Output:
6 0.900 3.000 6 0.900 3.000 7 0.900 3.000 7 0.900 3.000 8 0.900 3.000 9 0.900 3.000 9 0.900 3.000 10 0.900 3.000 11 0.900 3.000 11 0.900 3.000 12 0.900 3.000 13 0.900 3.000 13 0.900 3.000 14 0.900 3.000 15 0.900 3.000


But actually I am trying to save one Txt.-document which should look like the following Table: Huh

Output:
1 0.3 1. 2 0.3 1. 3 0.3 1. 4 0.3 1. 5 0.3 1. 6 0.3 1. 7 0.3 1. 8 0.3 1. 9 0.3 1. 10 0.3 1. 11 0.3 1. 12 0.3 1. 13 0.3 1. 14 0.3 1. 15 0.3 1. 16 0.6 2. 17 0.6 2. 18 0.6 2. 19 0.6 2. 20 0.6 2. 21 0.6 2. 22 0.6 2. 23 0.6 2. 24 0.6 2. 25 0.6 2. 26 0.6 2. 27 0.6 2. 28 0.6 2. 29 0.6 2. 30 0.6 2. 31 0.9 3. 32 0.9 3. 33 0.9 3. 34 0.9 3. 35 0.9 3. 36 0.9 3. 37 0.9 3. 38 0.9 3. 39 0.9 3. 40 0.9 3. 41 0.9 3. 42 0.9 3. 43 0.9 3. 44 0.9 3.
Reply
#2
It's kind of difficult to work with it without relating these variables to real life objects (or maybe I'm just noob:P) so it's not improved as much as it could be... But it generates the output you showed (including additional 45th row that yours' didn't have)

import numpy as np  
import math
import sys
import argparse
 
 
#print("Hallo world!")
#print("\n")
 
 
 
 
 
B = 0.30 # thickness
 
m = 3 # numbers of Layer
s = 5 # Steps for one Layer
Z = 0.30 # Z-value
z1= 0.30
z2= B*m
 
i=1 # variable for Lines
n=1 # Layer counter
g=2
S_ges = s * m  # lines in total
total_S_ges = S_ges


accumulated_i = 0 
whole_table = np.zeros([0, 3]) # 3 columns, 0 rows
 
while n<= m:
    
    #for g in range(1, m, 1):        
             
    I = np.linspace(i , total_S_ges , S_ges, dtype=int)
 
    table = np.zeros((S_ges, 3) )  # np.zeros(shape, dtype=float, order='C')

    table[:,0] = I
    table[:,1] = Z 
    table[:,2] = n

    whole_table = np.concatenate((whole_table, table), axis=0)

    #i +=1
 
    n += 1
    Z += B
    g += n

    # added lines
    i += S_ges
    total_S_ges += S_ges


print(whole_table)
 
fmt = ['%i'] + 2 * ['%.3f']
np.savetxt("Loop.txt", whole_table, fmt = fmt )
Output:
[[ 1. 0.3 1. ] [ 2. 0.3 1. ] [ 3. 0.3 1. ] [ 4. 0.3 1. ] [ 5. 0.3 1. ] [ 6. 0.3 1. ] [ 7. 0.3 1. ] [ 8. 0.3 1. ] [ 9. 0.3 1. ] [10. 0.3 1. ] [11. 0.3 1. ] [12. 0.3 1. ] [13. 0.3 1. ] [14. 0.3 1. ] [15. 0.3 1. ] [16. 0.6 2. ] [17. 0.6 2. ] [18. 0.6 2. ] [19. 0.6 2. ] [20. 0.6 2. ] [21. 0.6 2. ] [22. 0.6 2. ] [23. 0.6 2. ] [24. 0.6 2. ] [25. 0.6 2. ] [26. 0.6 2. ] [27. 0.6 2. ] [28. 0.6 2. ] [29. 0.6 2. ] [30. 0.6 2. ] [31. 0.9 3. ] [32. 0.9 3. ] [33. 0.9 3. ] [34. 0.9 3. ] [35. 0.9 3. ] [36. 0.9 3. ] [37. 0.9 3. ] [38. 0.9 3. ] [39. 0.9 3. ] [40. 0.9 3. ] [41. 0.9 3. ] [42. 0.9 3. ] [43. 0.9 3. ] [44. 0.9 3. ] [45. 0.9 3. ]]
Reply
#3
@michalmonday Thanks a Lot for the help! Actually it helped me a lot.
That what i was looking for.

With that i can go on with the table.
Reply
#4
Btw I made silly mistake and just modified the code by deleting this line:
i +=1

Otherwise the output didn't print number "16" and printed 2 number "17"s instead etc.

Nice that I could help:)
Reply
#5
For the first Columns it is already working very well.

so actually i have it like this now:

import numpy as np  
import math
import sys
import argparse
  
  
B = 0.30 # Layer thickness

# inputdata
m = 3 # numbers of Layer
s = 10 # Steps for one Layer

Z = 0.30 # Z-value
z1= 0.30
z2= B*m
  
i=1 # variable for Lines
n=1 # Layer counter
Typ = 0
L1_max = 2
L1_Step = L1_max/s

S_2 = s
 

whole_table = np.zeros([0, 5]) # 3 columns, 0 rows
  
while n<= m:
     
    #for g in range(1, m, 1):        
              
    I = np.linspace(i , S_2 , s, dtype=int)

   
   # L1 = np.linspace(0 , L1_max, s) #??
    #L1 = np.linspace(2 , -L1_max, s) #??
    L1 = np.linspace(-2 , +L1_max, s)
  

    table = np.zeros((s, 5) )  # np.zeros(shape, dtype=float, order='C')
 
    table[:,0] = I
    table[:,1] = Z 
    table[:,2] = L1
    table[:,3] = n
    table[:,4] = Typ
 	


    whole_table = np.concatenate( (whole_table, table), axis=0)
 
    
    n += 1
    Z += B
 
    # added lines
    i += s
    S_2 += s
 
 
print(whole_table)
  
fmt = ['%i'] +  ['%.1f'] + ['%.4f'] + ['%i'] * 2
np.savetxt("Loop.txt", whole_table, fmt = fmt )
Output:
Output:
[[ 1. 0.3 -2. 1. 0. ] [ 2. 0.3 -1.55555556 1. 0. ] [ 3. 0.3 -1.11111111 1. 0. ] [ 4. 0.3 -0.66666667 1. 0. ] [ 5. 0.3 -0.22222222 1. 0. ] [ 6. 0.3 0.22222222 1. 0. ] [ 7. 0.3 0.66666667 1. 0. ] [ 8. 0.3 1.11111111 1. 0. ] [ 9. 0.3 1.55555556 1. 0. ] [10. 0.3 2. 1. 0. ] [11. 0.6 -2. 2. 0. ] [12. 0.6 -1.55555556 2. 0. ] [13. 0.6 -1.11111111 2. 0. ] [14. 0.6 -0.66666667 2. 0. ] [15. 0.6 -0.22222222 2. 0. ] [16. 0.6 0.22222222 2. 0. ] [17. 0.6 0.66666667 2. 0. ] [18. 0.6 1.11111111 2. 0. ] [19. 0.6 1.55555556 2. 0. ] [20. 0.6 2. 2. 0. ] [21. 0.9 -2. 3. 0. ] [22. 0.9 -1.55555556 3. 0. ] [23. 0.9 -1.11111111 3. 0. ] [24. 0.9 -0.66666667 3. 0. ] [25. 0.9 -0.22222222 3. 0. ] [26. 0.9 0.22222222 3. 0. ] [27. 0.9 0.66666667 3. 0. ] [28. 0.9 1.11111111 3. 0. ] [29. 0.9 1.55555556 3. 0. ] [30. 0.9 2. 3. 0. ]]
But the third column seems to be a little bit more difficult.
Do you have an Idea here?
I would like to have the possibility to count in this column from o tu L_max and than to -L_max.
So should look like this:

Output:
1. 0.3 0.000 1. 0. 2. 0.3 0.222 1. 0. 3. 0.3 0.444 1. 0. 4. 0.3 0.667 1. 0. 5. 0.3 0.889 1. 0. 6. 0.3 1.111 1. 0. 7. 0.3 1.333 1. 0. 8. 0.3 1.556 1. 0. 9. 0.3 1.778 1. 0. 10. 0.3 2.000 1. 0. 11. 0.6 2.000 2. 0. 12. 0.6 1.778 2. 0. 13. 0.6 1.556 2. 0. 14. 0.6 1.333 2. 0. 15. 0.6 1.111 2. 0. 16. 0.6 0.889 2. 0. 17. 0.6 0.667 2. 0. 18. 0.6 0.444 2. 0. 19. 0.6 0.222 2. 0. 20. 0.6 0.000 2. 0. 21. 0.9 0.000 3. 0. 22. 0.9 -0.222 3. 0. 23. 0.9 -0.444 3. 0. 24. 0.9 -0.667 3. 0. 25. 0.9 -0.889 3. 0. 26. 0.9 -1.111 3. 0. 27. 0.9 -1.333 3. 0. 28. 0.9 -1.556 3. 0. 29. 0.9 -1.778 3. 0. 30. 0.9 -2.000 3. 0.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 300 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  output shape problem with np.arange alan6690 5 610 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  problem in output of a function akbarza 9 1,094 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 882 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Facing problem with Pycharm - Not getting the expected output amortal03 1 818 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  Try to solve GTG multiplication table problem. Frankduc 6 1,935 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,289 Feb-05-2021, 04:37 PM
Last Post: nilamo
  single input infinite output problem Chase91 2 1,906 Sep-23-2020, 10:01 PM
Last Post: Chase91
  sports Stats > table output loop problems paulfearn100 3 2,444 Jul-22-2020, 03:21 AM
Last Post: c_rutherford
  Save output into a Excel Sheet with Format Table skaailet 1 2,455 Apr-17-2020, 11:56 PM
Last Post: thirteendec

Forum Jump:

User Panel Messages

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