0

Here is what i am trying to do:

if len(targets) == 0:
   avg = frozenset([tuple(.123/(nodes_num - 1))])
   for t in range(nodes_num):
       if t == node:
          continue
       node_p = (avg,t)
       node_weig.append(node_p)

I don't know what i am doing wrong but all it says avg = fro... float object is not iterable

2 Answers 2

1

This is your problem: tuple(.123/(nodes_num - 1))

The tuple constructor takes an iterable and makes a tuple from the values it produces. Obviously, .123/(nodes_num - 1) is a float, which can't be iterated. If you want to make a one element tuple of this value, use: (.123/(nodes_num - 1),) (note: That trailing comma is necessary to make a one element tuple, otherwise the parens are just grouping the operation, not wrapping it in a tuple).

So the resulting line would be:

avg = frozenset([(.123/(nodes_num - 1),)])

That will make a len 1 frozenset containing a len 1 tuple with the calculated float value. If the goal is just a frozenset of that one value, you don't need the tuple wrapping at all, and can simplify to:

avg = frozenset([.123/(nodes_num - 1)])

Now, whether either does anything meaningful for your scenario I don't know, but it's the source of your error.

Sign up to request clarification or add additional context in comments.

Comments

0

From the doc:

tuple([iterable])

Return a tuple whose items are the same and in the same order as iterable‘s items. iterable may be a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged.

.123/(nodes_num - 1) is a float,that's why you got the error:

TypeError: 'float' object is not iterable

If I don't misunderstand your meaning,you can try to use

tuple([.123 / (nodes_num - 1)]) to make it iterable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.