0

I tried to do calculation in python using values accessed for any pair of two rows when iterating through a tuple list but I received the type error as below. Does anyone have idea how to solve this? Thanks!

The code is shown below:

for ws1 in ccy_results:
            for ws2 in ccy_results:
                variance = ws1.WS * ws2.WS
                K += variance

Error message:

    K += variance
TypeError: 'float' object is not iterable

An extract of ccy_results is shown below:

[ws(Qualifier='AUD', Curve='Libor3m', Tenor='1m', WS=214000000.0), 
ws(Qualifier='AUD', Curve='Libor3m', Tenor='2y', WS=-106000000.0), 
ws(Qualifier='AUD', Curve='Libor3m', Tenor='6m', WS=213000000.0),
ws(Qualifier='CHF', Curve='Libor6m', Tenor='15y', WS=-200000000.0), 
ws(Qualifier='CHF', Curve='Libor6m', Tenor='20y', WS=540000000.0),
ws(Qualifier='CHF', Curve='Libor6m', Tenor='30y', WS=756000000.0)]

I tried to do checking by printing ws1.WS using the below code but all WS in ccy_results appear. I think this is the reason. How can I solve this issue?

for ws1 in ccy_results:
            for ws2 in ccy_results:
                print(ws1.WS)

import collections
ws = collections.namedtuple('ws',('Qualifier', 'Curve', 'Tenor', 'WS'))
ccy_results = [ws(Qualifier='AUD', Curve='Libor3m', Tenor='1m', WS=214000000.0), 
ws(Qualifier='AUD', Curve='Libor3m', Tenor='2y', WS=-106000000.0), 
ws(Qualifier='AUD', Curve='Libor3m', Tenor='6m', WS=213000000.0),
ws(Qualifier='CHF', Curve='Libor6m', Tenor='15y', WS=-200000000.0), 
ws(Qualifier='CHF', Curve='Libor6m', Tenor='20y', WS=540000000.0),
ws(Qualifier='CHF', Curve='Libor6m', Tenor='30y', WS=756000000.0)]
K = []
for ws1 in ccy_results:
    for ws2 in ccy_results:
        variance = ws1.WS * ws2.WS
        K += variance
wwii
  • 23,232
  • 7
  • 37
  • 77
  • what's your desired output...you are looping twice and doing?? – Ajay Feb 21 '21 at 14:57
  • Are those namedtuples and is `ccy_results` the list in your question? What is `K`? We should be able to copy and paste directly from your [mre] and be able to reproduce the problem. We should have to guess what things are or how they were *made* - that could be important to solving the problem. – wwii Feb 21 '21 at 15:13
  • @Ajay I want to get the product of WSs for any pair of 2 rows when iterating through the namedtuples ccy_results – user15253500 Feb 21 '21 at 16:06
  • 1
    @wwii Yes, ```ccy_results``` are namedtuples. I would like to iterate through ccy_results in this function. ```K ``` is empty list that ```K=[]``` and to store the result obtained through iterating namedtuples ```ccy_results``` – user15253500 Feb 21 '21 at 16:13
  • 1
    I edited your question - if that edit is not an accurate [mre] please [edit] it. – wwii Feb 21 '21 at 16:20
  • @wwii Thanks a lot for editing! Is there any way that I can access the `WS` of single row when I iterate through the namedtuples `ccy_results`? I am trying to do so using `ws1.WS` and `ws2.WS` but they are calling the full list of the namedtuples, which is not what I want. – user15253500 Feb 22 '21 at 10:14
  • @user15253500 - You specified `for any pair of two rows` - your nested loops give you the *cartesian product* of the list with itself maybe you are looking for combinations instead? It isn't really clear what you are trying to accomplish. You should probably ask a new question. Maybe you can make use of one of the combinatoric iterators in the [itertools module](https://docs.python.org/3/library/itertools.html). – wwii Feb 22 '21 at 15:38

1 Answers1

0

The augmented assignment operation, +=, for a list is equivalent to list.extend which expects an iterable for its argument ( the left-hand-side of the expression ).

Change

K += variance

to

K.append(variance)

From Mutable Sequence Types

Operation Result
s.extend(t) or s += t extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t)
wwii
  • 23,232
  • 7
  • 37
  • 77