Python,结合3列中的独特内容(Excel电子表格)

美好的一天。在Excel电子表格里有一些数据,像这样构build:

在这里输入图像说明

我们希望把3列中的唯一值放在一起,很好地转化为如下格式:

Mike to America for Hotel; Meal 

 Kate to Europe; America for Transport 

等等

我只能解决2列。

 the_file = xlrd.open_workbook("testing.xlsx") the_sheet = the_file.sheet_by_name("Sheet1") products = defaultdict(list) for row_index in range(1, the_sheet.nrows): products[str(the_sheet.cell(row_index, 0).value)].append(the_sheet.cell(row_index, 1).value) for product, v in products.items() print product + " to " + ";".join(set(v)) 

输出是:

 Mike to America Hulk to America;Asia Kate to Europe;America Dave to Europe Jack to Europe;America;Asia Luci to Asia 

怎样才能使这三条线在一起工作?
谢谢。

想想还有更多的pythonic办法,但这是我想出的:

 from collections import defaultdict l = [ ['mike', 'america', 'hotel'], ['mike', 'america', 'meal'], ['jack', 'america', 'meal'], ['jack', 'europe', 'hotel'], ['jack', 'america', 'bonus'], ['jack', 'asia', 'hotel'], ['dave', 'europe', 'meal'], ] people = defaultdict(list) people_places = defaultdict(list) for row_index in range(len(l)): people[l[row_index][0]].append(l[row_index][1]) people_places[l[row_index][0] + '|' + l[row_index][1]].append(l[row_index][2]) for p, k in people.items(): activity = [] for place in k: activity += people_places[p + '|' + place] print '{} to {} for {}'.format( p, ';'.join(set(k)), ';'.join(set(activity)) ) 

您可以将代码翻译为直接使用电子表格行和单元格,或者先用类似的方法提取列表l

 l = [] with xlrd.open_workbook("testing.xlsx") as the_file: the_sheet = the_file.sheet_by_name("Sheet1") for row_index in range(1, the_sheet.nrows): l.append([ the_sheet.cell(row_index, 0).value, the_sheet.cell(row_index, 1).value, the_sheet.cell(row_index, 2).value]) 

首先你在这里提取你想要的行,我把它作为一个嵌套列表即[[col1, col2, col3],[col1, col2, col3]]

 box = list() bigbox = [] for i in range(len(the_sheet.col(1))): if i > 2: for j in range(1,4): box.append(str(the_sheet.col(j)[i]).split(":")[1]) bigbox.append(box) box = [] print bigbox 

然后我将嵌套列表转换成嵌套集合的嵌套字典,即{'name':{'travel': set of travel, 'expense': set of expense}, ...}

 dbox = dict() for name, travel, expense in bigbox: if name not in dbox: dbox[name] = {'travel': {travel}, 'expense': {expense}} else: dbox[name]['travel'].add(travel) dbox[name]['expense'].add(expense) print dbox 

最后你打印出来使用一些巫术魔法阅读文档的更多信息

 for name in dbox: print(name, 'to', "; ".join(dbox[name]['travel']), 'for', "; ".join(dbox[name]['expense'])) 

希望这个帮助,我想抱怨你怎么不给我的Excel文件,我需要创build,在我自己的下一次包括它,这是一些编程课程任务亲切?

这个时候给我的解决scheme是:

 from collections import defaultdict the_file = xlrd.open_workbook("4_test.xlsx") the_sheet = the_file.sheet_by_name("Sheet1") nested_dict = lambda: defaultdict(nested_dict) _dict = nested_dict() for row_index in range(1, the_sheet.nrows): expense = [] travel = [] name = str(the_sheet.cell(row_index, 0).value) for row_index_1 in range(1, the_sheet.nrows): if name == str(the_sheet.cell(row_index_1, 0).value): travel.append(str(the_sheet.cell(row_index_1, 1).value)) expense.append(str(the_sheet.cell(row_index_1, 2).value)) _dict[name]['travel'] = travel _dict[name]['expense']= expense for name in _dict: print name + " to "+ ",".join(set(_dict[name]['travel'])) + " for " + ",".join(set(_dict[name]['expense'])) 

输出::

Pintu到欧洲的Bonous

杰克欧洲,美国的酒店,餐

迈克去美国的Bonous,Hotel,Transport