和itertools GROUPBY问题 [英] Itertools Groupby Questions

查看:188
本文介绍了和itertools GROUPBY问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力理解itertools.groupby是如何工作的。我有一个在第一列,按目的地纬度列2和目的地经度列3.交货日期早些时候,我能够得到的帮助分组的日期那名同进的子阵列的Excel US preadsheet较大的阵列持有它们。这里是code,它做的。

 与xlrd.open_workbook(file_location)为工作簿:
    片= workbook.sheet_by_index(0)    日期=(sheet.cell_value(I,0),我在范围内(sheet.nrows))
    天= [列表(组)为重点,组itertools.groupby(日期)]

现在我需要把它更进一步,组纬度成一个阵列和经度到另一个阵列,但它们分组按天。我试着结合上述的东西像这样的事情中列出的code,但我不知道如何把纬度和经度变量引入和itertools GROUPBY功能。

 与xlrd.open_workbook(file_location)为工作簿:
    片= workbook.sheet_by_index(0)    在范围(sheet.nrows)我:
        纬度=(sheet.cell_value(1,2)为我的range(sheet.nrows))
        DeliveryX = [列表(组)为重点,组itertools.groupby(日期)]
        经度=(sheet.cell_value(I,3)我在范围内(sheet.nrows))
        DeliveryY = [列表(组)为重点,组itertools.groupby(日期)]


解决方案

GROUPBY 需要参数来决定如何组在给定列表中的值。你会做基本的元组像一个迭代:

  T =((纬度,经度,日期),(纬度,经度,日期),...)

您可以通过调用 itertools.izip(纬度,经度,日期)实现这一目标。然后,对块这个列表成组,通过比较所述第三字段,并提取第一和第二组,可以编写

 拉特= [[我为我在G]对于k,G中GROUPBY [0](T,键=拉姆达X:X [2])]
拉特= [[我[1]我用g]对于k,G中GROUPBY(T,键=拉姆达X:X [2])]

I am struggling to understand how itertools.groupby works. I have an excel spreadsheet that has delivery dates in the first column followed by destination Lat in column 2 and destination Lon in column 3. Earlier I was able to get help with grouping the dates that were the same into subarrays of the larger array that holds them. Here is the code that does it.

with xlrd.open_workbook(file_location) as workbook:
    sheet = workbook.sheet_by_index(0)

    Dates = (sheet.cell_value(i,0) for i in range(sheet.nrows))
    Day = [list(group) for key, group in itertools.groupby(Dates)]

Now I need to take it a step further and group the Lat into one array and Lon into another array, but group them by the day. I've tried combining the the code listed above with something like something like this, but I do not know how to incorporate the Lat and the Lon variables into itertools groupby function.

with xlrd.open_workbook(file_location) as workbook:
    sheet = workbook.sheet_by_index(0)

    for i in range(sheet.nrows):
        Lat = (sheet.cell_value(i,2) for i in range(sheet.nrows))
        DeliveryX = [list(group) for key, group in itertools.groupby(Dates)]
        Lon = (sheet.cell_value(i,3) for i in range(sheet.nrows))
        DeliveryY = [list(group) for key, group in itertools.groupby(Dates)] 

解决方案

groupby takes a key argument to decide how to group the values in the given list. You would essentially make an iterable of tuples like:

t = ((lat, lon, date), (lat, lon, date), ...)

You can achieve this by calling itertools.izip(Lat, Lon, Dates). Then, to chunk this list into groups by comparing the third fields, and extract the first and second ones, you can write

lats = [[i[0] for i in g] for k, g in groupby(t, key=lambda x: x[2])]
lats = [[i[1] for i in g] for k, g in groupby(t, key=lambda x: x[2])]

这篇关于和itertools GROUPBY问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆