繁体   English   中英

列表中的值的嵌套循环Python计算

[英]Nested Loop Python calculation of values in a list

我想在嵌套循环中计算以下函数, 并保持这种方式

Calc_Value = value_a(2D) - (values_b(0D) + values_b(1D))/10000

结果为:

1.1274875

定义为:

value_a(2D) corresponds to type **a**, year **2D** and value **1.1275**
value_b(0D) corresponds to type **b**, year **0D** and value **0**
value_b(1D) corresponds to type **b**, year **1D** and value **0.125**

我不知道如何定义下面的函数,以将值b正确地用于两个值,如上所示???

有一种方法可以直接在pandas( Pandas Version )中完成,但是想使用我的嵌套方法。

更新:

我提高了功能并收到了适当的值,但仍然有问题,我要计算几次而不是一次。 我得到每个值b的值a,直到计算出正确的值(最后一个值)为止。 我只想保留最后的值。

 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1275 1.1274875 --> **Only this value should be shown** 

该代码如下所示:

import pandas as pd

def CalcValue(year, a, b):  
    normalization = 0.0
    value_0D = 0.0
    value_1D = 0.0
    Calc = 0.0
    normalization = 10000.0
    if year == "0D":    
        value_0D = b
    elif year == "1D":
        value_1D = b
    Calc = a-(value_0D+value_1D)/normalization            
    return Calc


data = pd.read_csv('C:/Users/mcm/Desktop/Book1.csv').fillna('')

pd_date = pd.DatetimeIndex(data['date'].values)
data['date'] = pd_date
index_data = data.set_index('date')

for current_date in index_data.index.unique():    
    print('calculating date: ' + str(current_date))

    for index, row in index_data.iterrows():
        if index == current_date:
            for index2, row2 in index_data.iterrows(): 
                if index2 == current_date:
                    if row['type'] in {'a', 'b'} and row2['type'] in {'a', 'b'}:
                        if row['type'] != row2['type'] and row['type'] != 'a' and row2['type'] != 'b':  
                            test = CalcValue(row['year'], row2['value'],row['value'])
                            print(test)

数据如下所示:

date    type    year    value
2015-02-09  a   2D  1.1275
2015-02-09  b   10M 58.125
2015-02-09  b   11M 68.375
2015-02-09  b   1M  3.345
2015-02-09  b   1W  0.89
2015-02-09  b   1Y  79.375
2015-02-09  b   2M  7.535
2015-02-09  b   2W  1.8
2015-02-09  b   3M  11.61
2015-02-09  b   3W  2.48
2015-02-09  b   4M  16.2
2015-02-09  b   5M  21.65
2015-02-09  b   6M  27.1
2015-02-09  b   7M  33.625
2015-02-09  b   8M  41.375
2015-02-09  b   9M  49.5
2015-02-09  b   0D  0
2015-02-09  b   1D  0.125
2015-02-09  c   2Y  -28.5
2015-02-09  c   3Y  -28.75
2015-02-09  c   4Y  -28
2015-02-09  c   5Y  -27.5
2015-02-09  c   6Y  -27
2015-02-09  c   7Y  -26.75
2015-02-09  c   8Y  -26.25
2015-02-09  c   9Y  -25.5
2015-02-09  c   10Y -25
2015-02-10  a   2D  1.1297
2015-02-10  b   10M 60.5
2015-02-10  b   11M 70.375
2015-02-10  b   1M  3.32
2015-02-10  b   1W  0.84
2015-02-10  b   1Y  81.625
2015-02-10  b   2M  7.54
2015-02-10  b   2W  1.74
2015-02-10  b   3M  11.745
2015-02-10  b   3W  2.45
2015-02-10  b   4M  16.4
2015-02-10  b   5M  22.05
2015-02-10  b   6M  28.1
2015-02-10  b   7M  35.375
2015-02-10  b   8M  42.625
2015-02-10  b   9M  51
2015-02-10  b   0D  0.105
2015-02-10  b   1D  0.11
2015-02-10  c   2Y  -29.5
2015-02-10  c   3Y  -29.75
2015-02-10  c   4Y  -29.5
2015-02-10  c   5Y  -29
2015-02-10  c   6Y  -28.5
2015-02-10  c   7Y  -28
2015-02-10  c   8Y  -27.5
2015-02-10  c   9Y  -26.75
2015-02-10  c   10Y -26.25

这将是最小的:

for i in range(20):
    print(i)
#print(i)

这包括一个条件:

for i in range(20):
    if True:
        test = i
        print(test)
#print(test)

您的问题,仅仅是从一个循环得到最后的计算值 -你已经实现了这一点,但您要打印的循环内的每个计算值,即在每一个地方的条件得到满足迭代。 快速的解决方案是仅在循环之后进行打印(交换上面print()上的注释)。

一旦找到所需内容,通常就可以停止循环。 否则,您将使用另一个变量来存储结果:

keepitthatway = None
for i in range(42):
    if i==7:
        keepitthatway = i
        # keep on iterating if necessary or
        #break
print(i)
print(keepitthatway)

那里有很多Python编程教程,例如https://wiki.python.org/moin/ForLoop 看一看 ;-)


对于最小,完整和可验证的示例 ,这也将起作用。 虽然不是完全最小,但它类似于您的代码结构:

for current_date in range(2):
    print('calculating something...')
    for index, row in enumerate(range(20)):
        if index == current_date:
            for index2, row2 in enumerate(range(20)):
                if True:
                    if True:
                        if True:
                            test = index
                            print(test)
    #print(test)

仅限制数据集已经是一个改进:

import pandas as pd

# pretend to be the csv file
import io
csv = io.StringIO("""date\ttype\tyear\tvalue
2015-02-09\ta\t2D\t1.1275
2015-02-09\tb\t10M\t58.125
2015-02-09\tc\t2Y\t-28.5
2015-02-10\ta\t2D\t1.1297
2015-02-10\tb\t10M\t60.5
2015-02-10\tc\t2Y\t-29.5
2015-02-10\tc\t10Y\t-26.25
""")

def CalcValue(year, a, b):
    normalization = 0.0
    value_0D = 0.0
    value_1D = 0.0
    Calc = 0.0
    normalization = 10000.0
    if year == "0D":
        value_0D = b
    elif year == "1D":
        value_1D = b
    Calc = a-(value_0D+value_1D)/normalization
    return Calc

data = pd.read_csv(csv, sep="\t").fillna('')

#print(data)
#import sys;sys.exit()

pd_date = pd.DatetimeIndex(data['date'].values)
data['date'] = pd_date
index_data = data.set_index('date')

for current_date in index_data.index.unique():
    print('calculating date: ' + str(current_date))

    for index, row in index_data.iterrows():
        if index == current_date:
            for index2, row2 in index_data.iterrows():
                if index2 == current_date:
                    if row['type'] in {'a', 'b'} and row2['type'] in {'a', 'b'}:
                        if row['type'] != row2['type'] and row['type'] != 'a' and row2['type'] != 'b':
                            test = CalcValue(row['year'], row2['value'],row['value'])
    print(test)

版画

calculating date: 2015-02-09 00:00:00
1.1275
calculating date: 2015-02-10 00:00:00
1.1297

(当然,这些值与问题中的值不同,因为数据集已修改)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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