迭代列表列表... TypeError:'float'类型的参数不可迭代

时间:2017-09-06 10:13:07

标签: python loops

我无法理解为什么我要回复此错误!!

        for i in range(len(AmDeliveryPricelist)):
        if AmDeliveryPricelist[i] == 'nan': continue
        for j in range(len(AmDeliveryPricelist[i])):
            AmPricelist[i][j] = re.findall(r'(?:\d+\.)?\d+.\d+', AmPricelist[i][j])[0]
            if 'FREE' in AmDeliveryPricelist[i][j]:
                AmDeliveryPricelist[i][j] = 0.
            else:
                AmDeliveryPricelist[i][j] = re.findall(r'(?:\d+\.)?\d+.\d+', AmDeliveryPricelist[i][j])[0]

这就是错误:

Traceback (most recent call last):

  File "<ipython-input-38-3098773b6c36>", line 6, in <module>
    if 'FREE' in AmDeliveryPricelist[i][j]:

TypeError: argument of type 'float' is not iterable

代码贯穿列表列表..这是一个例子:

AmPricelist[:5]
Out[39]: 
[['5.00',
  '4.50',
  '4.85',
  '5.47',
  '5.65',
  '6.30',
  '6.30',
  '6.63',
  '6.99',
  '7.14',
  '7.15',
  '7.21',
  '8.31',
  '8.40',
  '8.41',
  '8.45',
  '8.57',
  '8.78',
  '8.79',
  '9.05',
  '9.07',
  '9.10',
  '9.11',
  '9.21',
  '9.12',
  '9.56',
  '9.63',
  '11.13',
  '9.90',
  '10.00',
  '10.33',
  '11.17',
  '11.87',
  '12.01',
  '12.98',
  '11.79',
  '13.72',
  '15.98',
  '16.60'],
 ['1.47',
  '0.71',
  '1.00',
  '2.03',
  '3.49',
  '2.45',
  '2.48',
  '3.99',
  '2.99',
  '2.99',
  '2.99',
  '3.25',
  '3.49',
  '5.00',
  '3.97',
  '3.99',
  '3.99',
  '3.99',
  '4.50',
  '4.71',
  '5.98',
  '5.99',
  '4.98',
  '4.99',
  '5.03',
  '6.40',
  '5.20',
  '5.60',
  '6.92',
  '5.79',
  '5.81',
  '5.97',
  '5.99',
  '5.99',
  '6.60',
  '7.43',
  '7.43',
  '7.50',
  '7.51',
  '7.61',
  '8.99',
  '8.50',
  '9.00',
  '9.78',
  '11.52',
  '11.80',
  '12.15',
  '12.40'],
 ['3.05',
  '3.06',
  '7.19',
  '7.27',
  '8.24',
  '8.27',
  '8.29',
  '8.67',
  '8.68',
  '8.70',
  '8.93',
  '9.03',
  '9.05',
  '9.09',
  '9.09',
  '9.24',
  '9.25',
  '9.36',
  '9.40',
  '9.60',
  '11.13',
  '9.90',
  '10.00',
  '10.13',
  '11.90',
  '10.73',
  '10.74',
  '11.20',
  '11.67',
  '11.87',
  '12.55',
  '12.75',
  '11.79',
  '14.20',
  '15.21',
  '15.95',
  '16.68',
  '18.33',
  '25.37'],
 ['5.00',
  '6.00',
  '6.04',
  '6.05',
  '6.17',
  '6.38',
  '6.70',
  '6.88',
  '6.90',
  '6.93',
  '7.07',
  '7.08',
  '7.27',
  '7.30',
  '7.45',
  '8.97',
  '7.88',
  '9.34',
  '8.17',
  '8.75',
  '8.82',
  '11.67',
  '18.05',
  '20.60'],
 ['4.37',
  '4.38',
  '5.99',
  '5.99',
  '6.19',
  '6.22',
  '6.24',
  '6.29',
  '7.56',
  '6.44',
  '6.45',
  '6.48',
  '6.64',
  '6.66',
  '7.01',
  '7.03',
  '7.54',
  '8.48',
  '9.07',
  '9.84',
  '9.85',
  '9.91',
  '8.85',
  '10.63',
  '13.53',
  '17.25',
  '31.44']]

AmDeliveryPricelist[:5]
Out[41]: 
[[0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.49',
  '1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '2.80',
  '1.26',
  '1.26',
  '1.26'],
 ['1.26',
  '2.03',
  '2.03',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  0.0,
  0.0,
  '1.26',
  '1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26'],
 ['1.26',
  '1.26',
  0.0,
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.49',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '2.80',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26'],
 ['1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  0.0,
  '1.26',
  0.0,
  '1.49',
  '1.26',
  '2.80',
  '1.26',
  '1.26',
  '1.26'],
 ['1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  0.0,
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.26',
  '1.49',
  '1.26',
  '1.26',
  '1.26',
  '2.80',
  '1.26',
  '1.26',
  '1.26',
  '1.26']]

我无法理解为什么此代码因此错误而失败。据我所知,我要求它迭代范围,这是可迭代的......

我在那里添加了一个打印(i,j),并且在打印完(0,0)之后打破了代码。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

由于你问为什么你的代码不起作用,这就是答案:

在此if语句中,您试图查看某个string是否属于float

if 'FREE' in AmDeliveryPricelist[i][j]:
    AmDeliveryPricelist[i][j] = 0.

我不认为这是你想要的行为,所以你应该考虑解决这个问题!希望它有所帮助!