Hallo Hartmut,

Übersetzen Sie meine Erklärung kann Ihnen helfen.

itertools.groupby erstellt einen iterierbaren Wert, der aufeinanderfolgende 
Schlüssel und Gruppen aus einem iterablen Wert zurückgibt.

Lassen Sie uns zunächst eine etwas andere Version Ihres groupby-Codes 
betrachten:

for k, g in itertools.groupby(vector, key=lambda x: x[0]):

In diesem Fall ist Vektor eine Liste von Tupeln:

vector = [('metall', 'eisen'), ('metall', 'silber'), ('gas', 'sauerstoff'), 
('gas', 'argon'), ('gas', 'helium')]

Die lambda-funktion, lambda x: x [0], bestimmt, zu welchem ​​Schlüssel 
unsere Daten gehören. Stellen Sie sich vor, dass lambda nur eine Abkürzung 
ist, eine einzeilige Funktionsdefinition. In diesem Fall, wenn die 
lamdba-funktion auf einem 'wert' ausgeführt wird, gibt sie die Daten an der 
ersten Indexposition von 'wert' zurück (ich habe x verwendet, Sie können r, 
z, hund oder was auch immer Sie wollen verwenden) keine echte Bedeutung 
außerhalb der lambda-funktionsdefinition). Wenn 'wert' eine Liste oder ein 
Tupel wäre, würde die lambda-funktion das erste Datenelement in der Liste 
oder dem Tupel zurückgeben. In unserem Beispiel werden die Daten basierend 
auf dem ersten Element jedes Tupels gruppiert, also 'metal' und 'gas'.

Das erste Mal durch die for-Schleife wird k 'metal' und g wird ein 
iterabler sein, der über jedes der Datenelemente (Tupel) in vektor 
iteriert, für das das erste element 'metal' ist, so wie du über g iterierst 
du wirst dann ("metall", "eisen") bekommen ("metall", "silber"). Beim 
zweiten Mal durch die for-Schleife wird k 'gas' und g wird über ('gas', 
'sauerstoff'), ('gas', 'argon') und ('gas', 'helium') iterieren. Fügen Sie 
also mehr Code zusammen, um die grouby-Ergebnisse zu verwenden, zum 
Beispiel:

import itertools
vector = [('metall', 'eisen'), ('metall', 'silber'), ('gas', 'sauerstoff'), 
('gas', 'argon'), ('gas', 'helium')]
for k, g in itertools.groupby(vector, key=lambda x: x[0]):
    print "group %s" % k
    for item in g:
        "%s is a %s" % (item[1], k)

würde folgende Ausgabe geben:

group metall
'eisen is a metall'
'silber is a metall'
group gas
'sauerstoff is a gas'
'argon is a gas'
'helium is a gas'

Normalerweise ist das iterable, das Sie an itertools.groupby übergeben, 
mehrdimensional und wird normalerweise nach dem Element sortiert, das Sie 
in Ihrer Lambda-Funktion verwenden. 

Wenn du das Formular nimmst, das ich benutzt habe:

for k, g in itertools.groupby(_rain_vector):

und _rain_vector war nur eine Liste der täglichen Niederschlagsmengen:

_rainfall_vector = [2.4, 1.2, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0]

In diesem Fall habe ich den Parameter 'key' nicht benutzt. Wenn Sie den 
Parameter 'key' nicht angeben, entspricht dies der Verwendung einer 
Lambda-Funktion Lambda x: x, dh die Funktion gibt den Parameter zurück, den 
sie übergeben hat. In meinem Fall ist also das erste Mal durch die 
for-Anweisung k 2.4 und g würde iterieren über 2.4, das zweite Mal durch 
die for-Anweisung k ist 1.2 und g würde über 2.2 iterieren, das dritte Mal 
durch die for-Anweisung k ist 0.0 und g würde iterieren über 0.0, 0.0 und 
0.0, das vierte Mal durch die for Die Aussage k ist 0,2 und g würde über 
0,2 iterieren. Die letzte Zeit durch die for-Anweisung k ist 0.0 und g 
würde über 0.0 und 0.0 iterieren. Dies scheint wenig nützlich zu sein, aber 
ich benutzte die groupby-Anweisung nicht, um eine komplexe Verarbeitung 
eines Datenvektors durchzuführen, sondern benutzte sie nur, um den längsten 
konsekutiven Lauf von Tagen ohne Regen zu bestimmen. Ich habe dies getan, 
indem ich die maximale Länge von g gefunden habe, wenn k 0.0 ist.

Ich hoffe, das hilft dir.


Since you have found this in my code I feel should help if I can. Perhaps 
if I avoid idioms that might confuse Google Translate my explanation may 
help you.

itertools.groupby makes an iterable that returns consecutive keys and 
groups from an iterable.

First let's consider a slightly different version of your groupby code:

for k, g in itertools.groupby(vector, key=lambda x: x[0]):

In this case vector is a list of tuples:

vector = [('metal', 'iron'), ('metal', 'silver'), ('gas', 'oxygen'), ('gas', 
'argon'), ('gas', 'helium')]

The lambda function, lambda x: x[0], determines what key our data is 
grouped on. Think of lambda as just being an shorthand, single line 
function definition. In this case when the lamdba function is executed on 
some 'value', it will return the data in the first index position of 
'value' (I have used x, you can use r, z, dog or whatever you want, it has 
no real meaning outside of the lambda function definition). If 'value' was 
a list or a tuple the lambda function would return the first data element 
in the list or tuple. So in our example the data will be grouped based on 
the first element of each tuple, that is 'metal' and 'gas'.

The first time through the for loop, k will be 'metal' and g will be an 
iterable that will iterate over each of the data elements (tuples) in 
vector for which the first element is 'metal', so as you iterate over g you 
will get ('metal', 'iron') then ('metal', 'silver'). The second time 
through the for loop,  k will be 'gas' and g will iterate over ('gas', 
'oxygen'), ('gas', 'argon') and ('gas', 'helium'). So putting some more 
code together to use the grouby results, for example:

import itertools
vector = [('metal', 'iron'), ('metal', 'silver'), ('gas', 'oxygen'), ('gas', 
'argon'), ('gas', 'helium')]
for k, g in itertools.groupby(vector, key=lambda x: x[0]):
    print "group %s" % k
    for item in g:
        "%s is a %s" % (item[1], k)

would give the following output:

group metal
'iron is a metal'
'silver is a metal'
group gas
'oxygen is a gas'
'argon is a gas'
'helium is a gas'

Normally the iterable you are passing to itertools.groupby is 
multi-dimensional and it is usually sorted on the element you use in your 
lambda function.

If you take the form that I used:

for k, g in itertools.groupby(_rain_vector):

and _rain_vector was just a list of daily rainfall totals:

_rainfall_vector = [2.4, 1.2, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0]

In this case I was are not using the the 'key' parameter. When you don't 
specify the 'key' parameter it is equivalent to using a lambda function 
lambda x: x, that is the function returns the parameter it was passed, so 
in my case the first time through the for statement k is 2.4 and g would 
iterate over 2.4, the second time through the for statement k is 1.2 and g 
would iterate over 2.2, the third time through the for statement k is 0.0 
and g would iterate over 0.0, 0.0 and 0.0, the fourth time through the for 
statement k is 0.2 and g would iterate over 0.2. The final time through the 
for statement k is 0.0 and g would iterate over 0.0 and 0.0. This might 
seem to be of little use but I was not using the groupby statement to do 
any complex processing of a vector of data, rather I was just using it to 
determine the longest consecutive run of days with no rain. I did this by 
finding the maximum length of g when k is 0.0.

I hope this is of some help to you.


Gary


On Sunday, 4 March 2018 18:15:36 UTC+10, Hartmut Schweidler wrote:
>
> Hallo Gary,
>
> genau solche Ideen kommen mir wenn ich Programme lese, jedoch fehlt mir 
> mangels umfangreicher Englisch Kenntnisse der Überblick.
>
> Durch Versuch und Irrtum komme ich jedoch oft zum Erfolg. Beispielsweise 
> suche ich nach einer Deutschen Anleitung für "for k,g in 
> *itertools.groupby*(_rain_vector, key=lambda r:1 if r > 0 else 0):" damit 
> ich den Sinn verstehen kann.
>
> Danke 
> Hartmut
>
>

Reply via email to