Nested lists in python containing a single string and not single letters





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I need to load text from a file which contains several lines, each line contains letters separated by coma, into a 2-dimensional list. When I run this, I get a 2 dimensional list, but the nested lists contain single strings instead of separated values, and I can not iterate over them. how do I solve this?



def read_matrix_file(filename):
matrix =
with open(filename, 'r') as matrix_letters:
for line in matrix_letters:
line = line.split()
matrix.append(line)
return matrix


result:



[['a,p,p,l,e'], ['a,g,o,d,o'], ['n,n,e,r,t'], ['g,a,T,A,C'], ['m,i,c,s,r'], ['P,o,P,o,P']]


I need each letter in the nested lists to be a single string so I can use them.



thanks in advance










share|improve this question

























  • So you need 'a,p,p,l,e' to be 'apple' or 'a','p','p','l','e' ?

    – Austin
    Nov 17 '18 at 7:04


















0















I need to load text from a file which contains several lines, each line contains letters separated by coma, into a 2-dimensional list. When I run this, I get a 2 dimensional list, but the nested lists contain single strings instead of separated values, and I can not iterate over them. how do I solve this?



def read_matrix_file(filename):
matrix =
with open(filename, 'r') as matrix_letters:
for line in matrix_letters:
line = line.split()
matrix.append(line)
return matrix


result:



[['a,p,p,l,e'], ['a,g,o,d,o'], ['n,n,e,r,t'], ['g,a,T,A,C'], ['m,i,c,s,r'], ['P,o,P,o,P']]


I need each letter in the nested lists to be a single string so I can use them.



thanks in advance










share|improve this question

























  • So you need 'a,p,p,l,e' to be 'apple' or 'a','p','p','l','e' ?

    – Austin
    Nov 17 '18 at 7:04














0












0








0








I need to load text from a file which contains several lines, each line contains letters separated by coma, into a 2-dimensional list. When I run this, I get a 2 dimensional list, but the nested lists contain single strings instead of separated values, and I can not iterate over them. how do I solve this?



def read_matrix_file(filename):
matrix =
with open(filename, 'r') as matrix_letters:
for line in matrix_letters:
line = line.split()
matrix.append(line)
return matrix


result:



[['a,p,p,l,e'], ['a,g,o,d,o'], ['n,n,e,r,t'], ['g,a,T,A,C'], ['m,i,c,s,r'], ['P,o,P,o,P']]


I need each letter in the nested lists to be a single string so I can use them.



thanks in advance










share|improve this question
















I need to load text from a file which contains several lines, each line contains letters separated by coma, into a 2-dimensional list. When I run this, I get a 2 dimensional list, but the nested lists contain single strings instead of separated values, and I can not iterate over them. how do I solve this?



def read_matrix_file(filename):
matrix =
with open(filename, 'r') as matrix_letters:
for line in matrix_letters:
line = line.split()
matrix.append(line)
return matrix


result:



[['a,p,p,l,e'], ['a,g,o,d,o'], ['n,n,e,r,t'], ['g,a,T,A,C'], ['m,i,c,s,r'], ['P,o,P,o,P']]


I need each letter in the nested lists to be a single string so I can use them.



thanks in advance







python list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 7:02









AkshayNevrekar

6,261102142




6,261102142










asked Nov 17 '18 at 6:53









TegernakoTegernako

576




576













  • So you need 'a,p,p,l,e' to be 'apple' or 'a','p','p','l','e' ?

    – Austin
    Nov 17 '18 at 7:04



















  • So you need 'a,p,p,l,e' to be 'apple' or 'a','p','p','l','e' ?

    – Austin
    Nov 17 '18 at 7:04

















So you need 'a,p,p,l,e' to be 'apple' or 'a','p','p','l','e' ?

– Austin
Nov 17 '18 at 7:04





So you need 'a,p,p,l,e' to be 'apple' or 'a','p','p','l','e' ?

– Austin
Nov 17 '18 at 7:04












3 Answers
3






active

oldest

votes


















1














split() function splits on white space by default. You can fix this by passing the string you want to split on. In this case, that would be a comma. The code below should work.



def read_matrix_file(filename):
matrix =
with open(filename, 'r') as matrix_letters:
for line in matrix_letters:
line = line.split(',')
matrix.append(line)
return matrix





share|improve this answer































    1














    The input format you described conforms to CSV format. Python has a library just for reading CSV files. If you just want to get the job done, you can use this library to do the work for you. Here's an example:



    Input(test.csv):



    a,string,here
    more,strings,here


    Code:



    >>> import csv
    >>> lines =
    >>> with open('test.csv') as file:
    ... reader = csv.reader(file)
    ... for row in reader:
    ... lines.append(row)
    ...
    >>>


    Output:



    >>> lines
    [['a', 'string', 'here'], ['more', 'strings', 'here']]





    share|improve this answer































      1














      Using the strip() function will get rid of the new line character as well:



      def read_matrix_file(filename):
      matrix =
      with open(filename, 'r') as matrix_letters:
      for line in matrix_letters:
      line = line.split(',')
      line[-1] = line[-1].strip()
      matrix.append(line)
      return matrix





      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',
        autoActivateHeartbeat: false,
        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%2f53348964%2fnested-lists-in-python-containing-a-single-string-and-not-single-letters%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









        1














        split() function splits on white space by default. You can fix this by passing the string you want to split on. In this case, that would be a comma. The code below should work.



        def read_matrix_file(filename):
        matrix =
        with open(filename, 'r') as matrix_letters:
        for line in matrix_letters:
        line = line.split(',')
        matrix.append(line)
        return matrix





        share|improve this answer




























          1














          split() function splits on white space by default. You can fix this by passing the string you want to split on. In this case, that would be a comma. The code below should work.



          def read_matrix_file(filename):
          matrix =
          with open(filename, 'r') as matrix_letters:
          for line in matrix_letters:
          line = line.split(',')
          matrix.append(line)
          return matrix





          share|improve this answer


























            1












            1








            1







            split() function splits on white space by default. You can fix this by passing the string you want to split on. In this case, that would be a comma. The code below should work.



            def read_matrix_file(filename):
            matrix =
            with open(filename, 'r') as matrix_letters:
            for line in matrix_letters:
            line = line.split(',')
            matrix.append(line)
            return matrix





            share|improve this answer













            split() function splits on white space by default. You can fix this by passing the string you want to split on. In this case, that would be a comma. The code below should work.



            def read_matrix_file(filename):
            matrix =
            with open(filename, 'r') as matrix_letters:
            for line in matrix_letters:
            line = line.split(',')
            matrix.append(line)
            return matrix






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '18 at 6:59









            merlynmerlyn

            1,78011323




            1,78011323

























                1














                The input format you described conforms to CSV format. Python has a library just for reading CSV files. If you just want to get the job done, you can use this library to do the work for you. Here's an example:



                Input(test.csv):



                a,string,here
                more,strings,here


                Code:



                >>> import csv
                >>> lines =
                >>> with open('test.csv') as file:
                ... reader = csv.reader(file)
                ... for row in reader:
                ... lines.append(row)
                ...
                >>>


                Output:



                >>> lines
                [['a', 'string', 'here'], ['more', 'strings', 'here']]





                share|improve this answer




























                  1














                  The input format you described conforms to CSV format. Python has a library just for reading CSV files. If you just want to get the job done, you can use this library to do the work for you. Here's an example:



                  Input(test.csv):



                  a,string,here
                  more,strings,here


                  Code:



                  >>> import csv
                  >>> lines =
                  >>> with open('test.csv') as file:
                  ... reader = csv.reader(file)
                  ... for row in reader:
                  ... lines.append(row)
                  ...
                  >>>


                  Output:



                  >>> lines
                  [['a', 'string', 'here'], ['more', 'strings', 'here']]





                  share|improve this answer


























                    1












                    1








                    1







                    The input format you described conforms to CSV format. Python has a library just for reading CSV files. If you just want to get the job done, you can use this library to do the work for you. Here's an example:



                    Input(test.csv):



                    a,string,here
                    more,strings,here


                    Code:



                    >>> import csv
                    >>> lines =
                    >>> with open('test.csv') as file:
                    ... reader = csv.reader(file)
                    ... for row in reader:
                    ... lines.append(row)
                    ...
                    >>>


                    Output:



                    >>> lines
                    [['a', 'string', 'here'], ['more', 'strings', 'here']]





                    share|improve this answer













                    The input format you described conforms to CSV format. Python has a library just for reading CSV files. If you just want to get the job done, you can use this library to do the work for you. Here's an example:



                    Input(test.csv):



                    a,string,here
                    more,strings,here


                    Code:



                    >>> import csv
                    >>> lines =
                    >>> with open('test.csv') as file:
                    ... reader = csv.reader(file)
                    ... for row in reader:
                    ... lines.append(row)
                    ...
                    >>>


                    Output:



                    >>> lines
                    [['a', 'string', 'here'], ['more', 'strings', 'here']]






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 17 '18 at 7:13









                    richflowrichflow

                    968212




                    968212























                        1














                        Using the strip() function will get rid of the new line character as well:



                        def read_matrix_file(filename):
                        matrix =
                        with open(filename, 'r') as matrix_letters:
                        for line in matrix_letters:
                        line = line.split(',')
                        line[-1] = line[-1].strip()
                        matrix.append(line)
                        return matrix





                        share|improve this answer




























                          1














                          Using the strip() function will get rid of the new line character as well:



                          def read_matrix_file(filename):
                          matrix =
                          with open(filename, 'r') as matrix_letters:
                          for line in matrix_letters:
                          line = line.split(',')
                          line[-1] = line[-1].strip()
                          matrix.append(line)
                          return matrix





                          share|improve this answer


























                            1












                            1








                            1







                            Using the strip() function will get rid of the new line character as well:



                            def read_matrix_file(filename):
                            matrix =
                            with open(filename, 'r') as matrix_letters:
                            for line in matrix_letters:
                            line = line.split(',')
                            line[-1] = line[-1].strip()
                            matrix.append(line)
                            return matrix





                            share|improve this answer













                            Using the strip() function will get rid of the new line character as well:



                            def read_matrix_file(filename):
                            matrix =
                            with open(filename, 'r') as matrix_letters:
                            for line in matrix_letters:
                            line = line.split(',')
                            line[-1] = line[-1].strip()
                            matrix.append(line)
                            return matrix






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 17 '18 at 7:39









                            reedx8reedx8

                            186




                            186






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348964%2fnested-lists-in-python-containing-a-single-string-and-not-single-letters%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

                                Bressuire

                                Vorschmack

                                Quarantine