栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

产生摘要(“枢轴”?)表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

产生摘要(“枢轴”?)表

在python方面,您可以使用itertools魔术来重新排列数据:

data = [('Apple',      'Coles',      1.50),        ('Apple',      'Woolworths', 1.60),        ('Apple',      'IGA',        1.70),        ('Banana',     'Coles',      0.50),        ('Banana',     'Woolworths', 0.60),        ('Banana',     'IGA',        0.70),        ('Cherry',     'Coles',      5.00),        ('Date',       'Coles',      2.00),        ('Date',       'Woolworths', 2.10),        ('Elderberry', 'IGA',        10.00)]from itertools import groupby, islicefrom operator import itemgetterfrom collections import defaultdictstores = sorted(set(row[1] for row in data))# probably splitting this up in multiple lines would be more readablepivot = ((fruit, defaultdict(lambda: None, (islice(d, 1, None) for d in data))) for fruit, data in groupby(sorted(data), itemgetter(0)))print 'Fruit'.ljust(12), 't'.join(stores)for fruit, prices in pivot:    print fruit.ljust(12), 't'.join(str(prices[s]) for s in stores)

输出:

Fruit        Coles      IGA     WoolwApple        1.5        1.7     1.6Banana       0.5        0.7     0.6Cherry       5.0        None    NoneDate         2.0        None    2.1Elderberry   None       10.0    None


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516789.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号