简体   繁体   中英

How to create a list from a for-loop

I am trying to create a list of numbers from a function and a for-loop. Here is a copy of my code:

Rh = 1096776000000

print "What is your value for 'n'?"
n = float(raw_input(">"))

m = range(int(n+1), int(n+21))

def wavelength(a,b):
    list = [((1 / (Rh * ((1 / (a**2)) - (1 / (float(x)**2))))) * 10 ** 14)
    for x in b]
    return list


for elements in wavelength(n,m):
    print "%.3f" % elements, 'nm'

This will print out what I want, but I need to take all of the data points and put them into a list. Any ideas?

I guess you're in need of something like this:

l = [] # An empty list
for elements in wavelength(n,m):
    l.append("%.3f" % elements, 'nm') # Adding rounded values to the list
print l # Print the full list 
results = ["%.3f nm" % elements for elements in wavelength(n,m)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM