-1
Pythonの-3の整数のリストにバイト以下の配列を変換するニシキヘビの方法は何ですか:Python-3でバイトを整数のリストに分割する方法は?
Input: b'34\n44\n-28\n-63\n22\n'
Desired output: [34, 44, -28, -63, 22]
Pythonの-3の整数のリストにバイト以下の配列を変換するニシキヘビの方法は何ですか:Python-3でバイトを整数のリストに分割する方法は?
Input: b'34\n44\n-28\n-63\n22\n'
Desired output: [34, 44, -28, -63, 22]
通常の文字列のと同じ方法を使用します。 int()
への分割およびマップ:bytes
はbytes.split()
含む同じメソッド(の多く持っているので
[int(v) for v in bytesvalue.split()]
これは動作し、int()
typeはそれがstr
値受け入れbytes
値と同じ方法を受け入れる:
If x is not a number or if base is given, then x must be a string,
bytes
, orbytearray
instance representing an integer literal in radix base.
デモ:
>>> bytesvalue = b'34\n44\n-28\n-63\n22\n'
>>> bytesvalue.split()
[b'34', b'44', b'-28', b'-63', b'22']
>>> [int(v) for v in bytesvalue.split()]
[34, 44, -28, -63, 22]
'[input.decodeにおけるxのINT(X)()。スプリット( '\ nの')]'? – pstatix
あなたは空白に分かれていますか? –
代わりに 'list(map(int、input.decode()。split( '\ n')))'、リストの理解度はやや速くなければならず、IMHOは_pythonic_です。 – pstatix