【问题标题】:Want to read sequential lines in a file and do some mathematical calculation [closed]想要读取文件中的连续行并进行一些数学计算[关闭]
【发布时间】:2013-09-03 08:25:33
【问题描述】:

我有一个文件(Data.txt),其中包含像(两列数据)这样的数据

0.105785959943        9.75133617601e+15
0.111906693211        9.03309900398e+15
0.118381569654        9.10020956844e+15
0.125231079854        9.92284743442e+15
0.132476899971        8.90313525209e+15
0.140141960337        8.94055824107e+15
0.148250518026        9.26206609674e+15
0.156828233614        8.91802025262e+15

该文件可能包含 100 行。让我将第一列中的值称为r_i,将第二列中的值称为d_ii 可能从0 更改为100)。我的问题是编写代码来计算 C*(r_(i+1)-r_i)^3 * d_i,其中C 是一个常数。

我还想将这些数据写入一个包含 3 列的新文件中,其中第三列应该是我们新计算的数据。

我怎样才能做到这一点?有没有人知道如何解决这个问题?

【问题讨论】:

  • 您使用哪种语言,C++python?你已经尝试过什么了吗?您当前的代码有什么具体问题?
  • @RS John:正如 BoBTFish 所说,您需要指定代码的实际问题
  • 如果C是常量,你的意思是C是一个包含常量的数组吗?
  • 没有 C 只是 2/3 pi
  • @SathishKrishnan:我相信他在使用数学意义上的[x]——“x 被截断为整数”。

标签: c++ python bash file-io


【解决方案1】:

要阅读 Python 中的文件,请查看 here。要将输出写入文件,请查看here

【讨论】:

    【解决方案2】:
    #include <iostream>
    
    int main(int, char*[]) {
      const double C = 1;
      double rp, dp;
      double r, d;
    
      std::cin >> rp >> dp;
      while (std::cin.good()) {
        std::cin >> r >> d;
        if (std::cin.good()) {
          double t = r - rp;
          double v = C * t * t * t * dp;
          std::cout << rp << " " << dp << " " << v << std::endl;
          rp = r; dp = d;
        }
      }
      std::cout << rp << " " << dp << " " << 0 << std::endl;
    
      return 0;
    }
    

    【讨论】:

      【解决方案3】:

      awk 非常适合这种类型的计算:

      awk -v c="$c" '{if (s) print o, c*($1-r)^3*d;o=$0;r=$1;d=$2;s=1}' file
      

      其中 c 是一个 shell 变量

      【讨论】:

        【解决方案4】:

        尽管有 5 个人对我的问题投了反对票,但我还是找到了解决问题的方法。 这是我使用的代码。它是用 Python 编写的。

        enter codefrom numpy import *
        from math import *
        import operator
        
        f = open('Data_Genergy_26.txt', 'r')
        lines = f.readlines()
        # initialize some variable to be lists:
        r = []
        ro = []
        E =[]
        
        # scan the rows of the file stored in lines, and put the values into some variables:
        for line in lines:
            p = line.split()
            r.append(float(p[0]))
            ro.append(float(p[1]))
        #Subtracting the current and previous item in a list
        def foo(it):
            it = iter(it)
            t = it.next()
            for s in it:
                yield t, s
                t = s        
        list(foo(r))
        E=[x[1] - x[0] for x in foo(r)]
        #print E
        #Cubing all elements in a list
        def cube(E):
            return [i ** 3 for i in E]
        #print cube(E)
        #computing total energy
        z =[a*b for a,b in zip(cube(E),ro)]
        z[:] = [x*4/3*pi for x in z] 
        z.append(0.0) #for making r, ro, and z of same dimension
        #print z     
        
        DataOut = column_stack((r,ro,z))
        savetxt('output.dat', DataOut)
        

        如果有任何更好的方法来实现这一点,请告诉我。 谢谢

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多