0

Python newbie here. I am trying to operate on lists which contain floating point numbers. avg is a list parameter that is returned from a different method. However, when I tried doing the following, it throws me an error that float() should have a string or a number and not a list. avg1 should contain a copy of the lists with float-type numbers instead of lists right? I tried a few edits I read on other posts with similar titles, but couldn't solve this. Just starting out so kindly tell me where I am going wrong.

def movingavg(EMA,avg):     
    EMA=[]
    avg1 = [float(i) for i in avg]
    EMA[:3] = avg1[:3]
    for i,j in zip(EMA[2:],avg1[3:]):
        a =float(i)*0.67 + float(j)*0.33
        EMA.append(a)
    return EMA

The error that I get is as follows :

avg1 = [float(i) for i in avg]
TypeError: float() argument must be a string or a number, not 'list'

Using Python 3.4

3
  • 8
    Please post an example of what avg looks like. Seems as if it is a list of lists or sth alike. Commented Sep 7, 2016 at 11:20
  • @jbndlr it is a list with numbers taken from a column in a pandas dataframe. [[12312188.0], [6055962.0], [2753511.0], [3216789.0], [3554074.0], [4638837.0], [7064825.0], [4162710.0], [3262018.0], [3913376.0], [3945705.0]] I have included a few values out of the many. Commented Sep 7, 2016 at 11:33
  • 3
    See, you got a list of lists there. Change your float-cast to float(i[0]) if it's always in this shape. Commented Sep 7, 2016 at 11:35

3 Answers 3

2

Instead of avg1 = [float(i) for i in avg] use below code.

avg1 = []
for i in avg:
    for j in i:
        avg1.append(float(j))

or can use below list comprehension.

avg1 = [float(i)  for val in avg for i in val]
Sign up to request clarification or add additional context in comments.

11 Comments

Using a list comprehension would be a lot more concise for this.
I am writing it. Just posted answer. Will edit in minute. :)
The result of the list comprehension will produce a single list rather than keeping the data structure of the OP.
Yups. I think the code which is executed after this line of code, requires single list and not list of list. That's why I created single list.
Initially I have written comprehension similar like yours. But then checked the code and changed it.
|
0

Please check return type of avg I think it's return type is list's of list.

2 Comments

I did, and you're right, it's a list of lists. But how do I cast it to float for use?
@AmanJaiswal that isn't actually using the map function though, that's just an embedded list comprehension.
0

To convert a list of lists to float, you need to use two list comprehensions, like this:

avg1 = [[float(i) for i in val] for val in avg]

1 Comment

This will result again in list of list.

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.