Creating a list of bits?











up vote
2
down vote

favorite
1












Currently, I have two functions: char2bin and segmentString.



segmentString takes a string and a fill character and returns lists of 8 character strings. For example, if there is a 13 character string, it splits it into a list of two strings where the second string has 3 fill characters to make it a complete 8.



>>>segmentString("Hello, World!", "-")
['Hello, W', 'orld!---']


char2bin takes individual string characters (single character) and turns them into a list of 8 bits. It does not work for multiple character strings. For example,



>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)


I need to create a function (in this example, let's call it framer) that takes the result from segmentString and convert it into a list of bits, where each list of bits are contained in a separate list within a list.



For example, from the segmentString function, this would create a list of two strings. Each letter of each separate string is converted into a list of bits, and each list of bits is contained as a list for each string.



>>>F=framer("Hello, World!", "-")
>>>F
[[[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, there is one general list that contains two lists that contain 8 lists of bits, which were converted from a string character by char2bin.
How would I do this?










share|improve this question









New contributor




hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I would use list comprehension
    – YoYoYonnY
    Nov 10 at 14:51






  • 1




    something like framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
    – YoYoYonnY
    Nov 10 at 15:07

















up vote
2
down vote

favorite
1












Currently, I have two functions: char2bin and segmentString.



segmentString takes a string and a fill character and returns lists of 8 character strings. For example, if there is a 13 character string, it splits it into a list of two strings where the second string has 3 fill characters to make it a complete 8.



>>>segmentString("Hello, World!", "-")
['Hello, W', 'orld!---']


char2bin takes individual string characters (single character) and turns them into a list of 8 bits. It does not work for multiple character strings. For example,



>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)


I need to create a function (in this example, let's call it framer) that takes the result from segmentString and convert it into a list of bits, where each list of bits are contained in a separate list within a list.



For example, from the segmentString function, this would create a list of two strings. Each letter of each separate string is converted into a list of bits, and each list of bits is contained as a list for each string.



>>>F=framer("Hello, World!", "-")
>>>F
[[[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, there is one general list that contains two lists that contain 8 lists of bits, which were converted from a string character by char2bin.
How would I do this?










share|improve this question









New contributor




hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I would use list comprehension
    – YoYoYonnY
    Nov 10 at 14:51






  • 1




    something like framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
    – YoYoYonnY
    Nov 10 at 15:07















up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





Currently, I have two functions: char2bin and segmentString.



segmentString takes a string and a fill character and returns lists of 8 character strings. For example, if there is a 13 character string, it splits it into a list of two strings where the second string has 3 fill characters to make it a complete 8.



>>>segmentString("Hello, World!", "-")
['Hello, W', 'orld!---']


char2bin takes individual string characters (single character) and turns them into a list of 8 bits. It does not work for multiple character strings. For example,



>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)


I need to create a function (in this example, let's call it framer) that takes the result from segmentString and convert it into a list of bits, where each list of bits are contained in a separate list within a list.



For example, from the segmentString function, this would create a list of two strings. Each letter of each separate string is converted into a list of bits, and each list of bits is contained as a list for each string.



>>>F=framer("Hello, World!", "-")
>>>F
[[[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, there is one general list that contains two lists that contain 8 lists of bits, which were converted from a string character by char2bin.
How would I do this?










share|improve this question









New contributor




hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Currently, I have two functions: char2bin and segmentString.



segmentString takes a string and a fill character and returns lists of 8 character strings. For example, if there is a 13 character string, it splits it into a list of two strings where the second string has 3 fill characters to make it a complete 8.



>>>segmentString("Hello, World!", "-")
['Hello, W', 'orld!---']


char2bin takes individual string characters (single character) and turns them into a list of 8 bits. It does not work for multiple character strings. For example,



>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)


I need to create a function (in this example, let's call it framer) that takes the result from segmentString and convert it into a list of bits, where each list of bits are contained in a separate list within a list.



For example, from the segmentString function, this would create a list of two strings. Each letter of each separate string is converted into a list of bits, and each list of bits is contained as a list for each string.



>>>F=framer("Hello, World!", "-")
>>>F
[[[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, there is one general list that contains two lists that contain 8 lists of bits, which were converted from a string character by char2bin.
How would I do this?







python list bit






share|improve this question









New contributor




hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 10 at 14:51





















New contributor




hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 14:47









hello

314




314




New contributor




hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






hello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • I would use list comprehension
    – YoYoYonnY
    Nov 10 at 14:51






  • 1




    something like framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
    – YoYoYonnY
    Nov 10 at 15:07




















  • I would use list comprehension
    – YoYoYonnY
    Nov 10 at 14:51






  • 1




    something like framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
    – YoYoYonnY
    Nov 10 at 15:07


















I would use list comprehension
– YoYoYonnY
Nov 10 at 14:51




I would use list comprehension
– YoYoYonnY
Nov 10 at 14:51




1




1




something like framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
– YoYoYonnY
Nov 10 at 15:07






something like framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
– YoYoYonnY
Nov 10 at 15:07














3 Answers
3






active

oldest

votes

















up vote
1
down vote













You can use a list comprehension for this:



def char2bin(byte):
return list(map(int, format(byte, '08b')))

def segmentString(text, padding, chunksize):
for index in range(0, len(text), chunksize):
yield text[index:index + chunksize].ljust(chunksize, padding)

def framer(text, padding='-', chunksize=8, encoding='utf8'):
return [[char2bin(byte) for byte in segment] for segment in
segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]


This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.



>>> framer('Hello, World!')
[[[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, 0, 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, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1]]]


Non-ascii characters require multiple bits to encode.



>>> framer('💩', padding='x00')
[[[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]]





share|improve this answer






























    up vote
    0
    down vote













    You could either use list comprehensions or make use of the itertools module.



    You can learn more about list comprehensions here, and more about itertootls here.






    share|improve this answer




























      up vote
      0
      down vote













      You can use below code to achieve your goal.



      def segment_string(s, fill_by):
      l =
      while s:
      if len(s) < 8:
      s = s + (fill_by) * (8 - len(s))

      l.append(s[0:8])
      s = s[8:]

      return l # ['Hello, W', 'orld!---']

      def char2bin(ch):
      a = bin(ord(ch))[2:]
      l = [int(c) for c in a]

      if len(l) < 8:
      l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)

      return l # [0, 1, 0, 0, 1, 0, 0, 0]

      def framer(s, fill_by='-'):
      segments = segment_string(s, fill_by) # Calling segment_string()
      print(segments)

      arr =
      for segment in segments:
      arr2 =
      for ch in segment:
      arr3 = char2bin(ch); # Calling char2bin()
      arr2.append(arr3)

      arr.append(arr2)

      return arr # final list to be returned


      if __name__ == "__main__":
      f = framer('Hello, World!', '~')
      print(f)


      Output »

      [[[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, 0, 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]]]

      # >>> bin(126)
      # '0b1111110'
      # >>>
      # >>> chr(126)
      # '~'
      # >>>





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });






        hello is a new contributor. Be nice, and check out our Code of Conduct.










         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240083%2fcreating-a-list-of-bits%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        You can use a list comprehension for this:



        def char2bin(byte):
        return list(map(int, format(byte, '08b')))

        def segmentString(text, padding, chunksize):
        for index in range(0, len(text), chunksize):
        yield text[index:index + chunksize].ljust(chunksize, padding)

        def framer(text, padding='-', chunksize=8, encoding='utf8'):
        return [[char2bin(byte) for byte in segment] for segment in
        segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]


        This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.



        >>> framer('Hello, World!')
        [[[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, 0, 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, 0, 1, 0, 1, 1, 0, 1],
        [0, 0, 1, 0, 1, 1, 0, 1],
        [0, 0, 1, 0, 1, 1, 0, 1]]]


        Non-ascii characters require multiple bits to encode.



        >>> framer('💩', padding='x00')
        [[[1, 1, 1, 1, 0, 0, 0, 0],
        [1, 0, 0, 1, 1, 1, 1, 1],
        [1, 0, 0, 1, 0, 0, 1, 0],
        [1, 0, 1, 0, 1, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0]]]





        share|improve this answer



























          up vote
          1
          down vote













          You can use a list comprehension for this:



          def char2bin(byte):
          return list(map(int, format(byte, '08b')))

          def segmentString(text, padding, chunksize):
          for index in range(0, len(text), chunksize):
          yield text[index:index + chunksize].ljust(chunksize, padding)

          def framer(text, padding='-', chunksize=8, encoding='utf8'):
          return [[char2bin(byte) for byte in segment] for segment in
          segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]


          This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.



          >>> framer('Hello, World!')
          [[[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, 0, 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, 0, 1, 0, 1, 1, 0, 1],
          [0, 0, 1, 0, 1, 1, 0, 1],
          [0, 0, 1, 0, 1, 1, 0, 1]]]


          Non-ascii characters require multiple bits to encode.



          >>> framer('💩', padding='x00')
          [[[1, 1, 1, 1, 0, 0, 0, 0],
          [1, 0, 0, 1, 1, 1, 1, 1],
          [1, 0, 0, 1, 0, 0, 1, 0],
          [1, 0, 1, 0, 1, 0, 0, 1],
          [0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0]]]





          share|improve this answer

























            up vote
            1
            down vote










            up vote
            1
            down vote









            You can use a list comprehension for this:



            def char2bin(byte):
            return list(map(int, format(byte, '08b')))

            def segmentString(text, padding, chunksize):
            for index in range(0, len(text), chunksize):
            yield text[index:index + chunksize].ljust(chunksize, padding)

            def framer(text, padding='-', chunksize=8, encoding='utf8'):
            return [[char2bin(byte) for byte in segment] for segment in
            segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]


            This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.



            >>> framer('Hello, World!')
            [[[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, 0, 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, 0, 1, 0, 1, 1, 0, 1],
            [0, 0, 1, 0, 1, 1, 0, 1],
            [0, 0, 1, 0, 1, 1, 0, 1]]]


            Non-ascii characters require multiple bits to encode.



            >>> framer('💩', padding='x00')
            [[[1, 1, 1, 1, 0, 0, 0, 0],
            [1, 0, 0, 1, 1, 1, 1, 1],
            [1, 0, 0, 1, 0, 0, 1, 0],
            [1, 0, 1, 0, 1, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0]]]





            share|improve this answer














            You can use a list comprehension for this:



            def char2bin(byte):
            return list(map(int, format(byte, '08b')))

            def segmentString(text, padding, chunksize):
            for index in range(0, len(text), chunksize):
            yield text[index:index + chunksize].ljust(chunksize, padding)

            def framer(text, padding='-', chunksize=8, encoding='utf8'):
            return [[char2bin(byte) for byte in segment] for segment in
            segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]


            This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.



            >>> framer('Hello, World!')
            [[[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, 0, 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, 0, 1, 0, 1, 1, 0, 1],
            [0, 0, 1, 0, 1, 1, 0, 1],
            [0, 0, 1, 0, 1, 1, 0, 1]]]


            Non-ascii characters require multiple bits to encode.



            >>> framer('💩', padding='x00')
            [[[1, 1, 1, 1, 0, 0, 0, 0],
            [1, 0, 0, 1, 1, 1, 1, 1],
            [1, 0, 0, 1, 0, 0, 1, 0],
            [1, 0, 1, 0, 1, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0]]]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 16:33

























            answered Nov 10 at 15:46









            Håken Lid

            10.3k62440




            10.3k62440
























                up vote
                0
                down vote













                You could either use list comprehensions or make use of the itertools module.



                You can learn more about list comprehensions here, and more about itertootls here.






                share|improve this answer

























                  up vote
                  0
                  down vote













                  You could either use list comprehensions or make use of the itertools module.



                  You can learn more about list comprehensions here, and more about itertootls here.






                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You could either use list comprehensions or make use of the itertools module.



                    You can learn more about list comprehensions here, and more about itertootls here.






                    share|improve this answer












                    You could either use list comprehensions or make use of the itertools module.



                    You can learn more about list comprehensions here, and more about itertootls here.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 14:57









                    user4437749

                    506




                    506






















                        up vote
                        0
                        down vote













                        You can use below code to achieve your goal.



                        def segment_string(s, fill_by):
                        l =
                        while s:
                        if len(s) < 8:
                        s = s + (fill_by) * (8 - len(s))

                        l.append(s[0:8])
                        s = s[8:]

                        return l # ['Hello, W', 'orld!---']

                        def char2bin(ch):
                        a = bin(ord(ch))[2:]
                        l = [int(c) for c in a]

                        if len(l) < 8:
                        l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)

                        return l # [0, 1, 0, 0, 1, 0, 0, 0]

                        def framer(s, fill_by='-'):
                        segments = segment_string(s, fill_by) # Calling segment_string()
                        print(segments)

                        arr =
                        for segment in segments:
                        arr2 =
                        for ch in segment:
                        arr3 = char2bin(ch); # Calling char2bin()
                        arr2.append(arr3)

                        arr.append(arr2)

                        return arr # final list to be returned


                        if __name__ == "__main__":
                        f = framer('Hello, World!', '~')
                        print(f)


                        Output »

                        [[[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, 0, 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]]]

                        # >>> bin(126)
                        # '0b1111110'
                        # >>>
                        # >>> chr(126)
                        # '~'
                        # >>>





                        share|improve this answer



























                          up vote
                          0
                          down vote













                          You can use below code to achieve your goal.



                          def segment_string(s, fill_by):
                          l =
                          while s:
                          if len(s) < 8:
                          s = s + (fill_by) * (8 - len(s))

                          l.append(s[0:8])
                          s = s[8:]

                          return l # ['Hello, W', 'orld!---']

                          def char2bin(ch):
                          a = bin(ord(ch))[2:]
                          l = [int(c) for c in a]

                          if len(l) < 8:
                          l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)

                          return l # [0, 1, 0, 0, 1, 0, 0, 0]

                          def framer(s, fill_by='-'):
                          segments = segment_string(s, fill_by) # Calling segment_string()
                          print(segments)

                          arr =
                          for segment in segments:
                          arr2 =
                          for ch in segment:
                          arr3 = char2bin(ch); # Calling char2bin()
                          arr2.append(arr3)

                          arr.append(arr2)

                          return arr # final list to be returned


                          if __name__ == "__main__":
                          f = framer('Hello, World!', '~')
                          print(f)


                          Output »

                          [[[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, 0, 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]]]

                          # >>> bin(126)
                          # '0b1111110'
                          # >>>
                          # >>> chr(126)
                          # '~'
                          # >>>





                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can use below code to achieve your goal.



                            def segment_string(s, fill_by):
                            l =
                            while s:
                            if len(s) < 8:
                            s = s + (fill_by) * (8 - len(s))

                            l.append(s[0:8])
                            s = s[8:]

                            return l # ['Hello, W', 'orld!---']

                            def char2bin(ch):
                            a = bin(ord(ch))[2:]
                            l = [int(c) for c in a]

                            if len(l) < 8:
                            l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)

                            return l # [0, 1, 0, 0, 1, 0, 0, 0]

                            def framer(s, fill_by='-'):
                            segments = segment_string(s, fill_by) # Calling segment_string()
                            print(segments)

                            arr =
                            for segment in segments:
                            arr2 =
                            for ch in segment:
                            arr3 = char2bin(ch); # Calling char2bin()
                            arr2.append(arr3)

                            arr.append(arr2)

                            return arr # final list to be returned


                            if __name__ == "__main__":
                            f = framer('Hello, World!', '~')
                            print(f)


                            Output »

                            [[[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, 0, 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]]]

                            # >>> bin(126)
                            # '0b1111110'
                            # >>>
                            # >>> chr(126)
                            # '~'
                            # >>>





                            share|improve this answer














                            You can use below code to achieve your goal.



                            def segment_string(s, fill_by):
                            l =
                            while s:
                            if len(s) < 8:
                            s = s + (fill_by) * (8 - len(s))

                            l.append(s[0:8])
                            s = s[8:]

                            return l # ['Hello, W', 'orld!---']

                            def char2bin(ch):
                            a = bin(ord(ch))[2:]
                            l = [int(c) for c in a]

                            if len(l) < 8:
                            l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)

                            return l # [0, 1, 0, 0, 1, 0, 0, 0]

                            def framer(s, fill_by='-'):
                            segments = segment_string(s, fill_by) # Calling segment_string()
                            print(segments)

                            arr =
                            for segment in segments:
                            arr2 =
                            for ch in segment:
                            arr3 = char2bin(ch); # Calling char2bin()
                            arr2.append(arr3)

                            arr.append(arr2)

                            return arr # final list to be returned


                            if __name__ == "__main__":
                            f = framer('Hello, World!', '~')
                            print(f)


                            Output »

                            [[[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, 0, 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]]]

                            # >>> bin(126)
                            # '0b1111110'
                            # >>>
                            # >>> chr(126)
                            # '~'
                            # >>>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 10 at 15:40

























                            answered Nov 10 at 15:34









                            hygull

                            2,72311126




                            2,72311126






















                                hello is a new contributor. Be nice, and check out our Code of Conduct.










                                 

                                draft saved


                                draft discarded


















                                hello is a new contributor. Be nice, and check out our Code of Conduct.













                                hello is a new contributor. Be nice, and check out our Code of Conduct.












                                hello is a new contributor. Be nice, and check out our Code of Conduct.















                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240083%2fcreating-a-list-of-bits%23new-answer', 'question_page');
                                }
                                );

                                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







                                Popular posts from this blog

                                Full-time equivalent

                                さくらももこ

                                13 indicted, 8 arrested in Calif. drug cartel investigation