Code: Alles auswählen
for key, value in sorted(dictionary.items()):
print key, value
Das ``and`` statt ``&&`` finde ich etwas lesbarer, weil das Wort `and` eine bekannte Bedeutung hat, während ``&&`` nicht ganz so selbsterklärend ist.
Code: Alles auswählen
for key, value in sorted(dictionary.items()):
print key, value
Code: Alles auswählen
while len(array) > 0:
biggest = array.keys()[0]
for k in array:
if array[k] > array[biggest]:
biggest = k
print "Letter: %s" %biggest, "- Anz: %i" %array[biggest], "%"
del array[biggest]
Code: Alles auswählen
l = array.items()
l.sort()
for buchstabe, anzahl in l:
print "Letter",buchstabe,"Anzahl",anzahl
# oder print "Letter %s - Anzahl %i" % (buchstabe,anzahl)
Code: Alles auswählen
l.sort(lambda a,b:cmp(a[1],b[1]))
Code: Alles auswählen
from operator import itemgetter
histogram = {'a': 10, 'b': 5, 'c': 100}
for item in sorted(histogram.items(), key=itemgetter(1), reverse=True):
print 'Letter %r - count: %d' % item
Code: Alles auswählen
funk = lambda a, b: a*2 + b
Code: Alles auswählen
def funk(a, b):
return a*2 + b
[/quote]pydoc lambda hat geschrieben: Lambda forms (lambda expressions) have the same syntactic position as expressions. They are a shorthand to create anonymous functions; the expression lambda arguments: expression yields a function object.
Code: Alles auswählen
def meineFunktion( x, y ):
def funk(a, b):
return a*2 + b
print funk( x, y )
Ich normalerweise auch, weil es an sich besser lesbar ist,jens hat geschrieben: Also in dem Falle würde ich mir einfach eine Funktion in einer Funktion machen
Code: Alles auswählen
In [20]: a = [('b', 200), ('z', 42), ('a', 100), ('b', 10)]
In [21]: sorted(a)
Out[21]: [('a', 100), ('b', 10), ('b', 200), ('z', 42)]
In [22]: sorted(a, lambda x, y: cmp(x[1], y[1]))
Out[22]: [('b', 10), ('z', 42), ('a', 100), ('b', 200)]
In [23]: import operator
In [24]: sorted(a, key=operator.itemgetter(1))
Out[24]: [('b', 10), ('z', 42), ('a', 100), ('b', 200)]