久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 groupby 周圍列出空組

list around groupby results in empty groups(在 groupby 周圍列出空組)
本文介紹了在 groupby 周圍列出空組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

為了更好地了解 itertools groupby 的感覺,所以我按數(shù)字對元組列表進(jìn)行了分組,并嘗試獲取結(jié)果組的列表.但是,當(dāng)我將 groupby 的結(jié)果轉(zhuǎn)換為列表時,我得到了一個奇怪的結(jié)果:除了最后一組之外,所有的都是空的.這是為什么?我認(rèn)為將迭代器轉(zhuǎn)換為列表效率會降低,但永遠(yuǎn)不會改變行為.我猜列表是空的,因為遍歷了內(nèi)部迭代器,但是什么時候/在哪里發(fā)生?

I was playing around to get a better feeling for itertools groupby, so I grouped a list of tuples by the number and tried to get a list of the resulting groups. When I convert the result of groupby to a list however, I get a strange result: all but the last group are empty. Why is that? I assumed turning an iterator into a list would be less efficient but never change behavior. I guess the lists are empty because the inner iterators are traversed but when/where does that happen?

import itertools

l=list(zip([1,2,2,3,3,3],['a','b','c','d','e','f']))
#[(1, 'a'), (2, 'b'), (2, 'c'), (3, 'd'), (3, 'e'), (3, 'f')]

grouped_l = list(itertools.groupby(l, key=lambda x:x[0]))
#[(1, <itertools._grouper at ...>), (2, <itertools._grouper at ...>), (3, <itertools._grouper at ...>)]

[list(x[1]) for x in grouped_l]
[[], [], [(3, 'f')]]


grouped_i = itertools.groupby(l, key=lambda x:x[0])
#<itertools.groupby at ...>
[list(x[1]) for x in grouped_i]
[[(1, 'a')], [(2, 'b'), (2, 'c')], [(3, 'd'), (3, 'e'), (3, 'f')]]

推薦答案

來自 itertools.groupby() 文檔:

返回的組本身是一個迭代器,它與 groupby() 共享底層迭代器.因為源是共享的,當(dāng)groupby()對象前進(jìn)時,之前的組就不再可見了.

The returned group is itself an iterator that shares the underlying iterable with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible.

groupby() 的輸出轉(zhuǎn)換為列表會推進(jìn) groupby() 對象.

Turning the output from groupby() into a list advances the groupby() object.

因此,您不應(yīng)該將 itertools.groupby 對象類型轉(zhuǎn)換為列表.如果您想將值存儲為 list,那么您應(yīng)該執(zhí)行類似 list comprehension 的操作,以便創(chuàng)建 groupby 對象的副本:

Hence, you shouldn't be type-casting itertools.groupby object to list. If you want to store the values as list, then you should be doing something like this list comprehension in order to create copy of groupby object:

grouped_l = [(a, list(b)) for a, b in itertools.groupby(l, key=lambda x:x[0])]

這將允許您多次迭代您的列表(從 groupby 對象轉(zhuǎn)換).但是,如果您對只迭代一次結(jié)果感興趣,那么您在問題中提到的第二個解決方案將滿足您的要求.

This will allow you to iterate your list (transformed from groupby object) multiple times. However, if you are interested in only iterating the result once, then the second solution you mentioned in the question will suffice your requirement.

這篇關(guān)于在 groupby 周圍列出空組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 精品欧美色视频网站在线观看 | 精品一区二区免费视频 | 四虎永久免费地址 | 国产精品久久国产精品 | 国产精久久久久久久 | 成人综合一区 | 亚洲男人天堂av | 99精品免费 | 黄色大片视频 | 日韩在线观看中文字幕 | 精品日韩| 久久麻豆精品 | 在线观看av免费 | 久久国产婷婷国产香蕉 | 蜜桃视频麻豆 | 亚洲国产精品久久久 | 久久久蜜臀国产一区二区 | 成人福利在线视频 | 亚洲精品久久久久久久久久久久久 | 亚洲av毛片 | 三级视频在线观看电影 | 国内精品在线视频 | 日韩中文欧美 | 亚洲欧美精 | 久久在线视频 | 久久综合伊人 | 一区二区三区视频在线免费观看 | 青草福利 | 中文字幕91 | 久久精品亚洲精品国产欧美kt∨ | 在线视频a | 欧美一区二区三区大片 | 欧美精品中文字幕久久二区 | 亚洲一区二区三区视频 | 97成人在线 | 99精品国产在热久久 | 日本淫视频 | 亚洲高清在线观看 | 久久99精品久久久水蜜桃 | 91中文字幕在线 | 国产乱码精品1区2区3区 |