简体   繁体   中英

Substituting the arrays to the function in python

I want to substitute number (from first column of my long data file) to an equation with iteration and make the all calculated result in list. Let's say I want to use y=2*x+1 as an equation and x is one of the numbers in the first column of my data file. My main problem is x is not same as the row number. Since I have separate data files, each row of first column of my data has a different unrelated number.

My attempt so far:

for n in data[n,0]:
    result = []
    if n<100:
        y = 2*x + 1
        result.append(y)
        print result

If you had the following data file:

1 abc
2 abc
3 abc
4 abc
15 abc

The below script would parse the file for the number in the first column, apply your function, then append the number to a list (named results). The code will need to be adapted depending on the format of your data file.

results = []
with open('data.txt', 'r') as f:
    for line in f:
        results.append(2*float(line.split(' ')[0]) + 1)

print results
[3.0, 5.0, 7.0, 9.0, 31.0]

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