How to convert ASCII values from a string of strings











up vote
-2
down vote

favorite












Is it possible to convert a string of ASCII characters from a list of strings:



[['You '], ['have '], ['made '], ['my '], ['day.']]



I understand that the conversion is done using ord(i) as explained here. I just can't seem to figure out how to retain the list of list structure after the conversion that reads something like this:



[[ 1, 2, 3 ], [ 1, 2, 3, 4 ], etc. ]



Thank you!










share|improve this question




















  • 4




    Are your nested lists always just a single element long?
    – Martijn Pieters
    Nov 19 at 16:11










  • You don't need ord if you're using Python 3.
    – PM 2Ring
    Nov 19 at 16:13






  • 1




    BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
    – PM 2Ring
    Nov 19 at 16:15










  • Ok. Thanks. Just revised the question.
    – nagymusic
    Nov 19 at 16:52















up vote
-2
down vote

favorite












Is it possible to convert a string of ASCII characters from a list of strings:



[['You '], ['have '], ['made '], ['my '], ['day.']]



I understand that the conversion is done using ord(i) as explained here. I just can't seem to figure out how to retain the list of list structure after the conversion that reads something like this:



[[ 1, 2, 3 ], [ 1, 2, 3, 4 ], etc. ]



Thank you!










share|improve this question




















  • 4




    Are your nested lists always just a single element long?
    – Martijn Pieters
    Nov 19 at 16:11










  • You don't need ord if you're using Python 3.
    – PM 2Ring
    Nov 19 at 16:13






  • 1




    BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
    – PM 2Ring
    Nov 19 at 16:15










  • Ok. Thanks. Just revised the question.
    – nagymusic
    Nov 19 at 16:52













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











Is it possible to convert a string of ASCII characters from a list of strings:



[['You '], ['have '], ['made '], ['my '], ['day.']]



I understand that the conversion is done using ord(i) as explained here. I just can't seem to figure out how to retain the list of list structure after the conversion that reads something like this:



[[ 1, 2, 3 ], [ 1, 2, 3, 4 ], etc. ]



Thank you!










share|improve this question















Is it possible to convert a string of ASCII characters from a list of strings:



[['You '], ['have '], ['made '], ['my '], ['day.']]



I understand that the conversion is done using ord(i) as explained here. I just can't seem to figure out how to retain the list of list structure after the conversion that reads something like this:



[[ 1, 2, 3 ], [ 1, 2, 3, 4 ], etc. ]



Thank you!







python tuples ascii






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 16:51

























asked Nov 19 at 16:08









nagymusic

263




263








  • 4




    Are your nested lists always just a single element long?
    – Martijn Pieters
    Nov 19 at 16:11










  • You don't need ord if you're using Python 3.
    – PM 2Ring
    Nov 19 at 16:13






  • 1




    BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
    – PM 2Ring
    Nov 19 at 16:15










  • Ok. Thanks. Just revised the question.
    – nagymusic
    Nov 19 at 16:52














  • 4




    Are your nested lists always just a single element long?
    – Martijn Pieters
    Nov 19 at 16:11










  • You don't need ord if you're using Python 3.
    – PM 2Ring
    Nov 19 at 16:13






  • 1




    BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
    – PM 2Ring
    Nov 19 at 16:15










  • Ok. Thanks. Just revised the question.
    – nagymusic
    Nov 19 at 16:52








4




4




Are your nested lists always just a single element long?
– Martijn Pieters
Nov 19 at 16:11




Are your nested lists always just a single element long?
– Martijn Pieters
Nov 19 at 16:11












You don't need ord if you're using Python 3.
– PM 2Ring
Nov 19 at 16:13




You don't need ord if you're using Python 3.
– PM 2Ring
Nov 19 at 16:13




1




1




BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
– PM 2Ring
Nov 19 at 16:15




BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
– PM 2Ring
Nov 19 at 16:15












Ok. Thanks. Just revised the question.
– nagymusic
Nov 19 at 16:52




Ok. Thanks. Just revised the question.
– nagymusic
Nov 19 at 16:52












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










use list comprehension or maps to traverse into the inner elements and apply the ord function:



example:



# python 3 (py2 syntax slightly different)
x = [['You '], ['have '], ['made '], ['my '], ['day.']]
[list(map(ord, i[0])) for i in x]
# outputs
Out[41]:
[[89, 111, 117, 32],
[104, 97, 118, 101, 32],
[109, 97, 100, 101, 32],
[109, 121, 32],
[100, 97, 121, 46]]





share|improve this answer





















  • Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
    – nagymusic
    Nov 19 at 16:31


















up vote
1
down vote













You have two options:





  • Nest your list comprehension:



    [[ord(c) for c in nested[0]] for nested in outerlist]



  • In Python 3, you can encode the string to a bytes object, and convert that to a list. bytes objects are just sequences of integers, after all:



    [list(nested[0].encode('ascii')) for nested in outerlist]


    The Python 2 equivalent is to use the bytearray() type; for str (byte strings):



    [list(bytearray(nested[0])) for nested in outerlist]



In both cases I assume that your nested list contain just a single element each, a string.



Demo on Python 3.7:



>>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
>>> [[ord(c) for c in nested[0]] for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
>>> [list(nested[0].encode('ascii')) for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]





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
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378577%2fhow-to-convert-ascii-values-from-a-string-of-strings%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    use list comprehension or maps to traverse into the inner elements and apply the ord function:



    example:



    # python 3 (py2 syntax slightly different)
    x = [['You '], ['have '], ['made '], ['my '], ['day.']]
    [list(map(ord, i[0])) for i in x]
    # outputs
    Out[41]:
    [[89, 111, 117, 32],
    [104, 97, 118, 101, 32],
    [109, 97, 100, 101, 32],
    [109, 121, 32],
    [100, 97, 121, 46]]





    share|improve this answer





















    • Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
      – nagymusic
      Nov 19 at 16:31















    up vote
    3
    down vote



    accepted










    use list comprehension or maps to traverse into the inner elements and apply the ord function:



    example:



    # python 3 (py2 syntax slightly different)
    x = [['You '], ['have '], ['made '], ['my '], ['day.']]
    [list(map(ord, i[0])) for i in x]
    # outputs
    Out[41]:
    [[89, 111, 117, 32],
    [104, 97, 118, 101, 32],
    [109, 97, 100, 101, 32],
    [109, 121, 32],
    [100, 97, 121, 46]]





    share|improve this answer





















    • Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
      – nagymusic
      Nov 19 at 16:31













    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    use list comprehension or maps to traverse into the inner elements and apply the ord function:



    example:



    # python 3 (py2 syntax slightly different)
    x = [['You '], ['have '], ['made '], ['my '], ['day.']]
    [list(map(ord, i[0])) for i in x]
    # outputs
    Out[41]:
    [[89, 111, 117, 32],
    [104, 97, 118, 101, 32],
    [109, 97, 100, 101, 32],
    [109, 121, 32],
    [100, 97, 121, 46]]





    share|improve this answer












    use list comprehension or maps to traverse into the inner elements and apply the ord function:



    example:



    # python 3 (py2 syntax slightly different)
    x = [['You '], ['have '], ['made '], ['my '], ['day.']]
    [list(map(ord, i[0])) for i in x]
    # outputs
    Out[41]:
    [[89, 111, 117, 32],
    [104, 97, 118, 101, 32],
    [109, 97, 100, 101, 32],
    [109, 121, 32],
    [100, 97, 121, 46]]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 16:12









    Haleemur Ali

    11.9k21637




    11.9k21637












    • Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
      – nagymusic
      Nov 19 at 16:31


















    • Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
      – nagymusic
      Nov 19 at 16:31
















    Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
    – nagymusic
    Nov 19 at 16:31




    Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
    – nagymusic
    Nov 19 at 16:31












    up vote
    1
    down vote













    You have two options:





    • Nest your list comprehension:



      [[ord(c) for c in nested[0]] for nested in outerlist]



    • In Python 3, you can encode the string to a bytes object, and convert that to a list. bytes objects are just sequences of integers, after all:



      [list(nested[0].encode('ascii')) for nested in outerlist]


      The Python 2 equivalent is to use the bytearray() type; for str (byte strings):



      [list(bytearray(nested[0])) for nested in outerlist]



    In both cases I assume that your nested list contain just a single element each, a string.



    Demo on Python 3.7:



    >>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
    >>> [[ord(c) for c in nested[0]] for nested in l]
    [[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
    >>> [list(nested[0].encode('ascii')) for nested in l]
    [[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]





    share|improve this answer

























      up vote
      1
      down vote













      You have two options:





      • Nest your list comprehension:



        [[ord(c) for c in nested[0]] for nested in outerlist]



      • In Python 3, you can encode the string to a bytes object, and convert that to a list. bytes objects are just sequences of integers, after all:



        [list(nested[0].encode('ascii')) for nested in outerlist]


        The Python 2 equivalent is to use the bytearray() type; for str (byte strings):



        [list(bytearray(nested[0])) for nested in outerlist]



      In both cases I assume that your nested list contain just a single element each, a string.



      Demo on Python 3.7:



      >>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
      >>> [[ord(c) for c in nested[0]] for nested in l]
      [[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
      >>> [list(nested[0].encode('ascii')) for nested in l]
      [[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You have two options:





        • Nest your list comprehension:



          [[ord(c) for c in nested[0]] for nested in outerlist]



        • In Python 3, you can encode the string to a bytes object, and convert that to a list. bytes objects are just sequences of integers, after all:



          [list(nested[0].encode('ascii')) for nested in outerlist]


          The Python 2 equivalent is to use the bytearray() type; for str (byte strings):



          [list(bytearray(nested[0])) for nested in outerlist]



        In both cases I assume that your nested list contain just a single element each, a string.



        Demo on Python 3.7:



        >>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
        >>> [[ord(c) for c in nested[0]] for nested in l]
        [[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
        >>> [list(nested[0].encode('ascii')) for nested in l]
        [[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]





        share|improve this answer












        You have two options:





        • Nest your list comprehension:



          [[ord(c) for c in nested[0]] for nested in outerlist]



        • In Python 3, you can encode the string to a bytes object, and convert that to a list. bytes objects are just sequences of integers, after all:



          [list(nested[0].encode('ascii')) for nested in outerlist]


          The Python 2 equivalent is to use the bytearray() type; for str (byte strings):



          [list(bytearray(nested[0])) for nested in outerlist]



        In both cases I assume that your nested list contain just a single element each, a string.



        Demo on Python 3.7:



        >>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
        >>> [[ord(c) for c in nested[0]] for nested in l]
        [[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
        >>> [list(nested[0].encode('ascii')) for nested in l]
        [[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 16:17









        Martijn Pieters

        692k12923972236




        692k12923972236






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378577%2fhow-to-convert-ascii-values-from-a-string-of-strings%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

            Wiesbaden

            Marschland

            Dieringhausen