Warum funktioniert meine Funktion, die eine Ziffernzeichenfolge in eine Liste von Bytes umwandelt?Python

Python-Programme
Anonymous
 Warum funktioniert meine Funktion, die eine Ziffernzeichenfolge in eine Liste von Bytes umwandelt?

Post by Anonymous »

Ich habe eine einfache Funktion geschrieben, die eine Zeichenfolge konvertiert, die nur aus Ziffern besteht (

Code: Select all

'0123456789'
) zu einer Liste von Bytes. Insbesondere findet es die längsten möglichen Sequenzen von Ziffern, die bei der Konvertierung in eine Ganzzahl weniger als 256 beträgt.

Code: Select all

from typing import List

DIGITS = {str(i): i for i in range(10)}

def get_bytes(s: str) -> List[int]:
byte_list = []
chunk = 0
for c in f'{s}0':
if (d := DIGITS.get(c)) is None:
raise ValueError(f"{c} is not a digit")

if (new := chunk * 10 + d) < 256:
chunk = new
else:
byte_list.append(chunk)
chunk = d

return byte_list[not byte_list[0]:]
< /code>
In [143]: get_bytes('0000')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[143], line 1
----> 1 get_bytes('0000')

Cell In[142], line 14, in get_bytes(s)
11         byte_list.append(chunk)
12         chunk = d
---> 14 return byte_list[not byte_list[0]:]

IndexError: list index out of range

In [144]: get_bytes('100100100')
Out[144]: [100, 100, 100]

In [145]: get_bytes('12301230123')
Out[145]: [123, 123, 123]
< /code>
I tried to fix the code, I couldn't. Now there is only one problem remaining, and the exact problem I tried to address with this question.
In the first example, expected output is [0, 0, 0, 0]
im dritten Beispiel sollte die korrekte Ausgabe [123, 0, 123, 0, 123] sein. Wie kann ich das beheben? Kurz gesagt, ich möchte sicherstellen, dass '' .Join (Str (i) für i in Ausgabe) == Original < /code>. < /P>

Nach meinen Tests ist der Code aus der akzeptierten Antwort besser als der auf Regex basierende Ansatz, sodass ich: < /p>
besser als der auf Regex basierende Ansatz ausgeführt habe: < /p>

Code: Select all

In [384]: get_bytes1(string) == get_bytes2(string)
Out[384]: True

In [385]: %timeit get_bytes1(string)
179 ms ± 588 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [386]: %timeit get_bytes2(string)
161 ms ± 748 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [387]: len(string)
Out[387]: 1000001
< /code>
get_bytes2
stammt aus der akzeptierten Antwort, Get_Bytes1 ist der auf Regex basierende Ansatz. Und String ist die ersten 1000000 -Dezimalstellen von π plus A '3' in der Vorderseite.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post