0

我有 3 个列表

r = [0.105, 0.111, 0.218, 0.225, 0.332, 0.440, 0.548, 0.656, 0.865, 1.75]
ro = [850, 856, 844, 830, 787, 826, 764, 773, 781, 748]
new_r = [0.25672, 0.39602, 0.55907, 0.65756, 0.77467, 0.93619]

r和的长度ro始终相同,但len(new_r)始终小于r和的长度ro。我要计算

(r[i+1] -r[i])**3*ro[i+1]

r[i]对于小于第一个元素的所有s 并将其new_r[]打印(将其写入文件)。r[i]然后对所有小于第二个元素的 s执行相同的操作new_r[]并将其打印(将其写入文件)。依此类推,直到最后一个值new_r

我该如何进行这种比较和计算?

4

2 回答 2

0

您可以使用 [something for i in a_range if blabla] 语法

for newr in new_r:
  print [(r[i+1]-r[i])**3 * ro[i+1] for i in xrange(len(r) - 1) if r[i] < newr]
于 2013-09-03T11:52:15.623 回答
0

你可以这样做

from __future__ import print_function # only for compatibility with the 2 and 3 version of python
r = [0.105, 0.111, 0.218, 0.225, 0.332, 0.440, 0.548, 0.656, 0.865, 1.75]
ro = [850, 856, 844, 830, 787, 826, 764, 773, 781, 748]
new_r = [0.25672, 0.39602, 0.55907, 0.65756, 0.77467, 0.93619]

for el in new_r:
    print("for ",el)
    for i in range(len(r)-1):
        val =  (r[i+1] -r[i])**3*ro[i+1]
        if val < el:
            print(val)
于 2013-09-03T11:54:05.197 回答