TypeError:'float'对象不能与浮点列表一起迭代

时间:2018-01-18 04:04:55

标签: python numpy for-loop

我有一个浮点数in1列表,我想做两件事:1)将1024个浮点项目列表转换为dB值,2)将该列表与阈值进行比较。我尝试在下面显示的代码中执行此操作。

console.log(this.props.navigation.state.params.movieid);

我假设“c”包含使用log10()方法转换为某个值的1024个项目的列表。我想将此列表与阈值进行比较。我已经尝试了c.all()和c.any()并且我收到了属性错误,所以我尝试使用“for i in c”上面的方法并收到以下错误

        in1=islice(in0, 1024)    #1024 values are taken from the variable in0 and stored in in1
    #Each item in the list of 1024 elements in in1 is converted to dB and then compared to a threshold 
    for i in in1:
            c = math.log10(i/1024)  #converts 1024 values stored in in1 to dB and stores them in c      
            for i in c:
         if i > 70:    #checks if any of the values stored in c are greater than a threshold value            
              print "A signal is present"
         else:
              print "No signal is present"        
        self.seen = 0
    self.consume_each(in0.shape[0]) #consume everything you've account for
                                # tell system to move on to next samples
    return 0        ##return 0 samples generated
                    ## as the block doesn't have an output stream

我想知道“c”中包含的任何值是否大于阈值。这样做有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

当您使用

循环in1(我假设的浮动列表)时
    for i in in1:

每个元素i都是一个浮点元素 所以,c也是一个浮点数(c = math.log10(i / 1024))。 这就是为什么你不能使用:

迭代浮点数
for i in c:

可以解决您的问题的是:

    #Each item in the list of 1024 elements in in1 is converted to dB and then compared to a threshold 
for i in map(lambda x: math.log10(float(x)/1024), in1):
     if i > 70:    #checks if any of the values stored in c are greater than a threshold value            
          print "A signal is present"
     else:
          print "No signal is present"        
    self.seen = 0
self.consume_each(in0.shape[0]) #consume everything you've account for
                            # tell system to move on to next samples
return 0        ##return 0 samples generated
                ## as the block doesn't have an output stream

表达式map(lambda x:math.log10(float(x)/ 1024),in1)将值转换为db。然后我们迭代已转换的值。

编辑:
您可以使用以下代码一次性完成所有操作:

def check_signal(value):
    if value > 70:
        print "A signal is present"
    else:
        print "No signal present"
    self.seen = 0
in1_db = map(lambda x: math.log10(float(x)/1024), in1)
map(check_signal, in1_db)
self.consume_each(in0.shape[0]) #consume everything you've account for
                        # tell system to move on to next samples
return 0        ##return 0 samples generated