Python 创建列表而不是数组
我对Python非常陌生,甚至不确定我是否正确地表达了标题。我试图得到一个包含两年统计数据的数据集(一个低于另一个)。这不起作用,因为我相信“table”变量是一个数组,其中每年的数据都是它自己的元素。如果我删除两行代码(以下指定为“Line1”和“Line2”),我确实得到了这两行代码,但是没有正确的形状;我使用“thm”变量的长度来确定一年中的行数,然后在将其附加到“table”变量之前使用numpy的重塑函数。谢谢Python 创建列表而不是数组,python,list,Python,List,我对Python非常陌生,甚至不确定我是否正确地表达了标题。我试图得到一个包含两年统计数据的数据集(一个低于另一个)。这不起作用,因为我相信“table”变量是一个数组,其中每年的数据都是它自己的元素。如果我删除两行代码(以下指定为“Line1”和“Line2”),我确实得到了这两行代码,但是没有正确的形状;我使用“thm”变量的长度来确定一年中的行数,然后在将其附加到“table”变量之前使用numpy的重塑函数。谢谢 import bs4 from bs4 import BeautifulS
import bs4
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd
import numpy as np
table=[]
for i in range(1956,1958):
url="https://www.basketball-reference.com/draft/NBA_{}.html#stats::none".format(i)
soup=BeautifulSoup(urlopen(url), "html.parser")
thm=[]
for tag in soup.find_all("tr"):
list=tag.find("th", class_="right")
if list:
thm.append(list.text)
mtable1=[]
trs=soup.find_all("tr")
for tr in trs:
tds=tr.find_all("td")
for td in tds:
mtable1.append(td.text)
#Line1
mtable2=[mtable1]
#Line2
mtable3=np.reshape(mtable2,[len(thm),21])
table.append(mtable3)
data=pd.DataFrame(table)
data.to_csv("data.csv")
找到了答案。我只需要将数组转换成一个列表,然后通过迭代将列表扩展到以后的年份
import bs4
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd
import numpy as np
table=[]
for i in range(1956,1958):
url="https://www.basketball-reference.com/draft/NBA_{}.html#stats::none".format(i)
soup=BeautifulSoup(urlopen(url), "html.parser")
#Obtaining table's headers
tag1=soup.find_all("tr")[1]
header=[]
for tag in tag1.find_all("th"):
list1=tag.text
if list1:
header.append(list1)
header=header[1:]
#Creating another column for the year of the draft
header.append("Year")
thm=[]
for tag in soup.find_all("tr"):
list=tag.find("th", class_="right")
if list:
thm.append(list.text)
mtable1=[]
trs=soup.find_all("tr")
for tr in trs:
tds=tr.find_all("td")
for td in tds:
mtable1.append(td.text)
mtable2=[mtable1]
mtable3=np.reshape(mtable2,[len(thm),21])
mtable4=mtable3.tolist()
for row in mtable4:
row.append(i)
table.extend(mtable4)
data=pd.DataFrame(table, columns=header)
data.to_csv("data.csv")
找到了答案。我只需要将数组转换成一个列表,然后通过迭代将列表扩展到以后的年份
import bs4
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd
import numpy as np
table=[]
for i in range(1956,1958):
url="https://www.basketball-reference.com/draft/NBA_{}.html#stats::none".format(i)
soup=BeautifulSoup(urlopen(url), "html.parser")
#Obtaining table's headers
tag1=soup.find_all("tr")[1]
header=[]
for tag in tag1.find_all("th"):
list1=tag.text
if list1:
header.append(list1)
header=header[1:]
#Creating another column for the year of the draft
header.append("Year")
thm=[]
for tag in soup.find_all("tr"):
list=tag.find("th", class_="right")
if list:
thm.append(list.text)
mtable1=[]
trs=soup.find_all("tr")
for tr in trs:
tds=tr.find_all("td")
for td in tds:
mtable1.append(td.text)
mtable2=[mtable1]
mtable3=np.reshape(mtable2,[len(thm),21])
mtable4=mtable3.tolist()
for row in mtable4:
row.append(i)
table.extend(mtable4)
data=pd.DataFrame(table, columns=header)
data.to_csv("data.csv")