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
New contributor
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
New contributor
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
New contributor
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
New contributor
New contributor
edited 1 hour ago
Ajax1234
41.4k42853
41.4k42853
New contributor
asked 1 hour ago
GregGreg
362
362
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Try:
splitter = re.split("(#+)", test)
1
It gives an empty string at the end.
– AkshayNevrekar
1 hour ago
2
@AkshayNevrekar Withsplitter = filter(None, splitter)
(as in the original post), it does not.
– DYZ
59 mins ago
This works for me. Thanks for the help!
– Greg
53 mins ago
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
52 mins ago
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
});
}
});
Greg is a new contributor. Be nice, and check out our Code of Conduct.
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
1 hour ago
2
@AkshayNevrekar Withsplitter = filter(None, splitter)
(as in the original post), it does not.
– DYZ
59 mins ago
This works for me. Thanks for the help!
– Greg
53 mins ago
add a comment |
Try:
splitter = re.split("(#+)", test)
1
It gives an empty string at the end.
– AkshayNevrekar
1 hour ago
2
@AkshayNevrekar Withsplitter = filter(None, splitter)
(as in the original post), it does not.
– DYZ
59 mins ago
This works for me. Thanks for the help!
– Greg
53 mins ago
add a comment |
Try:
splitter = re.split("(#+)", test)
Try:
splitter = re.split("(#+)", test)
answered 1 hour ago
mingganzmingganz
30816
30816
1
It gives an empty string at the end.
– AkshayNevrekar
1 hour ago
2
@AkshayNevrekar Withsplitter = filter(None, splitter)
(as in the original post), it does not.
– DYZ
59 mins ago
This works for me. Thanks for the help!
– Greg
53 mins ago
add a comment |
1
It gives an empty string at the end.
– AkshayNevrekar
1 hour ago
2
@AkshayNevrekar Withsplitter = filter(None, splitter)
(as in the original post), it does not.
– DYZ
59 mins ago
This works for me. Thanks for the help!
– Greg
53 mins ago
1
1
It gives an empty string at the end.
– AkshayNevrekar
1 hour ago
It gives an empty string at the end.
– AkshayNevrekar
1 hour ago
2
2
@AkshayNevrekar With
splitter = filter(None, splitter)
(as in the original post), it does not.– DYZ
59 mins ago
@AkshayNevrekar With
splitter = filter(None, splitter)
(as in the original post), it does not.– DYZ
59 mins ago
This works for me. Thanks for the help!
– Greg
53 mins ago
This works for me. Thanks for the help!
– Greg
53 mins ago
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
52 mins ago
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
52 mins ago
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 49 mins ago
answered 1 hour ago
Ajax1234Ajax1234
41.4k42853
41.4k42853
This one also does the job. Thanks for the help!
– Greg
52 mins ago
add a comment |
This one also does the job. Thanks for the help!
– Greg
52 mins ago
This one also does the job. Thanks for the help!
– Greg
52 mins ago
This one also does the job. Thanks for the help!
– Greg
52 mins ago
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 57 mins ago
Bodhi94Bodhi94
926520
926520
add a comment |
add a comment |
Greg is a new contributor. Be nice, and check out our Code of Conduct.
Greg is a new contributor. Be nice, and check out our Code of Conduct.
Greg is a new contributor. Be nice, and check out our Code of Conduct.
Greg is a new contributor. Be nice, and check out our Code of Conduct.
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