【问题标题】:Converting Comprehension List to Numpy Array - Glitch?将理解列表转换为 Numpy 数组 - 故障?
【发布时间】:2016-03-23 20:13:06
【问题描述】:

我无法将理解列表转换为 numpy 数组。我正在遍历不同的理解列表;不过,有些似乎还不错。我通过打印他们的shape 验证了这个错误,并且一些迭代没有返回正确的尺寸。因此,我无法将这些 numpy 数组(成分)与另一组数组(basic_info)连接起来。此外,我为这些迭代打印了 numpy 数组本身,并注意到 '[' 有一个额外的尾随空格。任何帮助将不胜感激!

请看下面:

(1) 这就是我创建列表理解的方式

html = browser.page_source
            soup = BeautifulSoup(html)
            table = soup.find('div', {'id': 'placeBody_dynField77_divScroll'})
            table_body = table.find('tbody')
            rows = table_body.findAll('tr')[1:]
            Ingredients = []
            for row in rows:
                cols = row.find_all('td')
                cols = [ele.text.strip() for ele in cols]
                Ingredients.append([ele for ele in cols if ele])
            Ingredients = np.array(Ingredients)

(2) 打印shape时,不返回列数(应该是8)

print(Ingredients)
print(Ingredients.shape, basic_info2.shape)

>>

[ ['Distillates (Petroleum), Hydrotreated Heavy Naphthenic', '64742-52-5', 'n/a', '40.00 %', '50.00 %', '45.00 %', '40-<50%', '0.00 %']
 ['2-(2-butoxyéthoxy) Éthanol', '112-34-5', 'n/a', '10.00 %', '20.00 %', '15.00 %', '10-<20%', '0.00 %']
 ['Low Odor Base Solvent', '64742-47-8', 'n/a', '10.00 %', '20.00 %', '15.00 %', '10-<20%', '0.00 %']
 ['Other Components Below Reportable Levels', 'n/a', '5.00 %', '10.00 %', '7.50 %', '5-<10%', '0.00 %']
 ['Naphtha (Petroleum), Hydrotreated, Heavy', '64742-48-9', 'n/a', '5.00 %', '10.00 %', '7.50 %', '5-<10%', '0.00 %']
 ['Solvent Naphtha (Petroleum), Medium Aliph.', '64742-88-7', 'n/a', '5.00 %', '10.00 %', '7.50 %', '5-<10%', '0.00 %']
 ['Stoddard Solvent', '8052-41-3', 'n/a', '5.00 %', '10.00 %', '7.50 %', '5-<10%', '0.00 %']
 ['Carbon Dioxide', '124-38-9', 'n/a', '1.00 %', '3.00 %', '2.00 %', '1-<3%', '0.00 %']
 ['Nonane', '111-84-2', 'Less Than (Max)', '0.00 %', '1.00 %', '0.50 %', '<1%', '0.00 %']
 ['Naphthalene', '91-20-3', 'Less Than (Max)', '0.00 %', '1.00 %', '0.50 %', '<1%', '0.00 %']]
[(10,), (10, 4)]
[ ['Gasoline, Low Boiling Point Naphtha', '86290-81-5', 'n/a', '90.00 %', '100.00 %', '95.00 %', '90.00-100.00%', '0.00 %']
 ['EthylBenzene', '100-41-4', 'Blank (percentage not specified)', '0.00 %', '100.00 %', '50.00 %', '0.00 %']
 ['Toluene', '108-88-3', 'Blank (percentage not specified)', '0.00 %', '100.00 %', '50.00 %', '0.00 %']
 ['N-Hexane', '110-54-3', 'Blank (percentage not specified)', '0.00 %', '100.00 %', '50.00 %', '0.00 %']
 ['Cyclohexane', '110-82-7', 'Blank (percentage not specified)', '0.00 %', '100.00 %', '50.00 %', '0.00 %']
 ['Xylene (Mixed Isomers)', '1330-20-7', 'Blank (percentage not specified)', '0.00 %', '100.00 %', '50.00 %', '0.00 %']
 ['Trimethylbenzene. All Isomers', '25551-13-7', 'Blank (percentage not specified)', '0.00 %', '100.00 %', '50.00 %', '0.00 %']
 ['Benzene', '71-43-2', 'Blank (percentage not specified)', '0.00 %', '100.00 %', '50.00 %', '0.00 %']
 ['Naphthalene', '91-20-3', 'Blank (percentage not specified)', '0.00 %', '100.00 %', '50.00 %', '0.00 %']]
[(9,), (9, 4)]

【问题讨论】:

    标签: python arrays list numpy list-comprehension


    【解决方案1】:

    numpy 不支持不规则数组(并非每一行都具有相同长度的数组)。你的Ingredients 似乎衣衫褴褛:

    >>> [len(x) for x in Ingredients]
    [8, 8, 8, 7, 8, 8, 8, 8, 8, 8]
    

    由于 numpy 不支持这一点,它会尽力而为,并为您提供一个长度为 10 的 object dtype 数组。也就是说,你得到的不是一个 10x8 的数组,而是一个形状为 10 的数组,其中每个元素只是一个对象(恰好是一个 Python 列表,但此时 numpy 不再关心)。

    您需要在 numpy 看到它之前使数组成为矩形,无论是通过修复您的处理还是通过填充或添加虚拟值,无论是否合适。

    请注意,尽管 numpy 并不是真正用于处理混合类型的表格数据:为此,您可能希望使用 pandas

    【讨论】:

      猜你喜欢
      • 2017-03-08
      • 2019-08-30
      • 2015-01-07
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      相关资源
      最近更新 更多