Python list of lists to bytes

I have list of list float and I want to convert it into bytes. Can please some help me to do this. for example

l = [[0.1, 1.0, 2.0], [2.0, 3.1, 4.1]]

and I want something like

bytes(l) -> b'\x01\x02\x03.......'

asked Apr 21, 2019 at 15:20

Python list of lists to bytes

Ravi ShankarRavi Shankar

1,6334 gold badges13 silver badges15 bronze badges

2

Since you've tagged this numpy, this is simply tobytes

a = np.array(l)
a.tobytes()
b'\x9a\x99\x99\x99\x99\x99\xb9?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00@\xcd\xcc\xcc\xcc\xcc\xcc\x08@ffffff\x10@'

This result can be re-processed as an ndarray using frombuffer, but the original shape will not be maintained.

answered Apr 21, 2019 at 16:03

Python list of lists to bytes

Hopefully this is what you wanted:

import struct

input = [[0.1, 1.0, 2.0], [2.0, 3.1, 4.1]]
outs = list()
string = ""

for i in input:
    for j in i:
        outs.append(bytes(struct.pack("f", j)))

for i in outs:
    string += str(i)
    print(i)

print('Bytes:', string)

answered Apr 21, 2019 at 15:39

mlotzmlotz

1323 bronze badges

Last update on August 19 2022 21:50:46 (UTC/GMT +8 hours)

Python Basic: Exercise-118 with Solution

Write a Python program to create a bytearray from a list.

Sample Solution:-

Python Code:

print()
nums = [10, 20, 56, 35, 17, 99]
# Create bytearray from list of integers.
values = bytearray(nums)
for x in values: print(x)
print()

Sample Output:

10                                                                                                            
20                                                                                                            
56                                                                                                            
35                                                                                                            
17                                                                                                            
99

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to prove that two string variables of same value point same memory location.
Next: Write a Python program to round a floating-point number to specified number decimal places.

Questions : How to convert list of lists to bytes

2022-09-21T01:31:02+00:00 2022-09-21T01:31:02+00:00

713

I have list of list float and I want to anycodings_python-3.x convert it into bytes. Can please some help anycodings_python-3.x me to do this. for example

l = [[0.1, 1.0, 2.0], [2.0, 3.1, 4.1]]

and I want something like

bytes(l) -> b'\x01\x02\x03.......'

Total Answers 2

32

Answers 1 : of How to convert list of lists to bytes

Since you've tagged this numpy, this is anycodings_numpy simply tobytes

a = np.array(l)
a.tobytes()
b'\x9a\x99\x99\x99\x99\x99\xb9?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00@\xcd\xcc\xcc\xcc\xcc\xcc\x08@ffffff\x10@'

This result can be re-processed as an anycodings_numpy ndarray using frombuffer, but the anycodings_numpy original shape will not be maintained.

0

2022-09-21T01:31:02+00:00 2022-09-21T01:31:02+00:00Answer Link

mRahman

2

Answers 2 : of How to convert list of lists to bytes

Hopefully this is what you wanted:

import struct

input = [[0.1, 1.0, 2.0], [2.0, 3.1, 4.1]]
outs = list()
string = ""

for i in input:
    for j in i:
        outs.append(bytes(struct.pack("f", j)))

for i in outs:
    string += str(i)
    print(i)

print('Bytes:', string)

0

2022-09-21T01:31:02+00:00 2022-09-21T01:31:02+00:00Answer Link

joy