norgsmen hat geschrieben: ↑Mittwoch 21. Juni 2023, 22:12
Beim zweiten Beispiel tritt dann die Fehlmeldung
TypeError: 'int' object is not iterable auf
Was sagt denn die Doku?
>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
>>> help(min)
Help on built-in function min in module builtins:
min(...)
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
"sum" will also ein "iterable" fressen, bekommt aber von "min" nur ein "value", in diesem Falle ein "int".
Warum funktioniert das erste Beispiel?
Code: Alles auswählen
>>> test=[1,1,4,5]
>>> param=(min(test.count(a), test.count(a)) for a in set(test))
>>> type(param)
<class 'generator'>
Es wird also ein "iterable" an "sum" übergeben.