【发布时间】: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