How to slice a list containing a set of strings by comma?












3















I have a set of strings inside a list that I read from a CSV file and it looks like this:



myList = ('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106'),...


I want to store it into a database (Django) that has three fields:



fieldOne = '17.0.1.34', '203.84.210.248','202.73.40.45', ....
fieldTwo = '17.1.182.21', '27.111.228.3', '27.111.228.6', ...
fieldThree = '714', '10310', '18106',...


What should I do?










share|improve this question




















  • 2





    Are fieldOne, fieldTwo etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.

    – slider
    Nov 20 '18 at 3:47


















3















I have a set of strings inside a list that I read from a CSV file and it looks like this:



myList = ('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106'),...


I want to store it into a database (Django) that has three fields:



fieldOne = '17.0.1.34', '203.84.210.248','202.73.40.45', ....
fieldTwo = '17.1.182.21', '27.111.228.3', '27.111.228.6', ...
fieldThree = '714', '10310', '18106',...


What should I do?










share|improve this question




















  • 2





    Are fieldOne, fieldTwo etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.

    – slider
    Nov 20 '18 at 3:47
















3












3








3








I have a set of strings inside a list that I read from a CSV file and it looks like this:



myList = ('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106'),...


I want to store it into a database (Django) that has three fields:



fieldOne = '17.0.1.34', '203.84.210.248','202.73.40.45', ....
fieldTwo = '17.1.182.21', '27.111.228.3', '27.111.228.6', ...
fieldThree = '714', '10310', '18106',...


What should I do?










share|improve this question
















I have a set of strings inside a list that I read from a CSV file and it looks like this:



myList = ('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106'),...


I want to store it into a database (Django) that has three fields:



fieldOne = '17.0.1.34', '203.84.210.248','202.73.40.45', ....
fieldTwo = '17.1.182.21', '27.111.228.3', '27.111.228.6', ...
fieldThree = '714', '10310', '18106',...


What should I do?







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 3:46









Mihai Chelaru

2,180101022




2,180101022










asked Nov 20 '18 at 3:44









Banthita LimwilaiBanthita Limwilai

213




213








  • 2





    Are fieldOne, fieldTwo etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.

    – slider
    Nov 20 '18 at 3:47
















  • 2





    Are fieldOne, fieldTwo etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.

    – slider
    Nov 20 '18 at 3:47










2




2





Are fieldOne, fieldTwo etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.

– slider
Nov 20 '18 at 3:47







Are fieldOne, fieldTwo etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.

– slider
Nov 20 '18 at 3:47














4 Answers
4






active

oldest

votes


















4














You have a list of 3-tuples:



myList = [
(a1, b1, c1),
(a2, b2, c2),
(a3, b3, c3),
...
]


You want to refactor this list into three variables:



fieldOne = [a1, a2, a3, ...]
fieldTwo = [b1, b2, b3, ...]
fieldThree = [c1, c2, c3, ...]


You can do this using list comprehension: "Make a list by taking the kth value from each element of myList". That would look like this:



fieldOne = [i[0] for i in myList]    # k = 0
fieldTwo = [i[1] for i in myList] # k = 1
fieldThree = [i[2] for i in myList] # k = 2


Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.






share|improve this answer































    1














    You could also zip() your results and store them in a dictionary:



    >>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
    >>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
    >>> dict(zip(fields, zip(*myList)))
    {'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}


    Then you can just reference the 'fieldOne', 'fieldTwo', 'fieldThree' keys from this dictionary and store it in your database.






    share|improve this answer































      0














      I would suggest using the following way so you can use it multiple times.



      def create_field(myList):
      fieldOne = [myList[i][0] for i in range(len(myList))]
      fieldTwo = [myList[i][1] for i in range(len(myList))]
      fieldThree = [myList[i][2] for i in range(len(myList))]
      return fieldOne, fieldTwo, fieldThree
      create_field(myList)





      share|improve this answer































        0














        Given your data, here is a possible solution:



        # Initialize each field list
        fieldOne =
        fieldTwo =
        fieldThree =

        # fetch each **item** in **myList**, we extract the component corresponding
        # append it to the appropriate field
        for item in myList:
        fieldOne.append(item[0])
        fieldTwo.append(item[1])
        fieldThree.append(item[2])





        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%2f53385902%2fhow-to-slice-a-list-containing-a-set-of-strings-by-comma%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You have a list of 3-tuples:



          myList = [
          (a1, b1, c1),
          (a2, b2, c2),
          (a3, b3, c3),
          ...
          ]


          You want to refactor this list into three variables:



          fieldOne = [a1, a2, a3, ...]
          fieldTwo = [b1, b2, b3, ...]
          fieldThree = [c1, c2, c3, ...]


          You can do this using list comprehension: "Make a list by taking the kth value from each element of myList". That would look like this:



          fieldOne = [i[0] for i in myList]    # k = 0
          fieldTwo = [i[1] for i in myList] # k = 1
          fieldThree = [i[2] for i in myList] # k = 2


          Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.






          share|improve this answer




























            4














            You have a list of 3-tuples:



            myList = [
            (a1, b1, c1),
            (a2, b2, c2),
            (a3, b3, c3),
            ...
            ]


            You want to refactor this list into three variables:



            fieldOne = [a1, a2, a3, ...]
            fieldTwo = [b1, b2, b3, ...]
            fieldThree = [c1, c2, c3, ...]


            You can do this using list comprehension: "Make a list by taking the kth value from each element of myList". That would look like this:



            fieldOne = [i[0] for i in myList]    # k = 0
            fieldTwo = [i[1] for i in myList] # k = 1
            fieldThree = [i[2] for i in myList] # k = 2


            Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.






            share|improve this answer


























              4












              4








              4







              You have a list of 3-tuples:



              myList = [
              (a1, b1, c1),
              (a2, b2, c2),
              (a3, b3, c3),
              ...
              ]


              You want to refactor this list into three variables:



              fieldOne = [a1, a2, a3, ...]
              fieldTwo = [b1, b2, b3, ...]
              fieldThree = [c1, c2, c3, ...]


              You can do this using list comprehension: "Make a list by taking the kth value from each element of myList". That would look like this:



              fieldOne = [i[0] for i in myList]    # k = 0
              fieldTwo = [i[1] for i in myList] # k = 1
              fieldThree = [i[2] for i in myList] # k = 2


              Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.






              share|improve this answer













              You have a list of 3-tuples:



              myList = [
              (a1, b1, c1),
              (a2, b2, c2),
              (a3, b3, c3),
              ...
              ]


              You want to refactor this list into three variables:



              fieldOne = [a1, a2, a3, ...]
              fieldTwo = [b1, b2, b3, ...]
              fieldThree = [c1, c2, c3, ...]


              You can do this using list comprehension: "Make a list by taking the kth value from each element of myList". That would look like this:



              fieldOne = [i[0] for i in myList]    # k = 0
              fieldTwo = [i[1] for i in myList] # k = 1
              fieldThree = [i[2] for i in myList] # k = 2


              Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 20 '18 at 3:49









              Green Cloak GuyGreen Cloak Guy

              2,5131720




              2,5131720

























                  1














                  You could also zip() your results and store them in a dictionary:



                  >>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
                  >>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
                  >>> dict(zip(fields, zip(*myList)))
                  {'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}


                  Then you can just reference the 'fieldOne', 'fieldTwo', 'fieldThree' keys from this dictionary and store it in your database.






                  share|improve this answer




























                    1














                    You could also zip() your results and store them in a dictionary:



                    >>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
                    >>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
                    >>> dict(zip(fields, zip(*myList)))
                    {'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}


                    Then you can just reference the 'fieldOne', 'fieldTwo', 'fieldThree' keys from this dictionary and store it in your database.






                    share|improve this answer


























                      1












                      1








                      1







                      You could also zip() your results and store them in a dictionary:



                      >>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
                      >>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
                      >>> dict(zip(fields, zip(*myList)))
                      {'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}


                      Then you can just reference the 'fieldOne', 'fieldTwo', 'fieldThree' keys from this dictionary and store it in your database.






                      share|improve this answer













                      You could also zip() your results and store them in a dictionary:



                      >>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
                      >>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
                      >>> dict(zip(fields, zip(*myList)))
                      {'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}


                      Then you can just reference the 'fieldOne', 'fieldTwo', 'fieldThree' keys from this dictionary and store it in your database.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 20 '18 at 3:57









                      RoadRunnerRoadRunner

                      11.1k31340




                      11.1k31340























                          0














                          I would suggest using the following way so you can use it multiple times.



                          def create_field(myList):
                          fieldOne = [myList[i][0] for i in range(len(myList))]
                          fieldTwo = [myList[i][1] for i in range(len(myList))]
                          fieldThree = [myList[i][2] for i in range(len(myList))]
                          return fieldOne, fieldTwo, fieldThree
                          create_field(myList)





                          share|improve this answer




























                            0














                            I would suggest using the following way so you can use it multiple times.



                            def create_field(myList):
                            fieldOne = [myList[i][0] for i in range(len(myList))]
                            fieldTwo = [myList[i][1] for i in range(len(myList))]
                            fieldThree = [myList[i][2] for i in range(len(myList))]
                            return fieldOne, fieldTwo, fieldThree
                            create_field(myList)





                            share|improve this answer


























                              0












                              0








                              0







                              I would suggest using the following way so you can use it multiple times.



                              def create_field(myList):
                              fieldOne = [myList[i][0] for i in range(len(myList))]
                              fieldTwo = [myList[i][1] for i in range(len(myList))]
                              fieldThree = [myList[i][2] for i in range(len(myList))]
                              return fieldOne, fieldTwo, fieldThree
                              create_field(myList)





                              share|improve this answer













                              I would suggest using the following way so you can use it multiple times.



                              def create_field(myList):
                              fieldOne = [myList[i][0] for i in range(len(myList))]
                              fieldTwo = [myList[i][1] for i in range(len(myList))]
                              fieldThree = [myList[i][2] for i in range(len(myList))]
                              return fieldOne, fieldTwo, fieldThree
                              create_field(myList)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 20 '18 at 4:05









                              hemantahemanta

                              178




                              178























                                  0














                                  Given your data, here is a possible solution:



                                  # Initialize each field list
                                  fieldOne =
                                  fieldTwo =
                                  fieldThree =

                                  # fetch each **item** in **myList**, we extract the component corresponding
                                  # append it to the appropriate field
                                  for item in myList:
                                  fieldOne.append(item[0])
                                  fieldTwo.append(item[1])
                                  fieldThree.append(item[2])





                                  share|improve this answer






























                                    0














                                    Given your data, here is a possible solution:



                                    # Initialize each field list
                                    fieldOne =
                                    fieldTwo =
                                    fieldThree =

                                    # fetch each **item** in **myList**, we extract the component corresponding
                                    # append it to the appropriate field
                                    for item in myList:
                                    fieldOne.append(item[0])
                                    fieldTwo.append(item[1])
                                    fieldThree.append(item[2])





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      Given your data, here is a possible solution:



                                      # Initialize each field list
                                      fieldOne =
                                      fieldTwo =
                                      fieldThree =

                                      # fetch each **item** in **myList**, we extract the component corresponding
                                      # append it to the appropriate field
                                      for item in myList:
                                      fieldOne.append(item[0])
                                      fieldTwo.append(item[1])
                                      fieldThree.append(item[2])





                                      share|improve this answer















                                      Given your data, here is a possible solution:



                                      # Initialize each field list
                                      fieldOne =
                                      fieldTwo =
                                      fieldThree =

                                      # fetch each **item** in **myList**, we extract the component corresponding
                                      # append it to the appropriate field
                                      for item in myList:
                                      fieldOne.append(item[0])
                                      fieldTwo.append(item[1])
                                      fieldThree.append(item[2])






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 20 '18 at 4:25

























                                      answered Nov 20 '18 at 4:00









                                      eapetchoeapetcho

                                      42927




                                      42927






























                                          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%2f53385902%2fhow-to-slice-a-list-containing-a-set-of-strings-by-comma%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

                                          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                          SQL update select statement

                                          'app-layout' is not a known element: how to share Component with different Modules