Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"It's not just a computer -- it's your ass." -- Cal Keegan


devel / comp.lang.python / Re: A problem with itertools.groupby

SubjectAuthor
* A problem with itertools.groupbyast
+- Re: A problem with itertools.groupbyAntoon Pardon
+- Re: A problem with itertools.groupbyChris Angelico
`- Re: A problem with itertools.groupbyPeter Pearson

1
A problem with itertools.groupby

<61bc495f$0$29477$426a74cc@news.free.fr>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16427&group=comp.lang.python#16427

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.uzoreto.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!94.232.112.244.MISMATCH!feed.abavia.com!abe004.abavia.com!abe001.abavia.com!proxad.net!feeder1-1.proxad.net!cleanfeed2-b.proxad.net!nnrp1-1.free.fr!not-for-mail
Date: Fri, 17 Dec 2021 09:25:03 +0100
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Thunderbird/91.4.0
Newsgroups: comp.lang.python
Content-Language: fr
From: ast...@invalid (ast)
Subject: A problem with itertools.groupby
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 53
Message-ID: <61bc495f$0$29477$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 17 Dec 2021 09:25:03 CET
NNTP-Posting-Host: 91.170.32.5
X-Trace: 1639729503 news-2.free.fr 29477 91.170.32.5:3622
X-Complaints-To: abuse@proxad.net
 by: ast - Fri, 17 Dec 2021 08:25 UTC

Python 3.9.9

Hello

I have some troubles with groupby from itertools

from itertools import groupby

for k, grp in groupby("aahfffddssssnnb"):
print(k, list(grp))
print(k, list(grp))

a ['a', 'a']
a []
h ['h']
h []
f ['f', 'f', 'f']
f []
d ['d', 'd']
d []
s ['s', 's', 's', 's']
s []
n ['n', 'n']
n []
b ['b']
b []

It works as expected.
itertools._grouper objects are probably iterators
so they provide their datas only once. OK

but:

li = [grp for k, grp in groupby("aahfffddssssnnb")]
list(li[0])

[]

list(li[1])

[]

It seems empty ... I don't understand why, this is
the first read of an iterator, it should provide its
data.

This one works:

["".join(grp) for k, grp in groupby("aahfffddssssnnb")]

['aa', 'h', 'fff', 'dd', 'ssss', 'nn', 'b']

regards

Re: A problem with itertools.groupby

<mailman.13.1639755720.1567.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16432&group=comp.lang.python#16432

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: antoon.p...@vub.be (Antoon Pardon)
Newsgroups: comp.lang.python
Subject: Re: A problem with itertools.groupby
Date: Fri, 17 Dec 2021 16:41:57 +0100
Lines: 26
Message-ID: <mailman.13.1639755720.1567.python-list@python.org>
References: <61bc495f$0$29477$426a74cc@news.free.fr>
<f8165b55-7e70-8f91-ee80-597c71dd6599@vub.be>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de XbdL3WiDKc5zO87ptbuRDAHo/+O4N0w/Gdoq8JFDq6OA==
Return-Path: <Antoon.Pardon@vub.be>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=vub.be header.i=@vub.be header.b=uN7OGkxO; dkim-adsp=pass;
dkim-atps=neutral
X-Spam-Status: OK 0.066
X-Spam-Evidence: '*H*': 0.89; '*S*': 0.02; 'subject:problem': 0.03;
'antoon': 0.16; 'condition.': 0.16; 'from:addr:antoon.pardon':
0.16; 'from:addr:vub.be': 0.16; 'from:name:antoon pardon': 0.16;
'message-id:@vub.be': 0.16; 'pardon.': 0.16; 'termination': 0.16;
'wrapper': 0.16; 'values': 0.17; 'to:addr:python-list': 0.20;
'seems': 0.26; 'header:User-Agent:1': 0.30; 'empty': 0.32; 'header
:In-Reply-To:1': 0.34; 'read': 0.38; 'data.': 0.40; 'should':
0.40; 'provide': 0.60; 'produce': 0.65; 'order': 0.69;
'subject:skip:i 10': 0.69; 'extra': 0.84; 'connected.': 0.84;
'received:eurprd01.prod.exchangelabs.com': 0.84;
'received:eur03-ve1-obe.outbound.protection.outlook.com': 0.91
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
b=ivcE4ME/jYiBTgNhrL4vW38l1AQhZjbOwOyX6Tsc2Kph/aS+k7BRAuQ+SDGB3CLCUwbnvpZuZEU/YALy56gsRqenvmbrGcShPTmJIHqs86ZW4AK8I+5LporDoIarELnRU0xwNyxgowK52C1hsC/oPM8wYw/JbUfBurfYmCAJXZLxAbLiRbrWc9nMj51li917qJvFtVZ2JCKvw1rH48q1x4IWMIT0nSdTavneeR3D1y2wK8O0zMYuNStdLExpgLp2eg9pHm43o/Muim/Kvnnum6zVeLaHPeKvR/4cUJVO4YESyN6gn/HDAXxf0csuPTd8R7ttOEcEhlBTgce3H9wvig==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector9901;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=h+2xVN/YNN8mUPe9McDv5rg9+gYB5tN5IN3EvEPeepQ=;
b=KL3hCu0iqg/lRpTI75aRvjmEpH7xmFz0Otmt/fMO6htSLWZvVgnS+0Zl65KNzJKtM1ohhCHxA1ljzJGe0xnH0Csa6YurtqZMJcmcEqLdqG9n0RWUHK9HBtA4JysWlkzssrsra/QLjSFudm0Ewm1BW2UgUB07kj8MND4B2PsayASfavURkkrdShUfaSKcH4yoHa3IyCgZaFPmMGKtG0xga7YHundS+itCM/dAIrqnxCTQ76MPdMe+YRxLJiFY3H4g7iaPnRYjhRvLuq8OeXQhOafT1xdrnMEE/79R6xmZAJQfen5UGJsiLCzYBLU8x4e2BsngyQ1+Owkb+lYz2VQktA==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
smtp.mailfrom=vub.be; dmarc=pass action=none header.from=vub.be; dkim=pass
header.d=vub.be; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vub.be; s=selector1;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=h+2xVN/YNN8mUPe9McDv5rg9+gYB5tN5IN3EvEPeepQ=;
b=uN7OGkxOpMS/jht8uzf83C0R9VezLhn/oH5vvh5Nzb5XtfCFSCxC4tu/DI1pte/rA7GD9BqoZQK6pc4F85n7bVYd9D6yQ3qx6XWK4+ESNt6OXaW66fBVw6W/GUvdJNQ1JlHkeS+CotQCeECpEMQIqwHIN1RpmOMsudmopJQ0f/U=
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vub.be;
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Content-Language: nl-BE
In-Reply-To: <61bc495f$0$29477$426a74cc@news.free.fr>
X-ClientProxiedBy: AM4PR0501CA0059.eurprd05.prod.outlook.com
(2603:10a6:200:68::27) To AM0PR01MB4147.eurprd01.prod.exchangelabs.com
(2603:10a6:208:6a::12)
X-MS-PublicTrafficType: Email
X-MS-Office365-Filtering-Correlation-Id: 18894c24-ff18-4392-adf3-08d9c173c287
X-MS-TrafficTypeDiagnostic: AM0PR01MB5698:EE_
X-Microsoft-Antispam-PRVS: <AM0PR01MB5698383F9B4758177DD08BE78F789@AM0PR01MB5698.eurprd01.prod.exchangelabs.com>
X-MS-Oob-TLC-OOBClassifiers: OLM:8273;
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: rowsQYyS+sytSKtvStCQXjKRpW4rG27LS7uBAgVI20MZYDagemR8v4mqEAKnR/B+5TIif4ShG29d67upRPbEx17bX3R/4jYBPO2U/eeXMsL9HEKVP1V6iEar3GVB39e4sg7+UxWLSaGrVlmPjKmqdDf4rAzJ1DjifeEmHzswRHcLqwj9xiCKfbThoF+GhWIk/NuMW5aK7JzaqCaQbtlV8r83yS3Kp1z+smhL39CXBqlaMlOgTR4JunVssRvzBJaKnOPbBRVU9BAGQ1FMAvfOazzhIQjUQPg1QSCMYe+0Vdl7OFVAFFUej64Bpp0GjcA3VERlPP8lxxtKMIFlVNGr1AUBpAITZNlyVW2cEqz3izHmcb6FjY96qA/nP9heF+EyZVvPxmNtpOwxTaseunETkvzgQdysfhC69VSnMaIxZIBKaGwC0Ps6IENozVyLKkA4NQjS1F1VkqhGiAEqyweo2haX6RKgqVAF7fDeN9vf1VgV1OQW6KJxLnqachC2NxDF1nv7B6MA5b90ookKC5amLXobvqy31WJpoHuBcPhGksDzvHGJgUAGtxxGMFNm3sLfbSIbLQQ0eEQXFiL8pYvyclNKoVNn4VyZV83kAK6BV0xlypaLvzu7Zc66eDTre37277euj+93r0j5E0c1cLQV4/rV8iP7dFPxW1WxPWaa+qlJtoAaBuElkAGfOVdPF8veNPEs/V2l/xXL7iSTPesp0A==
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:AM0PR01MB4147.eurprd01.prod.exchangelabs.com; PTR:;
CAT:NONE;
SFS:(4636009)(366004)(31686004)(66476007)(66556008)(66946007)(6486002)(36756003)(6512007)(786003)(316002)(8676002)(8936002)(4744005)(508600001)(83380400001)(5660300002)(186003)(52116002)(6506007)(44832011)(2906002)(6916009)(86362001)(31696002)(38100700002)(2616005)(43740500002);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0: Goo3l4AUwHp/617m851172/vxZRjsrs/88MfZyyRArwQr
IYcGzHltWgCxp+jNkmUywdFJpTJP/QwuJ/tR3IknuBL+r
AglHXICkdZ8R/mTVXwMaMdMhiE/MFXl4mduVp2U1trp0X
QpH/brtDzOWdrbab6GNfc98jyPG3IPbThol55ZMoSNZ9X
58PgNlRYXBw4/XBpiP9dPG/dVHaQX6HS1K6ldVurAIDf0
CzV+vbauteULHa1FvA3yfsWtli34kKJV/EZdjO/lrGJqq
hCeIkALngreL4xNErx8krms52WBwkKTMFvjXnOBqCFPJQ
dQtDZnanqRYUlERqOzIpjjEgWWDDLD236F+QKit53nGhX
cTceveVrtv3T4mcfd8qhiJgYLBzLXApdi0lexjlKyUDZ0
rmyjzfAU6+oE/eJoOc3CpLG95JQq52p2PjwDb7sU9e7Yb
0NSIGDpwHB/yWi9CRLyfrQjLKgSR4KBNBJVv3BDPj8bTw
J/wclZRtx8q0KMRif/Ng7fFckzdHHVgcHeIPDwwV7iVyf
s6sHvNHoqC+NkZD7BhMqLKs/YK3k1jn16cKFHyyzCU6SV
WcAnBnOw24ZRZxWhHr+bQtqxtBzpvRbkFJrkw9AaDSdMV
rLlBWKPKgYhZgoXHGkMbK86me86d0ueok9+kdsRrUWDVC
pSWwcM8ZmtyoIB2wvW5mv+3LeGbyV9SIHG/63iNhj8q3g
knlGhpPEvhM5ptUAhBLmHdeO/D8xqRN8BRfouro+PI5wV
mPZ9BCC+OxMTM547JypOvtC/UM2Yjn5LcOCOoMIYO5Mhb
tSxukzs+4SSnN/AmUO3sEeA/pOZ3XQTNy6vZyx/Dg1CQA
5wcY/5xcA/2fDRsmYv3MhdHIfV5DMj8UlE1ojvF6ZG8Db
uTRrkwfVPJ8eL8js91BfzjA3/mVSMcVJ6YQEmwEixMmbm
ZOLOKkNHIjAxwyFoA/gR/KJTGK3pGTc0fRG200FLIKwFh
HZdO34TWFJMwG4tCc7Q8xy5qAM25t6MY1ZpHhmBpj/rmv
SKXP0fdIxLa02FNMXLpC9TVeqcod1hOOuAe8ykr9+X/oW
76tkaREPTCLRmrjetS34/VFWxuUj64r/RNvwGpJHz5rQO
yNHSndl8mPk882XJy1Tt0KsnLtajHFX1AQ4tVG9cprVsR
uTs5XoD3oquYc/EQVc3O9txlb7zolTRt8o+Mbp7iu/rjg
gFcsnKT0mdCWDLyGtnVpQGMsjjwGZraMBq/xtKFKS8BR6
Kjv/Ls23oZcUi288tZ3REXsexeScltcnirE+GgZlPk64j
AZafKkJkpS2yrJjy+VoxW2JbBjKMxUxCED45OPgElknDs
41fjR+TwKepwVKPkmyC9Oq2yFSEfafDBDi1RlahQ8pyHZ
WkYaDTQTpqBDclNAELedGXovWns1w/ObRtkWeeam1V/8p
kAFGQ32OBMqJuIIBbFYXcyl68ffbwbui90xbpuLYVQBFe
Z2Iw67MTdmCxFYcjgPvtql+BYG2t4voNMocXkqVx/Zt4D
s0HF3uZsAjVH/ZHK1OkyiBJykstra28rBcGd3zdjoVvAU
SdqREtwp3tyChphTj4rjM74MpANql3977T7Q0CkrxaKZo
oFDlVjXyHodC3mlJQLTZKVGLR5H3TYwnFigzfPZB/9H/m
MtU7V80JHse0Ii1UBDQSDjYQV6ureShMJcvouDsF3Q=
X-OriginatorOrg: vub.be
X-MS-Exchange-CrossTenant-Network-Message-Id: 18894c24-ff18-4392-adf3-08d9c173c287
X-MS-Exchange-CrossTenant-AuthSource: AM0PR01MB4147.eurprd01.prod.exchangelabs.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2021 15:41:58.0113 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 695b7ca8-2da8-4545-a2da-42d03784e585
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName: rAzKBH4OeXVhBXRnNkql/pfVZFtIEqP5A5jU4kldRd8PqG2UsKp6TS+lfKGajZZfT7frNQv9pgW8xbb+hqialA==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR01MB5698
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <f8165b55-7e70-8f91-ee80-597c71dd6599@vub.be>
X-Mailman-Original-References: <61bc495f$0$29477$426a74cc@news.free.fr>
 by: Antoon Pardon - Fri, 17 Dec 2021 15:41 UTC

> but:
>
> li = [grp for k, grp in groupby("aahfffddssssnnb")]
> list(li[0])
>
> []
>
> list(li[1])
>
> []
>
> It seems empty ... I don't understand why, this is
> the first read of an iterator, it should provide its
> data.

The group-iterators are connected. Each group-iterator is a wrapper
around the original iterator
with an extra termination condition. So in order to start the next
group-iterator the previous
group-iterator is exhausted, because the original iterator has to be
ready to produce values
for the next group-iterator.

--
Antoon Pardon.

Re: A problem with itertools.groupby

<mailman.14.1639755857.1567.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16433&group=comp.lang.python#16433

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: A problem with itertools.groupby
Date: Sat, 18 Dec 2021 02:44:04 +1100
Lines: 29
Message-ID: <mailman.14.1639755857.1567.python-list@python.org>
References: <61bc495f$0$29477$426a74cc@news.free.fr>
<CAPTjJmqUQ6PUAbkvJVotzZF9882dkB4ZGiQWQFBdaw97Y0WUPg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de VFh4WgIJ/8hz0OhwErManQ04YUiFPWPVbkYH13cZjU5A==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=SUcUgCwy;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.005
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:problem': 0.03;
'18,': 0.09; 'ast': 0.09; 'cc:addr:python-list': 0.09;
'paragraph': 0.09; 'cc:no real name:2**0': 0.14; 'import': 0.15;
'chrisa': 0.16; 'explanatory': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'itertools': 0.16; 'wrote:':
0.16; 'python': 0.16; 'cc:addr:python.org': 0.20; 'sat,': 0.22;
'cc:2**0': 0.25; 'seems': 0.26; 'dec': 0.31; 'empty': 0.32;
'message-id:@mail.gmail.com': 0.32; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'received:209.85': 0.37; 'read': 0.38; 'received:209': 0.39;
'data.': 0.40; 'should': 0.40; 'provide': 0.60; 'to:none': 0.60;
'subject:skip:i 10': 0.69; '2021': 0.71; '2:32': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:cc;
bh=Kwl+O5bkKiEwHk3FdXbIciuoAwhPyM47W8ansrBndoI=;
b=SUcUgCwyxkf/kuWwLObsjv5Seo5kkwWHS06mFv3dQ9X6hVqaLv7hWY/c2H4WZj0GZh
+m23ZplXxHdd07bUZLosDBGXsdF2Ef3J13MkQWe7hwjBA+n1zFaqpJWZ4MwukEoi08Sr
14LA/MWIpGXG37cJvfNZuCtw4XUJ0YkYS3sWmsqdjKJ2uKL92EdfoVwRefO92ljggxc9
LMTehIHJxhJfK3PMJ7JPpn2tfhhl6YgM5Yric7v94UXQVkH9fRKxY54tC3Y6q41ULc7o
61e6F/CIIKHGd1+qihauitRZOmy+jqfuS57EEGTZDH51spyOfhzXetOdJQZzXGE6i0Dh
F+iA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:cc;
bh=Kwl+O5bkKiEwHk3FdXbIciuoAwhPyM47W8ansrBndoI=;
b=aTNN+oP/uQ5uQ5Q9s8sLIVx4wnHkmh8sg5zfuglJ/rKmP32OVM5SplT96r3VyWiGYa
tQhi15JbnlSLtx7gXC5pqio7kB/2snM9AmCKdw4xLPT7FNPMOkhqqYWqJlK+fz4/WhMt
QGloxYQ/8vyUcmzdY3YXSpuvk3WnbB0LyJR1WljPMeLhHyY8vtFtHI93/32+jJha0IB3
oD7NMdv6nkV38swEydAII4sUo3pCMXLpPxyjQ0nIZSZphXKuFyHsYOZyeFTkruCeWszp
P+5oNhnpGLN1Zq9uXhRjdopjyRj3rcGo9tJoAB0lTihQOYNJwenJiZ0CDDn2/6uqzh7z
itXA==
X-Gm-Message-State: AOAM530nHZZXc4axJdy/KNpTSWMSi4BQdyXnZA4vs/nATCl9PkHDwQ7v
Ng+hvPv20DH5XLlrYb6HNDquPjDPul3kBjuuyQE2PdY7FrM=
X-Google-Smtp-Source: ABdhPJylE85dOFH2kprih6q5lWS9aG0ZsuchyYLuzutPapxz+cBi7Cy9uAOWiW84dXyk6ONT5o6srH4/RgcT2YL4as0=
X-Received: by 2002:a7b:c0d7:: with SMTP id s23mr10125581wmh.135.1639755855874;
Fri, 17 Dec 2021 07:44:15 -0800 (PST)
In-Reply-To: <61bc495f$0$29477$426a74cc@news.free.fr>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmqUQ6PUAbkvJVotzZF9882dkB4ZGiQWQFBdaw97Y0WUPg@mail.gmail.com>
X-Mailman-Original-References: <61bc495f$0$29477$426a74cc@news.free.fr>
 by: Chris Angelico - Fri, 17 Dec 2021 15:44 UTC

On Sat, Dec 18, 2021 at 2:32 AM ast <ast@invalid> wrote:
>
> Python 3.9.9
>
> Hello
>
> I have some troubles with groupby from itertools
>
> from itertools import groupby
>
> li = [grp for k, grp in groupby("aahfffddssssnnb")]
> list(li[0])
>
> []
>
> list(li[1])
>
> []
>
> It seems empty ... I don't understand why, this is
> the first read of an iterator, it should provide its
> data.
>

https://docs.python.org/3/library/itertools.html#itertools.groupby

Check the explanatory third paragraph :)

ChrisA

Re: A problem with itertools.groupby

<j23qriFosbrU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16434&group=comp.lang.python#16434

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: pkpear...@nowhere.invalid (Peter Pearson)
Newsgroups: comp.lang.python
Subject: Re: A problem with itertools.groupby
Date: 17 Dec 2021 16:06:10 GMT
Lines: 32
Message-ID: <j23qriFosbrU1@mid.individual.net>
References: <61bc495f$0$29477$426a74cc@news.free.fr>
X-Trace: individual.net 0IRMLSFbDuAbww1vr4sHEAOi30ZAYSllXhQIJ6lj5ewAwm2O1r
Cancel-Lock: sha1:KRqbFPx2v3kB2lHGw5sJO3c+1cQ=
User-Agent: slrn/1.0.3 (Linux)
 by: Peter Pearson - Fri, 17 Dec 2021 16:06 UTC

On Fri, 17 Dec 2021 09:25:03 +0100, ast <ast@invalid> wrote:
[snip]
>
> but:
>
> li = [grp for k, grp in groupby("aahfffddssssnnb")]
> list(li[0])
>
> []
>
> list(li[1])
>
> []
>
> It seems empty ... I don't understand why, this is
> the first read of an iterator, it should provide its
> data.

Baffling. Here's a shorter and less readable illustration:

>>> list(groupby("aabbb"))
[('a', <itertools._grouper object at 0xb7018c0c>),
('b', <itertools._grouper object at 0xb7018a0c>)]
>>> list(groupby("aabbb"))[0]
('a', <itertools._grouper object at 0xb71d3e4c>)
>>> list(list(groupby("aabbb"))[0][1])
[]

--
To email me, substitute nowhere->runbox, invalid->com.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor