search within list to group elements by shared content [closed]
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
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.
add a comment |
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
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
add a comment |
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
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
python list
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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']]
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
add a comment |
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']
add a comment |
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']
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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']]
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
add a comment |
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']]
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
add a comment |
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']]
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']]
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
add a comment |
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
add a comment |
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']
add a comment |
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']
add a comment |
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']
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']
answered Nov 13 '18 at 9:52
Rahul K PRahul K P
7,26122133
7,26122133
add a comment |
add a comment |
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']
add a comment |
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']
add a comment |
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']
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']
edited Nov 13 '18 at 10:20
answered Nov 13 '18 at 9:53
Vineeth SaiVineeth Sai
2,48751323
2,48751323
add a comment |
add a comment |
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