How to create a list of a list of bits?
up vote
0
down vote
favorite
Say I have a list of strings. For example, this is a list of two strings.
["Hello, W", "orld!---"]
.
I already have a function char2bin
that converts separate string characters characters into a list of binary. Note, this does not work for multiple character strings.
Ex.
>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)
How would I convert a list of multiple strings into a list of a list of binaries?
For example, ["Hello, W", "orld!---"]
would result in
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0,1,1,0,1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0,1,1,0,1,1,1,1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0,1,1,1,0,1,1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0,1,1,1,0,0,1,0], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0,0,1,0,0,0,0,1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1,1,1,1,0], [0, 1, 1, 1,1, 1, 1, 0]]]
As you can see, each string character has converted into a list of binaries, but since there are two separate strings, there are also two separate lists of binary lists.
python
add a comment |
up vote
0
down vote
favorite
Say I have a list of strings. For example, this is a list of two strings.
["Hello, W", "orld!---"]
.
I already have a function char2bin
that converts separate string characters characters into a list of binary. Note, this does not work for multiple character strings.
Ex.
>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)
How would I convert a list of multiple strings into a list of a list of binaries?
For example, ["Hello, W", "orld!---"]
would result in
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0,1,1,0,1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0,1,1,0,1,1,1,1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0,1,1,1,0,1,1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0,1,1,1,0,0,1,0], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0,0,1,0,0,0,0,1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1,1,1,1,0], [0, 1, 1, 1,1, 1, 1, 0]]]
As you can see, each string character has converted into a list of binaries, but since there are two separate strings, there are also two separate lists of binary lists.
python
2
Please share the code you're working with. It will help us understand exactly what's happening
– Hampus Larsson
Nov 10 at 19:51
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Say I have a list of strings. For example, this is a list of two strings.
["Hello, W", "orld!---"]
.
I already have a function char2bin
that converts separate string characters characters into a list of binary. Note, this does not work for multiple character strings.
Ex.
>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)
How would I convert a list of multiple strings into a list of a list of binaries?
For example, ["Hello, W", "orld!---"]
would result in
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0,1,1,0,1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0,1,1,0,1,1,1,1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0,1,1,1,0,1,1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0,1,1,1,0,0,1,0], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0,0,1,0,0,0,0,1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1,1,1,1,0], [0, 1, 1, 1,1, 1, 1, 0]]]
As you can see, each string character has converted into a list of binaries, but since there are two separate strings, there are also two separate lists of binary lists.
python
Say I have a list of strings. For example, this is a list of two strings.
["Hello, W", "orld!---"]
.
I already have a function char2bin
that converts separate string characters characters into a list of binary. Note, this does not work for multiple character strings.
Ex.
>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)
How would I convert a list of multiple strings into a list of a list of binaries?
For example, ["Hello, W", "orld!---"]
would result in
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0,1,1,0,1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0,1,1,0,1,1,1,1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0,1,1,1,0,1,1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0,1,1,1,0,0,1,0], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0,0,1,0,0,0,0,1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1,1,1,1,0], [0, 1, 1, 1,1, 1, 1, 0]]]
As you can see, each string character has converted into a list of binaries, but since there are two separate strings, there are also two separate lists of binary lists.
python
python
asked Nov 10 at 19:48
hello
314
314
2
Please share the code you're working with. It will help us understand exactly what's happening
– Hampus Larsson
Nov 10 at 19:51
add a comment |
2
Please share the code you're working with. It will help us understand exactly what's happening
– Hampus Larsson
Nov 10 at 19:51
2
2
Please share the code you're working with. It will help us understand exactly what's happening
– Hampus Larsson
Nov 10 at 19:51
Please share the code you're working with. It will help us understand exactly what's happening
– Hampus Larsson
Nov 10 at 19:51
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
you can iterate over the characters in a string, and iterate over the words in a word_list. So you can do the following :
def convert_word(a):
return [char2bin(x) for x in a]
result = [convert_word(word) for word in word_list]
using my own interpretation of char2bin:
def char2bin(x):
return list(bin(ord(x)))[2:]
def convert_word(a):
return [char2bin(x) for x in a]
word_list = ['abc','bcd']
[convert_word(word) for word in word_list]
gives the result:
[[['1', '1', '0', '0', '0', '0', '1'],
['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1']],
[['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1'],
['1', '1', '0', '0', '1', '0', '0']]]
which i believe is the format you are looking for.
add a comment |
up vote
0
down vote
There are three basic approaches here, comprehension, functional, and procedural. The comprehension form is considered idiomatic Python (or "pythonic").
Comprehension
List comprehensions can build the list elements from any Python expression, including other list comprehensions--you can nest them.
[[char2bin(c) for c in word] for word in ["Hello, W", "orld!---"]]
The Python comprehension syntax has equivalents of map()
loops and element filter()
s without requiring an explicit lambda like those builtin functions would. We don't need filters for this task. But because you already have a function char2bin()
declared, you can already use map()
without the lambda, like so.
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
The above version mixes a little functional style into the comprehension, and is probably the most Pythonic.
In Python 2 you wouldn't need to put it in the [*]
part to make it a list, but in Python 3 map()
makes a lazy generator instead of a list.
Functional
You can always rewrite comprehensions in terms of maps and filters by using lambda (anonymous functions). You usually don't use a lambda in Python when a comprehension would do.
[*map(lambda word: [*map(char2bin, word)], ["Hello, W", "orld!---"])]
And the newer unpack-into-list syntax [*foo]
can still be done with the list
constructor.
list(map(lambda word: list(map(char2bin, word), ["Hello, W", "orld!---"]))
The above is a more functional style, although it could be taken a bit further to point-free style with a functional library using currying and function composition. But even pure-functional languages like Haskell have comprehensions, which is where Python got them from in the first place.
Procedural
List comprehensions can always be converted to a procedural-style by using explicit for loops and accumulators (and if-statements when there are filters). This is much more verbose, but would be more familiar to those coming from a procedural language like C, so you still see it pretty often in Python.
result =
for word in ["Hello, W", "orld!---"]:
binchars =
for c in word:
binchars.append(char2bin(c))
result.append(binchars)
Compare the above to my recommendation:
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
1
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:20
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
you can iterate over the characters in a string, and iterate over the words in a word_list. So you can do the following :
def convert_word(a):
return [char2bin(x) for x in a]
result = [convert_word(word) for word in word_list]
using my own interpretation of char2bin:
def char2bin(x):
return list(bin(ord(x)))[2:]
def convert_word(a):
return [char2bin(x) for x in a]
word_list = ['abc','bcd']
[convert_word(word) for word in word_list]
gives the result:
[[['1', '1', '0', '0', '0', '0', '1'],
['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1']],
[['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1'],
['1', '1', '0', '0', '1', '0', '0']]]
which i believe is the format you are looking for.
add a comment |
up vote
0
down vote
you can iterate over the characters in a string, and iterate over the words in a word_list. So you can do the following :
def convert_word(a):
return [char2bin(x) for x in a]
result = [convert_word(word) for word in word_list]
using my own interpretation of char2bin:
def char2bin(x):
return list(bin(ord(x)))[2:]
def convert_word(a):
return [char2bin(x) for x in a]
word_list = ['abc','bcd']
[convert_word(word) for word in word_list]
gives the result:
[[['1', '1', '0', '0', '0', '0', '1'],
['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1']],
[['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1'],
['1', '1', '0', '0', '1', '0', '0']]]
which i believe is the format you are looking for.
add a comment |
up vote
0
down vote
up vote
0
down vote
you can iterate over the characters in a string, and iterate over the words in a word_list. So you can do the following :
def convert_word(a):
return [char2bin(x) for x in a]
result = [convert_word(word) for word in word_list]
using my own interpretation of char2bin:
def char2bin(x):
return list(bin(ord(x)))[2:]
def convert_word(a):
return [char2bin(x) for x in a]
word_list = ['abc','bcd']
[convert_word(word) for word in word_list]
gives the result:
[[['1', '1', '0', '0', '0', '0', '1'],
['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1']],
[['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1'],
['1', '1', '0', '0', '1', '0', '0']]]
which i believe is the format you are looking for.
you can iterate over the characters in a string, and iterate over the words in a word_list. So you can do the following :
def convert_word(a):
return [char2bin(x) for x in a]
result = [convert_word(word) for word in word_list]
using my own interpretation of char2bin:
def char2bin(x):
return list(bin(ord(x)))[2:]
def convert_word(a):
return [char2bin(x) for x in a]
word_list = ['abc','bcd']
[convert_word(word) for word in word_list]
gives the result:
[[['1', '1', '0', '0', '0', '0', '1'],
['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1']],
[['1', '1', '0', '0', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1'],
['1', '1', '0', '0', '1', '0', '0']]]
which i believe is the format you are looking for.
answered Nov 10 at 19:53
Christian Sloper
1,036213
1,036213
add a comment |
add a comment |
up vote
0
down vote
There are three basic approaches here, comprehension, functional, and procedural. The comprehension form is considered idiomatic Python (or "pythonic").
Comprehension
List comprehensions can build the list elements from any Python expression, including other list comprehensions--you can nest them.
[[char2bin(c) for c in word] for word in ["Hello, W", "orld!---"]]
The Python comprehension syntax has equivalents of map()
loops and element filter()
s without requiring an explicit lambda like those builtin functions would. We don't need filters for this task. But because you already have a function char2bin()
declared, you can already use map()
without the lambda, like so.
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
The above version mixes a little functional style into the comprehension, and is probably the most Pythonic.
In Python 2 you wouldn't need to put it in the [*]
part to make it a list, but in Python 3 map()
makes a lazy generator instead of a list.
Functional
You can always rewrite comprehensions in terms of maps and filters by using lambda (anonymous functions). You usually don't use a lambda in Python when a comprehension would do.
[*map(lambda word: [*map(char2bin, word)], ["Hello, W", "orld!---"])]
And the newer unpack-into-list syntax [*foo]
can still be done with the list
constructor.
list(map(lambda word: list(map(char2bin, word), ["Hello, W", "orld!---"]))
The above is a more functional style, although it could be taken a bit further to point-free style with a functional library using currying and function composition. But even pure-functional languages like Haskell have comprehensions, which is where Python got them from in the first place.
Procedural
List comprehensions can always be converted to a procedural-style by using explicit for loops and accumulators (and if-statements when there are filters). This is much more verbose, but would be more familiar to those coming from a procedural language like C, so you still see it pretty often in Python.
result =
for word in ["Hello, W", "orld!---"]:
binchars =
for c in word:
binchars.append(char2bin(c))
result.append(binchars)
Compare the above to my recommendation:
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
1
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:20
add a comment |
up vote
0
down vote
There are three basic approaches here, comprehension, functional, and procedural. The comprehension form is considered idiomatic Python (or "pythonic").
Comprehension
List comprehensions can build the list elements from any Python expression, including other list comprehensions--you can nest them.
[[char2bin(c) for c in word] for word in ["Hello, W", "orld!---"]]
The Python comprehension syntax has equivalents of map()
loops and element filter()
s without requiring an explicit lambda like those builtin functions would. We don't need filters for this task. But because you already have a function char2bin()
declared, you can already use map()
without the lambda, like so.
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
The above version mixes a little functional style into the comprehension, and is probably the most Pythonic.
In Python 2 you wouldn't need to put it in the [*]
part to make it a list, but in Python 3 map()
makes a lazy generator instead of a list.
Functional
You can always rewrite comprehensions in terms of maps and filters by using lambda (anonymous functions). You usually don't use a lambda in Python when a comprehension would do.
[*map(lambda word: [*map(char2bin, word)], ["Hello, W", "orld!---"])]
And the newer unpack-into-list syntax [*foo]
can still be done with the list
constructor.
list(map(lambda word: list(map(char2bin, word), ["Hello, W", "orld!---"]))
The above is a more functional style, although it could be taken a bit further to point-free style with a functional library using currying and function composition. But even pure-functional languages like Haskell have comprehensions, which is where Python got them from in the first place.
Procedural
List comprehensions can always be converted to a procedural-style by using explicit for loops and accumulators (and if-statements when there are filters). This is much more verbose, but would be more familiar to those coming from a procedural language like C, so you still see it pretty often in Python.
result =
for word in ["Hello, W", "orld!---"]:
binchars =
for c in word:
binchars.append(char2bin(c))
result.append(binchars)
Compare the above to my recommendation:
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
1
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:20
add a comment |
up vote
0
down vote
up vote
0
down vote
There are three basic approaches here, comprehension, functional, and procedural. The comprehension form is considered idiomatic Python (or "pythonic").
Comprehension
List comprehensions can build the list elements from any Python expression, including other list comprehensions--you can nest them.
[[char2bin(c) for c in word] for word in ["Hello, W", "orld!---"]]
The Python comprehension syntax has equivalents of map()
loops and element filter()
s without requiring an explicit lambda like those builtin functions would. We don't need filters for this task. But because you already have a function char2bin()
declared, you can already use map()
without the lambda, like so.
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
The above version mixes a little functional style into the comprehension, and is probably the most Pythonic.
In Python 2 you wouldn't need to put it in the [*]
part to make it a list, but in Python 3 map()
makes a lazy generator instead of a list.
Functional
You can always rewrite comprehensions in terms of maps and filters by using lambda (anonymous functions). You usually don't use a lambda in Python when a comprehension would do.
[*map(lambda word: [*map(char2bin, word)], ["Hello, W", "orld!---"])]
And the newer unpack-into-list syntax [*foo]
can still be done with the list
constructor.
list(map(lambda word: list(map(char2bin, word), ["Hello, W", "orld!---"]))
The above is a more functional style, although it could be taken a bit further to point-free style with a functional library using currying and function composition. But even pure-functional languages like Haskell have comprehensions, which is where Python got them from in the first place.
Procedural
List comprehensions can always be converted to a procedural-style by using explicit for loops and accumulators (and if-statements when there are filters). This is much more verbose, but would be more familiar to those coming from a procedural language like C, so you still see it pretty often in Python.
result =
for word in ["Hello, W", "orld!---"]:
binchars =
for c in word:
binchars.append(char2bin(c))
result.append(binchars)
Compare the above to my recommendation:
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
There are three basic approaches here, comprehension, functional, and procedural. The comprehension form is considered idiomatic Python (or "pythonic").
Comprehension
List comprehensions can build the list elements from any Python expression, including other list comprehensions--you can nest them.
[[char2bin(c) for c in word] for word in ["Hello, W", "orld!---"]]
The Python comprehension syntax has equivalents of map()
loops and element filter()
s without requiring an explicit lambda like those builtin functions would. We don't need filters for this task. But because you already have a function char2bin()
declared, you can already use map()
without the lambda, like so.
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
The above version mixes a little functional style into the comprehension, and is probably the most Pythonic.
In Python 2 you wouldn't need to put it in the [*]
part to make it a list, but in Python 3 map()
makes a lazy generator instead of a list.
Functional
You can always rewrite comprehensions in terms of maps and filters by using lambda (anonymous functions). You usually don't use a lambda in Python when a comprehension would do.
[*map(lambda word: [*map(char2bin, word)], ["Hello, W", "orld!---"])]
And the newer unpack-into-list syntax [*foo]
can still be done with the list
constructor.
list(map(lambda word: list(map(char2bin, word), ["Hello, W", "orld!---"]))
The above is a more functional style, although it could be taken a bit further to point-free style with a functional library using currying and function composition. But even pure-functional languages like Haskell have comprehensions, which is where Python got them from in the first place.
Procedural
List comprehensions can always be converted to a procedural-style by using explicit for loops and accumulators (and if-statements when there are filters). This is much more verbose, but would be more familiar to those coming from a procedural language like C, so you still see it pretty often in Python.
result =
for word in ["Hello, W", "orld!---"]:
binchars =
for c in word:
binchars.append(char2bin(c))
result.append(binchars)
Compare the above to my recommendation:
[[*map(char2bin, word)] for word in ["Hello, W", "orld!---"]]
edited Nov 11 at 20:00
answered Nov 10 at 19:56
gilch
2,408514
2,408514
1
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:20
add a comment |
1
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:20
1
1
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:20
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:20
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242792%2fhow-to-create-a-list-of-a-list-of-bits%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Please share the code you're working with. It will help us understand exactly what's happening
– Hampus Larsson
Nov 10 at 19:51