0

With the code below, it prints the value 'phase' one by one. I am trying to print these values as an array outside of for loop.

import math

Period = 6.2

time1 = datafile1[:,0]
magnitude1 = datafile1[:,1]
for i in range(len(time1)):
   print(i,time1[i])
   floor = math.floor((time1[i]-time1[0])/Period)
   phase = ((time1[i]-time1[0])/Period)-floor 
   print (phase)

It is printing like this:

0.002
0.003
0.004
0.005

I would like it to print like this:

[0.002, 0.003, 0.004, 0.005]
4
  • you could append them to a list and print the list Commented Sep 26, 2018 at 3:30
  • Please post your datafile1 structure so answerers can offer working and complete code. Commented Sep 26, 2018 at 3:31
  • Add your phases to a list and do something like: print('[' + ', '.join(phases) + ']') Commented Sep 26, 2018 at 3:32
  • just take an empty list and append the elements one by one in it by the same for loop . Commented Sep 26, 2018 at 3:36

3 Answers 3

2

This would be the least modification requirement path to that result

result = []

time1 = datafile1[:,0]
magnitude1 = datafile1[:,1]
for i in range(len(time1)):
   result.append(i,time1[i])
   floor = math.floor((time1[i]-time1[0])/Period)
   phase = ((time1[i]-time1[0])/Period)-floor 
   result.append(phase)

print(result)
Sign up to request clarification or add additional context in comments.

Comments

0

Here I've made it so instead of printing your results, you append them to a list, then print out the full list.

import math

Period = 6.2

time1 = datafile1[:,0]
magnitude1 = datafile1[:,1]

my_list = []
for i in range(len(time1)):
   my_list.append(i,time1[i])
   floor = math.floor((time1[i]-time1[0])/Period)
   phase = ((time1[i]-time1[0])/Period)-floor 
   my_list.append(phase)

print(my_list)

Comments

0

You can do

import math

Period = 6.2

time1 = datafile1[:,0]
magnitude1 = datafile1[:,1]
list_to_print = []
for i in range(len(time1)):
   print(i,time1[i])
   floor = math.floor((time1[i]-time1[0])/Period)
   phase = ((time1[i]-time1[0])/Period)-floor
   list_to_print.append(phase)
print (list_to_print)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.