search within list to group elements by shared content [closed]










-2















if I have list with this content



[MNA1
MNA3
MNA3_1
MNA3_2
MNA2
MPA3_3
MPA3_2
MPA3_1
MPA3
MPB]


how can I take all values that have the same first four chars in one group
example



MPA3_3
MPA3_2
MPA3_1
MPA3


must be in one group(list) or any other thing










share|improve this question













closed as unclear what you're asking by coldspeed, stovfl, Umair, DanielBarbarian, Nirekin Nov 13 '18 at 12:20


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















  • that is not a list of strings

    – Netwave
    Nov 13 '18 at 9:38











  • This not a Python list. Please post some actual data.

    – schwobaseggl
    Nov 13 '18 at 9:38






  • 1





    Have you tried anything at all?

    – Jerry
    Nov 13 '18 at 9:39











  • its a list of objects and each object has object name as string

    – M.salameh
    Nov 13 '18 at 9:46











  • Can you put a sample some data you'd like to work on

    – Moussa
    Nov 13 '18 at 9:49















-2















if I have list with this content



[MNA1
MNA3
MNA3_1
MNA3_2
MNA2
MPA3_3
MPA3_2
MPA3_1
MPA3
MPB]


how can I take all values that have the same first four chars in one group
example



MPA3_3
MPA3_2
MPA3_1
MPA3


must be in one group(list) or any other thing










share|improve this question













closed as unclear what you're asking by coldspeed, stovfl, Umair, DanielBarbarian, Nirekin Nov 13 '18 at 12:20


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















  • that is not a list of strings

    – Netwave
    Nov 13 '18 at 9:38











  • This not a Python list. Please post some actual data.

    – schwobaseggl
    Nov 13 '18 at 9:38






  • 1





    Have you tried anything at all?

    – Jerry
    Nov 13 '18 at 9:39











  • its a list of objects and each object has object name as string

    – M.salameh
    Nov 13 '18 at 9:46











  • Can you put a sample some data you'd like to work on

    – Moussa
    Nov 13 '18 at 9:49













-2












-2








-2


1






if I have list with this content



[MNA1
MNA3
MNA3_1
MNA3_2
MNA2
MPA3_3
MPA3_2
MPA3_1
MPA3
MPB]


how can I take all values that have the same first four chars in one group
example



MPA3_3
MPA3_2
MPA3_1
MPA3


must be in one group(list) or any other thing










share|improve this question














if I have list with this content



[MNA1
MNA3
MNA3_1
MNA3_2
MNA2
MPA3_3
MPA3_2
MPA3_1
MPA3
MPB]


how can I take all values that have the same first four chars in one group
example



MPA3_3
MPA3_2
MPA3_1
MPA3


must be in one group(list) or any other thing







python list






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 9:37









M.salamehM.salameh

457




457




closed as unclear what you're asking by coldspeed, stovfl, Umair, DanielBarbarian, Nirekin Nov 13 '18 at 12:20


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by coldspeed, stovfl, Umair, DanielBarbarian, Nirekin Nov 13 '18 at 12:20


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • that is not a list of strings

    – Netwave
    Nov 13 '18 at 9:38











  • This not a Python list. Please post some actual data.

    – schwobaseggl
    Nov 13 '18 at 9:38






  • 1





    Have you tried anything at all?

    – Jerry
    Nov 13 '18 at 9:39











  • its a list of objects and each object has object name as string

    – M.salameh
    Nov 13 '18 at 9:46











  • Can you put a sample some data you'd like to work on

    – Moussa
    Nov 13 '18 at 9:49

















  • that is not a list of strings

    – Netwave
    Nov 13 '18 at 9:38











  • This not a Python list. Please post some actual data.

    – schwobaseggl
    Nov 13 '18 at 9:38






  • 1





    Have you tried anything at all?

    – Jerry
    Nov 13 '18 at 9:39











  • its a list of objects and each object has object name as string

    – M.salameh
    Nov 13 '18 at 9:46











  • Can you put a sample some data you'd like to work on

    – Moussa
    Nov 13 '18 at 9:49
















that is not a list of strings

– Netwave
Nov 13 '18 at 9:38





that is not a list of strings

– Netwave
Nov 13 '18 at 9:38













This not a Python list. Please post some actual data.

– schwobaseggl
Nov 13 '18 at 9:38





This not a Python list. Please post some actual data.

– schwobaseggl
Nov 13 '18 at 9:38




1




1





Have you tried anything at all?

– Jerry
Nov 13 '18 at 9:39





Have you tried anything at all?

– Jerry
Nov 13 '18 at 9:39













its a list of objects and each object has object name as string

– M.salameh
Nov 13 '18 at 9:46





its a list of objects and each object has object name as string

– M.salameh
Nov 13 '18 at 9:46













Can you put a sample some data you'd like to work on

– Moussa
Nov 13 '18 at 9:49





Can you put a sample some data you'd like to work on

– Moussa
Nov 13 '18 at 9:49












3 Answers
3






active

oldest

votes


















1














assuming you have strings.



l = [
'MNA1',
'MNA3',
'MNA3_1',
'MNA3_2',
'MNA2',
'MPA3_3',
'MPA3_2',
'MPA3_1',
'MPA3',
'MPB'
]


you can do



values = set([elem[:4] for elem in l])
newlist = [[elem for elem in l if elem[:4]==x] for x in values]


or as a oneliner:



newlist = [[elem for elem in l if elem[:4]==x] for x in set([elem[:4] for elem in l])]


newlist looks like:



 [['MNA1'],
['MNA3', 'MNA3_1', 'MNA3_2'],
['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
['MPB'],
['MNA2']]





share|improve this answer

























  • what if this was list of objects and each object has name so I used for loop to print this list?

    – M.salameh
    Nov 13 '18 at 9:54











  • if its a list of objects with names you can replace elem[:4] with elem.getName()[:4]

    – Florian H
    Nov 13 '18 at 9:57


















0














You can do with groupby,



In [13]: for g,l in groupby(lst,key=lambda x:x.split('_')[0]):
...: temp = list(l)
...: if len(temp) == 4:
...: print temp
...:
['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3']





share|improve this answer






























    0














    Here's one way of grouping your item together.



    l = [
    'MNA1',
    'MNA3',
    'MNA3_1',
    'MNA3_2',
    'MNA2',
    'MPA3_3',
    'MPA3_2',
    'MPA3_1',
    'MPA3',
    'MPB'
    ]

    new_dict =
    for item in ls:
    key = item[0:4]
    if key in new_dict.keys():
    new_dict[key].append(item)
    else:
    new_dict[key] = [item]

    print(new_dict)


    Outputs:



    'MNA1': ['MNA1'],
    'MNA2': ['MNA2'],
    'MNA3': ['MNA3', 'MNA3_1', 'MNA3_2'],
    'MPA3': ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
    'MPB': ['MPB']





    share|improve this answer































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      assuming you have strings.



      l = [
      'MNA1',
      'MNA3',
      'MNA3_1',
      'MNA3_2',
      'MNA2',
      'MPA3_3',
      'MPA3_2',
      'MPA3_1',
      'MPA3',
      'MPB'
      ]


      you can do



      values = set([elem[:4] for elem in l])
      newlist = [[elem for elem in l if elem[:4]==x] for x in values]


      or as a oneliner:



      newlist = [[elem for elem in l if elem[:4]==x] for x in set([elem[:4] for elem in l])]


      newlist looks like:



       [['MNA1'],
      ['MNA3', 'MNA3_1', 'MNA3_2'],
      ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
      ['MPB'],
      ['MNA2']]





      share|improve this answer

























      • what if this was list of objects and each object has name so I used for loop to print this list?

        – M.salameh
        Nov 13 '18 at 9:54











      • if its a list of objects with names you can replace elem[:4] with elem.getName()[:4]

        – Florian H
        Nov 13 '18 at 9:57















      1














      assuming you have strings.



      l = [
      'MNA1',
      'MNA3',
      'MNA3_1',
      'MNA3_2',
      'MNA2',
      'MPA3_3',
      'MPA3_2',
      'MPA3_1',
      'MPA3',
      'MPB'
      ]


      you can do



      values = set([elem[:4] for elem in l])
      newlist = [[elem for elem in l if elem[:4]==x] for x in values]


      or as a oneliner:



      newlist = [[elem for elem in l if elem[:4]==x] for x in set([elem[:4] for elem in l])]


      newlist looks like:



       [['MNA1'],
      ['MNA3', 'MNA3_1', 'MNA3_2'],
      ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
      ['MPB'],
      ['MNA2']]





      share|improve this answer

























      • what if this was list of objects and each object has name so I used for loop to print this list?

        – M.salameh
        Nov 13 '18 at 9:54











      • if its a list of objects with names you can replace elem[:4] with elem.getName()[:4]

        – Florian H
        Nov 13 '18 at 9:57













      1












      1








      1







      assuming you have strings.



      l = [
      'MNA1',
      'MNA3',
      'MNA3_1',
      'MNA3_2',
      'MNA2',
      'MPA3_3',
      'MPA3_2',
      'MPA3_1',
      'MPA3',
      'MPB'
      ]


      you can do



      values = set([elem[:4] for elem in l])
      newlist = [[elem for elem in l if elem[:4]==x] for x in values]


      or as a oneliner:



      newlist = [[elem for elem in l if elem[:4]==x] for x in set([elem[:4] for elem in l])]


      newlist looks like:



       [['MNA1'],
      ['MNA3', 'MNA3_1', 'MNA3_2'],
      ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
      ['MPB'],
      ['MNA2']]





      share|improve this answer















      assuming you have strings.



      l = [
      'MNA1',
      'MNA3',
      'MNA3_1',
      'MNA3_2',
      'MNA2',
      'MPA3_3',
      'MPA3_2',
      'MPA3_1',
      'MPA3',
      'MPB'
      ]


      you can do



      values = set([elem[:4] for elem in l])
      newlist = [[elem for elem in l if elem[:4]==x] for x in values]


      or as a oneliner:



      newlist = [[elem for elem in l if elem[:4]==x] for x in set([elem[:4] for elem in l])]


      newlist looks like:



       [['MNA1'],
      ['MNA3', 'MNA3_1', 'MNA3_2'],
      ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
      ['MPB'],
      ['MNA2']]






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 13 '18 at 9:55

























      answered Nov 13 '18 at 9:50









      Florian HFlorian H

      1,0861312




      1,0861312












      • what if this was list of objects and each object has name so I used for loop to print this list?

        – M.salameh
        Nov 13 '18 at 9:54











      • if its a list of objects with names you can replace elem[:4] with elem.getName()[:4]

        – Florian H
        Nov 13 '18 at 9:57

















      • what if this was list of objects and each object has name so I used for loop to print this list?

        – M.salameh
        Nov 13 '18 at 9:54











      • if its a list of objects with names you can replace elem[:4] with elem.getName()[:4]

        – Florian H
        Nov 13 '18 at 9:57
















      what if this was list of objects and each object has name so I used for loop to print this list?

      – M.salameh
      Nov 13 '18 at 9:54





      what if this was list of objects and each object has name so I used for loop to print this list?

      – M.salameh
      Nov 13 '18 at 9:54













      if its a list of objects with names you can replace elem[:4] with elem.getName()[:4]

      – Florian H
      Nov 13 '18 at 9:57





      if its a list of objects with names you can replace elem[:4] with elem.getName()[:4]

      – Florian H
      Nov 13 '18 at 9:57













      0














      You can do with groupby,



      In [13]: for g,l in groupby(lst,key=lambda x:x.split('_')[0]):
      ...: temp = list(l)
      ...: if len(temp) == 4:
      ...: print temp
      ...:
      ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3']





      share|improve this answer



























        0














        You can do with groupby,



        In [13]: for g,l in groupby(lst,key=lambda x:x.split('_')[0]):
        ...: temp = list(l)
        ...: if len(temp) == 4:
        ...: print temp
        ...:
        ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3']





        share|improve this answer

























          0












          0








          0







          You can do with groupby,



          In [13]: for g,l in groupby(lst,key=lambda x:x.split('_')[0]):
          ...: temp = list(l)
          ...: if len(temp) == 4:
          ...: print temp
          ...:
          ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3']





          share|improve this answer













          You can do with groupby,



          In [13]: for g,l in groupby(lst,key=lambda x:x.split('_')[0]):
          ...: temp = list(l)
          ...: if len(temp) == 4:
          ...: print temp
          ...:
          ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3']






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 9:52









          Rahul K PRahul K P

          7,26122133




          7,26122133





















              0














              Here's one way of grouping your item together.



              l = [
              'MNA1',
              'MNA3',
              'MNA3_1',
              'MNA3_2',
              'MNA2',
              'MPA3_3',
              'MPA3_2',
              'MPA3_1',
              'MPA3',
              'MPB'
              ]

              new_dict =
              for item in ls:
              key = item[0:4]
              if key in new_dict.keys():
              new_dict[key].append(item)
              else:
              new_dict[key] = [item]

              print(new_dict)


              Outputs:



              'MNA1': ['MNA1'],
              'MNA2': ['MNA2'],
              'MNA3': ['MNA3', 'MNA3_1', 'MNA3_2'],
              'MPA3': ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
              'MPB': ['MPB']





              share|improve this answer





























                0














                Here's one way of grouping your item together.



                l = [
                'MNA1',
                'MNA3',
                'MNA3_1',
                'MNA3_2',
                'MNA2',
                'MPA3_3',
                'MPA3_2',
                'MPA3_1',
                'MPA3',
                'MPB'
                ]

                new_dict =
                for item in ls:
                key = item[0:4]
                if key in new_dict.keys():
                new_dict[key].append(item)
                else:
                new_dict[key] = [item]

                print(new_dict)


                Outputs:



                'MNA1': ['MNA1'],
                'MNA2': ['MNA2'],
                'MNA3': ['MNA3', 'MNA3_1', 'MNA3_2'],
                'MPA3': ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
                'MPB': ['MPB']





                share|improve this answer



























                  0












                  0








                  0







                  Here's one way of grouping your item together.



                  l = [
                  'MNA1',
                  'MNA3',
                  'MNA3_1',
                  'MNA3_2',
                  'MNA2',
                  'MPA3_3',
                  'MPA3_2',
                  'MPA3_1',
                  'MPA3',
                  'MPB'
                  ]

                  new_dict =
                  for item in ls:
                  key = item[0:4]
                  if key in new_dict.keys():
                  new_dict[key].append(item)
                  else:
                  new_dict[key] = [item]

                  print(new_dict)


                  Outputs:



                  'MNA1': ['MNA1'],
                  'MNA2': ['MNA2'],
                  'MNA3': ['MNA3', 'MNA3_1', 'MNA3_2'],
                  'MPA3': ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
                  'MPB': ['MPB']





                  share|improve this answer















                  Here's one way of grouping your item together.



                  l = [
                  'MNA1',
                  'MNA3',
                  'MNA3_1',
                  'MNA3_2',
                  'MNA2',
                  'MPA3_3',
                  'MPA3_2',
                  'MPA3_1',
                  'MPA3',
                  'MPB'
                  ]

                  new_dict =
                  for item in ls:
                  key = item[0:4]
                  if key in new_dict.keys():
                  new_dict[key].append(item)
                  else:
                  new_dict[key] = [item]

                  print(new_dict)


                  Outputs:



                  'MNA1': ['MNA1'],
                  'MNA2': ['MNA2'],
                  'MNA3': ['MNA3', 'MNA3_1', 'MNA3_2'],
                  'MPA3': ['MPA3_3', 'MPA3_2', 'MPA3_1', 'MPA3'],
                  'MPB': ['MPB']






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 10:20

























                  answered Nov 13 '18 at 9:53









                  Vineeth SaiVineeth Sai

                  2,48751323




                  2,48751323













                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20