How to combine similar characters in a list?
I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:
test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)
Which returns this in the splitter variable:
['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']
I'm trying to combine the hashes so the list turns into this:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
Thanks for any help!
python
add a comment |
I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:
test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)
Which returns this in the splitter variable:
['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']
I'm trying to combine the hashes so the list turns into this:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
Thanks for any help!
python
If you want to combine similar characters your result must be['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating#.
– Mykola Zotko
Feb 9 at 14:26
re.splitwith the right pattern oritertools.groupbyin general should do it.
– pylang
Feb 11 at 18:42
add a comment |
I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:
test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)
Which returns this in the splitter variable:
['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']
I'm trying to combine the hashes so the list turns into this:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
Thanks for any help!
python
I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:
test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)
Which returns this in the splitter variable:
['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']
I'm trying to combine the hashes so the list turns into this:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
Thanks for any help!
python
python
edited Feb 9 at 4:11
Ajax1234
42.7k42954
42.7k42954
asked Feb 9 at 4:10
GregGreg
543
543
If you want to combine similar characters your result must be['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating#.
– Mykola Zotko
Feb 9 at 14:26
re.splitwith the right pattern oritertools.groupbyin general should do it.
– pylang
Feb 11 at 18:42
add a comment |
If you want to combine similar characters your result must be['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating#.
– Mykola Zotko
Feb 9 at 14:26
re.splitwith the right pattern oritertools.groupbyin general should do it.
– pylang
Feb 11 at 18:42
If you want to combine similar characters your result must be
['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating #.– Mykola Zotko
Feb 9 at 14:26
If you want to combine similar characters your result must be
['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating #.– Mykola Zotko
Feb 9 at 14:26
re.split with the right pattern or itertools.groupby in general should do it.– pylang
Feb 11 at 18:42
re.split with the right pattern or itertools.groupby in general should do it.– pylang
Feb 11 at 18:42
add a comment |
3 Answers
3
active
oldest
votes
Try:
splitter = re.split("(#+)", test)
1
It gives an empty string at the end.
– AkshayNevrekar
Feb 9 at 4:34
3
@AkshayNevrekar Withsplitter = filter(None, splitter)(as in the original post), it does not.
– DYZ
Feb 9 at 4:37
This works for me. Thanks for the help!
– Greg
Feb 9 at 4:43
Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".
– pylang
Feb 11 at 18:39
add a comment |
You can use itertools.groupby:
import itertools
test = 'hello###_world###test#test123##'
new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
You can also use re.findall:
import re
result = re.findall('#+|[^#]+', test)
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
This one also does the job. Thanks for the help!
– Greg
Feb 9 at 4:44
add a comment |
Add + at the end of the regular expression and filtering None values will do the trick
>>> import re
>>> test = 'hello###_world###test#test123##'
>>> splitter = re.split("(#+)", test)
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
>>> splitter = list(filter(None, splitter))
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
>>>
add a comment |
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
});
}
});
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%2f54603094%2fhow-to-combine-similar-characters-in-a-list%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
Try:
splitter = re.split("(#+)", test)
1
It gives an empty string at the end.
– AkshayNevrekar
Feb 9 at 4:34
3
@AkshayNevrekar Withsplitter = filter(None, splitter)(as in the original post), it does not.
– DYZ
Feb 9 at 4:37
This works for me. Thanks for the help!
– Greg
Feb 9 at 4:43
Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".
– pylang
Feb 11 at 18:39
add a comment |
Try:
splitter = re.split("(#+)", test)
1
It gives an empty string at the end.
– AkshayNevrekar
Feb 9 at 4:34
3
@AkshayNevrekar Withsplitter = filter(None, splitter)(as in the original post), it does not.
– DYZ
Feb 9 at 4:37
This works for me. Thanks for the help!
– Greg
Feb 9 at 4:43
Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".
– pylang
Feb 11 at 18:39
add a comment |
Try:
splitter = re.split("(#+)", test)
Try:
splitter = re.split("(#+)", test)
answered Feb 9 at 4:29
mingganzmingganz
33816
33816
1
It gives an empty string at the end.
– AkshayNevrekar
Feb 9 at 4:34
3
@AkshayNevrekar Withsplitter = filter(None, splitter)(as in the original post), it does not.
– DYZ
Feb 9 at 4:37
This works for me. Thanks for the help!
– Greg
Feb 9 at 4:43
Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".
– pylang
Feb 11 at 18:39
add a comment |
1
It gives an empty string at the end.
– AkshayNevrekar
Feb 9 at 4:34
3
@AkshayNevrekar Withsplitter = filter(None, splitter)(as in the original post), it does not.
– DYZ
Feb 9 at 4:37
This works for me. Thanks for the help!
– Greg
Feb 9 at 4:43
Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".
– pylang
Feb 11 at 18:39
1
1
It gives an empty string at the end.
– AkshayNevrekar
Feb 9 at 4:34
It gives an empty string at the end.
– AkshayNevrekar
Feb 9 at 4:34
3
3
@AkshayNevrekar With
splitter = filter(None, splitter) (as in the original post), it does not.– DYZ
Feb 9 at 4:37
@AkshayNevrekar With
splitter = filter(None, splitter) (as in the original post), it does not.– DYZ
Feb 9 at 4:37
This works for me. Thanks for the help!
– Greg
Feb 9 at 4:43
This works for me. Thanks for the help!
– Greg
Feb 9 at 4:43
Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".
– pylang
Feb 11 at 18:39
Yep. I'd just make a habit to use raw strings in general with regex patterns, e.g. `r"(#+)".
– pylang
Feb 11 at 18:39
add a comment |
You can use itertools.groupby:
import itertools
test = 'hello###_world###test#test123##'
new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
You can also use re.findall:
import re
result = re.findall('#+|[^#]+', test)
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
This one also does the job. Thanks for the help!
– Greg
Feb 9 at 4:44
add a comment |
You can use itertools.groupby:
import itertools
test = 'hello###_world###test#test123##'
new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
You can also use re.findall:
import re
result = re.findall('#+|[^#]+', test)
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
This one also does the job. Thanks for the help!
– Greg
Feb 9 at 4:44
add a comment |
You can use itertools.groupby:
import itertools
test = 'hello###_world###test#test123##'
new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
You can also use re.findall:
import re
result = re.findall('#+|[^#]+', test)
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
You can use itertools.groupby:
import itertools
test = 'hello###_world###test#test123##'
new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
You can also use re.findall:
import re
result = re.findall('#+|[^#]+', test)
Output:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
edited Feb 9 at 4:47
answered Feb 9 at 4:12
Ajax1234Ajax1234
42.7k42954
42.7k42954
This one also does the job. Thanks for the help!
– Greg
Feb 9 at 4:44
add a comment |
This one also does the job. Thanks for the help!
– Greg
Feb 9 at 4:44
This one also does the job. Thanks for the help!
– Greg
Feb 9 at 4:44
This one also does the job. Thanks for the help!
– Greg
Feb 9 at 4:44
add a comment |
Add + at the end of the regular expression and filtering None values will do the trick
>>> import re
>>> test = 'hello###_world###test#test123##'
>>> splitter = re.split("(#+)", test)
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
>>> splitter = list(filter(None, splitter))
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
>>>
add a comment |
Add + at the end of the regular expression and filtering None values will do the trick
>>> import re
>>> test = 'hello###_world###test#test123##'
>>> splitter = re.split("(#+)", test)
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
>>> splitter = list(filter(None, splitter))
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
>>>
add a comment |
Add + at the end of the regular expression and filtering None values will do the trick
>>> import re
>>> test = 'hello###_world###test#test123##'
>>> splitter = re.split("(#+)", test)
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
>>> splitter = list(filter(None, splitter))
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
>>>
Add + at the end of the regular expression and filtering None values will do the trick
>>> import re
>>> test = 'hello###_world###test#test123##'
>>> splitter = re.split("(#+)", test)
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
>>> splitter = list(filter(None, splitter))
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
>>>
answered Feb 9 at 4:40
Bodhi94Bodhi94
987621
987621
add a comment |
add a comment |
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.
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%2f54603094%2fhow-to-combine-similar-characters-in-a-list%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
If you want to combine similar characters your result must be
['h’, ‘e', 'll', 'o', '###'...]. In your case you split the string by repeating#.– Mykola Zotko
Feb 9 at 14:26
re.splitwith the right pattern oritertools.groupbyin general should do it.– pylang
Feb 11 at 18:42