Konvertiere eine Zahl in eine Liste von ganzen Zahlen Wie Schreibe ich die magic Funktion unten? >>> num = 123 >>> lst = magic(num) >>> >>> print lst, type(lst) [1, 2, 3], <type 'list'> InformationsquelleAutor der Frage user94774 | 2009-04-23 integerlistpython 13 a = 123456 b = str(a) c = [] for digit in b: c.append (int(digit)) print c InformationsquelleAutor der Antwort RedBlueThing 66 Meinst du? num = 1234 lst = [int(i) for i in str(num)] InformationsquelleAutor der Antwort Bjorn Tipling 14 Könnte man dies tun: >>> num = 123 >>> lst = map(int, str(num)) >>> lst, type(lst) ([1, 2, 3], <type 'list'>) InformationsquelleAutor der Antwort John Fouhy 8 magic = lambda num: map(int, str(num)) nur dann nicht magic(12345) oder magic(someInt) #or whatever InformationsquelleAutor der Antwort Alex 6 >>> from collections import deque >>> def magic(num): digits = deque() while True: num,r = divmod(num,10) digits.appendleft(r) if num == 0: break return list(digits) >>> magic(123) [1, 2, 3] Laut meinen timings, diese Lösung ist deutlich schneller als die string-Methode (magic2), auch für kleinere Beispiele. >>> def magic2(num): return [int(i) for i in str(num)] Timings: magic >>> timeit.timeit(setup='from __main__ import magic', stmt='magic(123)') 1.3874572762508706 >>> timeit.timeit(setup='from __main__ import magic', stmt='magic(999999999)') 3.2624468999981673 magic2 >>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(123)') 3.693756106896217 >>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(999999999)') 10.485281719412114 InformationsquelleAutor der Antwort jamylak 5 Glaube nicht, das Wort Liste als Variablen-Namen! Es ist ein name, der in python eingebauten Datentyp. Auch, bitte klären Sie Ihre Frage. Wenn Sie auf der Suche nach einem Weg, um erstellen Sie eine ein-Mann-Liste, führen Sie die folgenden: a = 123 my_list = [a] und "pythonizing" Cannonade ' s Antwort: a = 123 my_list = [int(d) for d in str(a)] InformationsquelleAutor der Antwort Boris Gorelik 2 num = map(int, list(str(num))) InformationsquelleAutor der Antwort wilbeibi 1 Wenn es heißt als Magie, warum man nicht einfach magic: def magic(x): if x < 10: return [x] else: return magic(x//10) + [x%10] InformationsquelleAutor der Antwort englealuze 0 Verwenden Sie einfach : a= str (num) lst = list(a) InformationsquelleAutor der Antwort MrDHat 0 Können Sie versuchen, diese: def convert_to_list(number): return list(map(lambda x: int(x), str(number))) convert_to_list(1245) InformationsquelleAutor der Antwort Anthony Schreibe einen Kommentar Antworten abbrechenDu musst angemeldet sein, um einen Kommentar abzugeben.
InformationsquelleAutor der Antwort RedBlueThing
Meinst du?
InformationsquelleAutor der Antwort Bjorn Tipling
Könnte man dies tun:
InformationsquelleAutor der Antwort John Fouhy
nur dann nicht
oder
InformationsquelleAutor der Antwort Alex
Laut meinen timings, diese Lösung ist deutlich schneller als die string-Methode (
magic2
), auch für kleinere Beispiele.Timings:
magic
magic2
InformationsquelleAutor der Antwort jamylak
Glaube nicht, das Wort Liste als Variablen-Namen! Es ist ein name, der in python eingebauten Datentyp.
Auch, bitte klären Sie Ihre Frage. Wenn Sie auf der Suche nach einem Weg, um erstellen Sie eine ein-Mann-Liste, führen Sie die folgenden:
und "pythonizing" Cannonade ' s Antwort:
InformationsquelleAutor der Antwort Boris Gorelik
InformationsquelleAutor der Antwort wilbeibi
Wenn es heißt als Magie, warum man nicht einfach magic:
InformationsquelleAutor der Antwort englealuze
Verwenden Sie einfach :
InformationsquelleAutor der Antwort MrDHat
Können Sie versuchen, diese:
InformationsquelleAutor der Antwort Anthony