有 OrderedDict 理解吗?
Is there an OrderedDict comprehension?
我不知道是否有这样的事情 - 但我正在尝试进行有序的字典理解。不过好像不行?
import requests
from bs4 import BeautifulSoup
from collections import OrderedDict
soup = BeautifulSoup(html, 'html.parser')
tables = soup.find_all('table')
t_data = OrderedDict()
rows = tables[1].find_all('tr')
t_data = {row.th.text: row.td.text for row in rows if row.td }
它现在保留为正常的字典理解(我也忽略了对 soup 样板文件的通常请求)。
有什么想法吗?
您不能直接对 OrderedDict
进行理解。但是,您可以在 OrderedDict
.
的构造函数中使用生成器
试穿尺码:
import requests
from bs4 import BeautifulSoup
from collections import OrderedDict
soup = BeautifulSoup(html, 'html.parser')
tables = soup.find_all('table')
rows = tables[1].find_all('tr')
t_data = OrderedDict((row.th.text, row.td.text) for row in rows if row.td)
我不知道是否有这样的事情 - 但我正在尝试进行有序的字典理解。不过好像不行?
import requests
from bs4 import BeautifulSoup
from collections import OrderedDict
soup = BeautifulSoup(html, 'html.parser')
tables = soup.find_all('table')
t_data = OrderedDict()
rows = tables[1].find_all('tr')
t_data = {row.th.text: row.td.text for row in rows if row.td }
它现在保留为正常的字典理解(我也忽略了对 soup 样板文件的通常请求)。 有什么想法吗?
您不能直接对 OrderedDict
进行理解。但是,您可以在 OrderedDict
.
试穿尺码:
import requests
from bs4 import BeautifulSoup
from collections import OrderedDict
soup = BeautifulSoup(html, 'html.parser')
tables = soup.find_all('table')
rows = tables[1].find_all('tr')
t_data = OrderedDict((row.th.text, row.td.text) for row in rows if row.td)