How to separate a string into 2 list of lists
up vote
1
down vote
favorite
I got this string:
n
n
NtOtHPtMtDtUtItNtOn
EtStAtEtItTtLtNtItNn
NtPtNtNtNtGtAOtDtCn
n
n
PERMANENTE
PETTINE
n
n
actually if you looks at original string ,you cannot see the t and n ,so I just edited to better understanding.
What is I'm trying to do is separate to 2 different list of lists,for example:
lists1 = [[NOHPMDUINO][ESAEITLNIN][NPNNNGAODC]]
lists2 = [[PERMANENTE][PETTINE]]
I tried to use many methods to solve this, but without success.
at first I removed the new lines at the beginning with .strip('n') method, and I tried to use replace , but I don't know how to make it right.
Thank you zsomko and snakecharmerb,
Using the method of zsomko and adding strip() to remove the newline at the beginning , here is the loop that I did to divide to 2 variables:
var = True
for line in t:
if line !=['']:
if var:
group1.append(line)
else:
group2.append(line)
else:
var = False
I hope this will help to someone :) If somebody has better solution ,more efficient ,I would like to hear
python-3.x
add a comment |
up vote
1
down vote
favorite
I got this string:
n
n
NtOtHPtMtDtUtItNtOn
EtStAtEtItTtLtNtItNn
NtPtNtNtNtGtAOtDtCn
n
n
PERMANENTE
PETTINE
n
n
actually if you looks at original string ,you cannot see the t and n ,so I just edited to better understanding.
What is I'm trying to do is separate to 2 different list of lists,for example:
lists1 = [[NOHPMDUINO][ESAEITLNIN][NPNNNGAODC]]
lists2 = [[PERMANENTE][PETTINE]]
I tried to use many methods to solve this, but without success.
at first I removed the new lines at the beginning with .strip('n') method, and I tried to use replace , but I don't know how to make it right.
Thank you zsomko and snakecharmerb,
Using the method of zsomko and adding strip() to remove the newline at the beginning , here is the loop that I did to divide to 2 variables:
var = True
for line in t:
if line !=['']:
if var:
group1.append(line)
else:
group2.append(line)
else:
var = False
I hope this will help to someone :) If somebody has better solution ,more efficient ,I would like to hear
python-3.x
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I got this string:
n
n
NtOtHPtMtDtUtItNtOn
EtStAtEtItTtLtNtItNn
NtPtNtNtNtGtAOtDtCn
n
n
PERMANENTE
PETTINE
n
n
actually if you looks at original string ,you cannot see the t and n ,so I just edited to better understanding.
What is I'm trying to do is separate to 2 different list of lists,for example:
lists1 = [[NOHPMDUINO][ESAEITLNIN][NPNNNGAODC]]
lists2 = [[PERMANENTE][PETTINE]]
I tried to use many methods to solve this, but without success.
at first I removed the new lines at the beginning with .strip('n') method, and I tried to use replace , but I don't know how to make it right.
Thank you zsomko and snakecharmerb,
Using the method of zsomko and adding strip() to remove the newline at the beginning , here is the loop that I did to divide to 2 variables:
var = True
for line in t:
if line !=['']:
if var:
group1.append(line)
else:
group2.append(line)
else:
var = False
I hope this will help to someone :) If somebody has better solution ,more efficient ,I would like to hear
python-3.x
I got this string:
n
n
NtOtHPtMtDtUtItNtOn
EtStAtEtItTtLtNtItNn
NtPtNtNtNtGtAOtDtCn
n
n
PERMANENTE
PETTINE
n
n
actually if you looks at original string ,you cannot see the t and n ,so I just edited to better understanding.
What is I'm trying to do is separate to 2 different list of lists,for example:
lists1 = [[NOHPMDUINO][ESAEITLNIN][NPNNNGAODC]]
lists2 = [[PERMANENTE][PETTINE]]
I tried to use many methods to solve this, but without success.
at first I removed the new lines at the beginning with .strip('n') method, and I tried to use replace , but I don't know how to make it right.
Thank you zsomko and snakecharmerb,
Using the method of zsomko and adding strip() to remove the newline at the beginning , here is the loop that I did to divide to 2 variables:
var = True
for line in t:
if line !=['']:
if var:
group1.append(line)
else:
group2.append(line)
else:
var = False
I hope this will help to someone :) If somebody has better solution ,more efficient ,I would like to hear
python-3.x
python-3.x
edited Nov 11 at 8:48
asked Nov 10 at 14:40
LordNord
346
346
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.
Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.
>>> for line in s.splitlines():
... if not line:
... # Skip empty lines
... continue
... cleaned = line.replace('t', '')
... print(cleaned)
...
NOHPMDUINO
ESAEITLNIN
NPNNNGAODC
PERMANENTE
PETTINE
Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.
We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:
>>> def g(s):
... out =
... for line in s.splitlines():
... if not line:
... if out:
... yield out
... out =
... continue
... cleaned = line.replace('t', '')
... out.append([cleaned])
... if out:
... yield out
...
>>>
The generator collects lines in a list (out
) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.
Looping over the generator returns the lists of lists in turn.
>>> for x in g(s):print(x)
...
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
[['PERMANENTE'], ['PETTINE']]
Alternatively, if you want a list of lists of lists, call list
on the generator:
>>> lists = list(g(s))
>>> print(lists)
[[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]
If you want to assign the result to named variables, you can unpack the call to list
:
>>> group1, group2 = list(g(s))
>>> group1
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
>>> group2
[['PERMANENTE'], ['PETTINE']]
but note to do this you need to know the number of lists that will be generated in advance.
Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
– LordNord
Nov 11 at 7:43
@LordNord I added grouping / listing logic for you.
– snakecharmerb
Nov 11 at 9:05
wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
– LordNord
Nov 11 at 9:11
@LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
– snakecharmerb
Nov 11 at 9:20
add a comment |
up vote
2
down vote
First eliminate the tabs and split the string
into lines:
lines = [line.replace('t', '') for line in string.splitlines()]
Then the following would yield the list of lists in the variable groups
as expected:
groups =
group =
for line in lines:
if group and not line:
groups.append(group)
group =
elif line:
group.append(line)
New contributor
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.
Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.
>>> for line in s.splitlines():
... if not line:
... # Skip empty lines
... continue
... cleaned = line.replace('t', '')
... print(cleaned)
...
NOHPMDUINO
ESAEITLNIN
NPNNNGAODC
PERMANENTE
PETTINE
Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.
We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:
>>> def g(s):
... out =
... for line in s.splitlines():
... if not line:
... if out:
... yield out
... out =
... continue
... cleaned = line.replace('t', '')
... out.append([cleaned])
... if out:
... yield out
...
>>>
The generator collects lines in a list (out
) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.
Looping over the generator returns the lists of lists in turn.
>>> for x in g(s):print(x)
...
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
[['PERMANENTE'], ['PETTINE']]
Alternatively, if you want a list of lists of lists, call list
on the generator:
>>> lists = list(g(s))
>>> print(lists)
[[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]
If you want to assign the result to named variables, you can unpack the call to list
:
>>> group1, group2 = list(g(s))
>>> group1
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
>>> group2
[['PERMANENTE'], ['PETTINE']]
but note to do this you need to know the number of lists that will be generated in advance.
Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
– LordNord
Nov 11 at 7:43
@LordNord I added grouping / listing logic for you.
– snakecharmerb
Nov 11 at 9:05
wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
– LordNord
Nov 11 at 9:11
@LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
– snakecharmerb
Nov 11 at 9:20
add a comment |
up vote
1
down vote
accepted
You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.
Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.
>>> for line in s.splitlines():
... if not line:
... # Skip empty lines
... continue
... cleaned = line.replace('t', '')
... print(cleaned)
...
NOHPMDUINO
ESAEITLNIN
NPNNNGAODC
PERMANENTE
PETTINE
Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.
We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:
>>> def g(s):
... out =
... for line in s.splitlines():
... if not line:
... if out:
... yield out
... out =
... continue
... cleaned = line.replace('t', '')
... out.append([cleaned])
... if out:
... yield out
...
>>>
The generator collects lines in a list (out
) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.
Looping over the generator returns the lists of lists in turn.
>>> for x in g(s):print(x)
...
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
[['PERMANENTE'], ['PETTINE']]
Alternatively, if you want a list of lists of lists, call list
on the generator:
>>> lists = list(g(s))
>>> print(lists)
[[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]
If you want to assign the result to named variables, you can unpack the call to list
:
>>> group1, group2 = list(g(s))
>>> group1
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
>>> group2
[['PERMANENTE'], ['PETTINE']]
but note to do this you need to know the number of lists that will be generated in advance.
Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
– LordNord
Nov 11 at 7:43
@LordNord I added grouping / listing logic for you.
– snakecharmerb
Nov 11 at 9:05
wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
– LordNord
Nov 11 at 9:11
@LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
– snakecharmerb
Nov 11 at 9:20
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.
Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.
>>> for line in s.splitlines():
... if not line:
... # Skip empty lines
... continue
... cleaned = line.replace('t', '')
... print(cleaned)
...
NOHPMDUINO
ESAEITLNIN
NPNNNGAODC
PERMANENTE
PETTINE
Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.
We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:
>>> def g(s):
... out =
... for line in s.splitlines():
... if not line:
... if out:
... yield out
... out =
... continue
... cleaned = line.replace('t', '')
... out.append([cleaned])
... if out:
... yield out
...
>>>
The generator collects lines in a list (out
) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.
Looping over the generator returns the lists of lists in turn.
>>> for x in g(s):print(x)
...
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
[['PERMANENTE'], ['PETTINE']]
Alternatively, if you want a list of lists of lists, call list
on the generator:
>>> lists = list(g(s))
>>> print(lists)
[[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]
If you want to assign the result to named variables, you can unpack the call to list
:
>>> group1, group2 = list(g(s))
>>> group1
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
>>> group2
[['PERMANENTE'], ['PETTINE']]
but note to do this you need to know the number of lists that will be generated in advance.
You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.
Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.
>>> for line in s.splitlines():
... if not line:
... # Skip empty lines
... continue
... cleaned = line.replace('t', '')
... print(cleaned)
...
NOHPMDUINO
ESAEITLNIN
NPNNNGAODC
PERMANENTE
PETTINE
Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.
We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:
>>> def g(s):
... out =
... for line in s.splitlines():
... if not line:
... if out:
... yield out
... out =
... continue
... cleaned = line.replace('t', '')
... out.append([cleaned])
... if out:
... yield out
...
>>>
The generator collects lines in a list (out
) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.
Looping over the generator returns the lists of lists in turn.
>>> for x in g(s):print(x)
...
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
[['PERMANENTE'], ['PETTINE']]
Alternatively, if you want a list of lists of lists, call list
on the generator:
>>> lists = list(g(s))
>>> print(lists)
[[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]
If you want to assign the result to named variables, you can unpack the call to list
:
>>> group1, group2 = list(g(s))
>>> group1
[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
>>> group2
[['PERMANENTE'], ['PETTINE']]
but note to do this you need to know the number of lists that will be generated in advance.
edited Nov 11 at 9:18
answered Nov 10 at 16:02
snakecharmerb
8,69132246
8,69132246
Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
– LordNord
Nov 11 at 7:43
@LordNord I added grouping / listing logic for you.
– snakecharmerb
Nov 11 at 9:05
wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
– LordNord
Nov 11 at 9:11
@LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
– snakecharmerb
Nov 11 at 9:20
add a comment |
Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
– LordNord
Nov 11 at 7:43
@LordNord I added grouping / listing logic for you.
– snakecharmerb
Nov 11 at 9:05
wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
– LordNord
Nov 11 at 9:11
@LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
– snakecharmerb
Nov 11 at 9:20
Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
– LordNord
Nov 11 at 7:43
Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
– LordNord
Nov 11 at 7:43
@LordNord I added grouping / listing logic for you.
– snakecharmerb
Nov 11 at 9:05
@LordNord I added grouping / listing logic for you.
– snakecharmerb
Nov 11 at 9:05
wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
– LordNord
Nov 11 at 9:11
wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
– LordNord
Nov 11 at 9:11
@LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
– snakecharmerb
Nov 11 at 9:20
@LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
– snakecharmerb
Nov 11 at 9:20
add a comment |
up vote
2
down vote
First eliminate the tabs and split the string
into lines:
lines = [line.replace('t', '') for line in string.splitlines()]
Then the following would yield the list of lists in the variable groups
as expected:
groups =
group =
for line in lines:
if group and not line:
groups.append(group)
group =
elif line:
group.append(line)
New contributor
add a comment |
up vote
2
down vote
First eliminate the tabs and split the string
into lines:
lines = [line.replace('t', '') for line in string.splitlines()]
Then the following would yield the list of lists in the variable groups
as expected:
groups =
group =
for line in lines:
if group and not line:
groups.append(group)
group =
elif line:
group.append(line)
New contributor
add a comment |
up vote
2
down vote
up vote
2
down vote
First eliminate the tabs and split the string
into lines:
lines = [line.replace('t', '') for line in string.splitlines()]
Then the following would yield the list of lists in the variable groups
as expected:
groups =
group =
for line in lines:
if group and not line:
groups.append(group)
group =
elif line:
group.append(line)
New contributor
First eliminate the tabs and split the string
into lines:
lines = [line.replace('t', '') for line in string.splitlines()]
Then the following would yield the list of lists in the variable groups
as expected:
groups =
group =
for line in lines:
if group and not line:
groups.append(group)
group =
elif line:
group.append(line)
New contributor
New contributor
answered Nov 10 at 16:01
zsomko
212
212
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240025%2fhow-to-separate-a-string-into-2-list-of-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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