我有一个像
这样的元组列表data = [
('di', 'c1', 'avg11'),
('di', 'c2', 'stdev12'),
('eng', 'c1', 'stdev21'),
('eng', 'c2', 'stdev22')
]
如何将'di'
和'eng'
项目放入字典中,如下所示:
data1 = {'c1':'avg11','c2':'stdev12'}
data2 = {'c1':'stdev21','c2':'stdev22'}
使用pandas或numpy还是普通的python?
答案 0 :(得分:2)
使用<nav id="site-navigation" class="main-navigation" role="navigation" aria-label="Menu supérieur">
<ul id="top-menu" class="menu">
<li a="" id="nav-menu-item-2105" class="main-menu-item menu-first menu-item-depth-0 menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children has_children mega2"><a href="http://localhost/wordpress/commande/" class="menu-link main-menu-link"><span>Commande</span></a>
<div class="sf-mega">
<div class="sf-mega-inner clearfix">
<ul class="navi first menu-depth-1">
<li a="" id="nav-menu-item-2106" class=" menu-item-depth-1 menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children has_children normal_menu_item"><a href="http://localhost/wordpress/panier/" class="menu-link sub-menu-link"><span>Panier</span></a>
<ul class="navi navi menu-depth-2">
<li a="" id="nav-menu-item-2107" class=" navi menu-item-depth-2 menu-item menu-item-type-post_type menu-item-object-page normal_menu_item"><a href="http://localhost/wordpress/boutique/" class="menu-link sub-menu-link"><span>Boutique</span></a></li>
<li a="" id="nav-menu-item-2108" class=" navi menu-item-depth-2 menu-item menu-item-type-post_type menu-item-object-page normal_menu_item"><a href="http://localhost/wordpress/blog/" class="menu-link sub-menu-link"><span>Blog</span></a></li>
<li a="" id="nav-menu-item-2109" class=" navi menu-item-depth-2 menu-item menu-item-type-post_type menu-item-object-page normal_menu_item"><a href="http://localhost/wordpress/about/page-markup-and-formatting/" class="menu-link sub-menu-link"><span>Page Markup And Formatting</span></a></li>
</ul>
</li>
<li a="" id="nav-menu-item-2113" class=" menu-item-depth-1 menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children has_children normal_menu_item"><a href="http://localhost/wordpress/panier/" class="menu-link sub-menu-link"><span>Panier</span></a>
<ul class="navi navi menu-depth-2">
<li a="" id="nav-menu-item-2112" class=" navi menu-item-depth-2 menu-item menu-item-type-post_type menu-item-object-page normal_menu_item"><a href="http://localhost/wordpress/level-1/level-2/level-3a/" class="menu-link sub-menu-link"><span>Level 3a</span></a></li>
<li a="" id="nav-menu-item-2111" class=" navi menu-item-depth-2 menu-item menu-item-type-post_type menu-item-object-page normal_menu_item"><a href="http://localhost/wordpress/level-1/level-2/level-3b/" class="menu-link sub-menu-link"><span>Level 3b</span></a></li>
<li a="" id="nav-menu-item-2110" class=" navi menu-item-depth-2 menu-item menu-item-type-post_type menu-item-object-page normal_menu_item"><a href="http://localhost/wordpress/about/page-image-alignment/" class="menu-link sub-menu-link"><span>Page Image Alignment</span></a></li>
</ul>
</li>
</ul>
</div>
</div>
</li>
<li a="" id="nav-menu-item-2114" class="main-menu-item menu-item-depth-0 menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children has_children normal_menu_item"><a href="http://localhost/wordpress/boutique/" class="menu-link main-menu-link"><span>Boutique</span></a>
<ul class="navi first menu-depth-1">
<li a="" id="nav-menu-item-2115" class=" menu-item-depth-1 menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children has_children normal_menu_item"><a href="http://localhost/wordpress/page-d-exemple/" class="menu-link sub-menu-link"><span>Page d’exemple</span></a>
<ul class="navi navi menu-depth-2">
<li a="" id="nav-menu-item-2116" class=" navi menu-item-depth-2 menu-item menu-item-type-post_type menu-item-object-page normal_menu_item"><a href="http://localhost/wordpress/page-b/" class="menu-link sub-menu-link"><span>Page B</span></a></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
你可以做
Pandas
答案 1 :(得分:2)
在纯Python中,只需构建一个顶级字典,如果仍然缺少,使用dict.setdefault()
来实现嵌套字典:
result = {}
for outer, inner, value in data:
result.setdefault(outer, {})[inner] = value
这是对数据进行分组的最简单,更易读的方法。
演示:
>>> data = [
... ('di', 'c1', 'avg11'),
... ('di', 'c2', 'stdev12'),
... ('eng', 'c1', 'stdev21'),
... ('eng', 'c2', 'stdev22')
... ]
>>> result = {}
>>> for outer, inner, value in data:
... result.setdefault(outer, {})[inner] = value
...
>>> result
{'di': {'c1': 'avg11', 'c2': 'stdev12'}, 'eng': {'c1': 'stdev21', 'c2': 'stdev22'}}
如果必须之后有data1
和data2
个变量,只需在'di'
和'eng'
键中设置:
data1 = result['di']
data2 = result['eng']
但如果你有更多或更少或不同的外键,这是相当不灵活的。
答案 2 :(得分:0)
使用普通的python你可以做到
def process_data(data):
res = {}
for itm in data:
if itm[0] not in res:
res[itm[0]] = {}
res[itm[0]][itm[1]] = itm[2]
return res
使用示例:
>>> d = [
... ('di', 'c1', 'avg11'),
... ('di', 'c2', 'stdev12'),
... ('eng', 'c1', 'stdev21'),
... ('eng', 'c2', 'stdev22')
... ]
>>> print(process_data(d))
{'eng': {'c2': 'stdev22', 'c1': 'stdev21'}, 'di': {'c2': 'stdev12', 'c1': 'avg11'}}
答案 3 :(得分:0)
使用itertools.groupby()
功能:
import itertools
data = [
('di', 'c1', 'avg11'), ('di', 'c2', 'stdev12'),
('eng', 'c1', 'stdev21'), ('eng', 'c2', 'stdev22')
]
result = {k: {i[1]:i[2] for i in g}
for k,g in itertools.groupby(sorted(data), key=lambda x: x[0])}
print(result)
输出:
{'di': {'c1': 'avg11', 'c2': 'stdev12'}, 'eng': {'c1': 'stdev21', 'c2': 'stdev22'}}