Removing a 'range' of characters at specific indexes in string












2















I have looked over similar questions, but I still have trouble figuring this one out.



I have two Lists of strings, one of which consists of characters like 'abcdefg' and another one consisting of strings which consist of white spaces and a special character. The special character indicates where I should remove characters from my 'abcdefg' string. The special character's position in the list would be the same position I would need to remove a character from the first list. I also need to remove the adjacent characters.



EDIT: I want to remove a character (and the adjacent characters) at the same position the '*' char is located in airstrikes, but in reinforces. Does this make sense?



reinforces = ["abcdefg", "hijklmn"]
airstrikes = [" * "]
battlefield = reinforces[0]
bomb_range =
count = 0

if range(len(airstrikes)) != 0:
for airstrike in airstrikes:
for char in airstrike:
print(count)
count = count + 1
if (char == '*'):
bomb_range.append(count-1)
bomb_range.append(count)
bomb_range.append(count+1)
break


#Trying to hardcode it initially just to get it to work. Some kind of looping is needed though.
battlefield = battlefield[:bomb_range[0]] + battlefield[bomb_range[1]:]
battlefield = battlefield[:bomb_range[1]] + battlefield[bomb_range[2]:]
#battlefield = battlefield[:bomb_range[2]] + battlefield[bomb_range[3]:] #Will not work of course. But how could I achieve what I want?


I am sorry about the nested loops. If it hurts looking at it, feel free to bash and correct me. I am sorry if I missed any answers on this forum which could have helped me find a solution. Know that I did try to find one.










share|improve this question

























  • I see no question in your full text. What do you want to know?

    – feliks
    Nov 14 '18 at 20:54











  • It is completely unclear what you want to do. How, exactly, does the content in airstrikes tell you how to transform reinforces? Show examples of the results you would expect if your transform was successful; given the examples here.

    – jdv
    Nov 14 '18 at 20:56













  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Since you haven't specified a problem, we're at a loss.

    – Prune
    Nov 14 '18 at 20:57











  • I have updated the description. Hope it helps

    – DonutSteve
    Nov 14 '18 at 20:58
















2















I have looked over similar questions, but I still have trouble figuring this one out.



I have two Lists of strings, one of which consists of characters like 'abcdefg' and another one consisting of strings which consist of white spaces and a special character. The special character indicates where I should remove characters from my 'abcdefg' string. The special character's position in the list would be the same position I would need to remove a character from the first list. I also need to remove the adjacent characters.



EDIT: I want to remove a character (and the adjacent characters) at the same position the '*' char is located in airstrikes, but in reinforces. Does this make sense?



reinforces = ["abcdefg", "hijklmn"]
airstrikes = [" * "]
battlefield = reinforces[0]
bomb_range =
count = 0

if range(len(airstrikes)) != 0:
for airstrike in airstrikes:
for char in airstrike:
print(count)
count = count + 1
if (char == '*'):
bomb_range.append(count-1)
bomb_range.append(count)
bomb_range.append(count+1)
break


#Trying to hardcode it initially just to get it to work. Some kind of looping is needed though.
battlefield = battlefield[:bomb_range[0]] + battlefield[bomb_range[1]:]
battlefield = battlefield[:bomb_range[1]] + battlefield[bomb_range[2]:]
#battlefield = battlefield[:bomb_range[2]] + battlefield[bomb_range[3]:] #Will not work of course. But how could I achieve what I want?


I am sorry about the nested loops. If it hurts looking at it, feel free to bash and correct me. I am sorry if I missed any answers on this forum which could have helped me find a solution. Know that I did try to find one.










share|improve this question

























  • I see no question in your full text. What do you want to know?

    – feliks
    Nov 14 '18 at 20:54











  • It is completely unclear what you want to do. How, exactly, does the content in airstrikes tell you how to transform reinforces? Show examples of the results you would expect if your transform was successful; given the examples here.

    – jdv
    Nov 14 '18 at 20:56













  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Since you haven't specified a problem, we're at a loss.

    – Prune
    Nov 14 '18 at 20:57











  • I have updated the description. Hope it helps

    – DonutSteve
    Nov 14 '18 at 20:58














2












2








2








I have looked over similar questions, but I still have trouble figuring this one out.



I have two Lists of strings, one of which consists of characters like 'abcdefg' and another one consisting of strings which consist of white spaces and a special character. The special character indicates where I should remove characters from my 'abcdefg' string. The special character's position in the list would be the same position I would need to remove a character from the first list. I also need to remove the adjacent characters.



EDIT: I want to remove a character (and the adjacent characters) at the same position the '*' char is located in airstrikes, but in reinforces. Does this make sense?



reinforces = ["abcdefg", "hijklmn"]
airstrikes = [" * "]
battlefield = reinforces[0]
bomb_range =
count = 0

if range(len(airstrikes)) != 0:
for airstrike in airstrikes:
for char in airstrike:
print(count)
count = count + 1
if (char == '*'):
bomb_range.append(count-1)
bomb_range.append(count)
bomb_range.append(count+1)
break


#Trying to hardcode it initially just to get it to work. Some kind of looping is needed though.
battlefield = battlefield[:bomb_range[0]] + battlefield[bomb_range[1]:]
battlefield = battlefield[:bomb_range[1]] + battlefield[bomb_range[2]:]
#battlefield = battlefield[:bomb_range[2]] + battlefield[bomb_range[3]:] #Will not work of course. But how could I achieve what I want?


I am sorry about the nested loops. If it hurts looking at it, feel free to bash and correct me. I am sorry if I missed any answers on this forum which could have helped me find a solution. Know that I did try to find one.










share|improve this question
















I have looked over similar questions, but I still have trouble figuring this one out.



I have two Lists of strings, one of which consists of characters like 'abcdefg' and another one consisting of strings which consist of white spaces and a special character. The special character indicates where I should remove characters from my 'abcdefg' string. The special character's position in the list would be the same position I would need to remove a character from the first list. I also need to remove the adjacent characters.



EDIT: I want to remove a character (and the adjacent characters) at the same position the '*' char is located in airstrikes, but in reinforces. Does this make sense?



reinforces = ["abcdefg", "hijklmn"]
airstrikes = [" * "]
battlefield = reinforces[0]
bomb_range =
count = 0

if range(len(airstrikes)) != 0:
for airstrike in airstrikes:
for char in airstrike:
print(count)
count = count + 1
if (char == '*'):
bomb_range.append(count-1)
bomb_range.append(count)
bomb_range.append(count+1)
break


#Trying to hardcode it initially just to get it to work. Some kind of looping is needed though.
battlefield = battlefield[:bomb_range[0]] + battlefield[bomb_range[1]:]
battlefield = battlefield[:bomb_range[1]] + battlefield[bomb_range[2]:]
#battlefield = battlefield[:bomb_range[2]] + battlefield[bomb_range[3]:] #Will not work of course. But how could I achieve what I want?


I am sorry about the nested loops. If it hurts looking at it, feel free to bash and correct me. I am sorry if I missed any answers on this forum which could have helped me find a solution. Know that I did try to find one.







python string list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 20:57







DonutSteve

















asked Nov 14 '18 at 20:49









DonutSteveDonutSteve

779




779













  • I see no question in your full text. What do you want to know?

    – feliks
    Nov 14 '18 at 20:54











  • It is completely unclear what you want to do. How, exactly, does the content in airstrikes tell you how to transform reinforces? Show examples of the results you would expect if your transform was successful; given the examples here.

    – jdv
    Nov 14 '18 at 20:56













  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Since you haven't specified a problem, we're at a loss.

    – Prune
    Nov 14 '18 at 20:57











  • I have updated the description. Hope it helps

    – DonutSteve
    Nov 14 '18 at 20:58



















  • I see no question in your full text. What do you want to know?

    – feliks
    Nov 14 '18 at 20:54











  • It is completely unclear what you want to do. How, exactly, does the content in airstrikes tell you how to transform reinforces? Show examples of the results you would expect if your transform was successful; given the examples here.

    – jdv
    Nov 14 '18 at 20:56













  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Since you haven't specified a problem, we're at a loss.

    – Prune
    Nov 14 '18 at 20:57











  • I have updated the description. Hope it helps

    – DonutSteve
    Nov 14 '18 at 20:58

















I see no question in your full text. What do you want to know?

– feliks
Nov 14 '18 at 20:54





I see no question in your full text. What do you want to know?

– feliks
Nov 14 '18 at 20:54













It is completely unclear what you want to do. How, exactly, does the content in airstrikes tell you how to transform reinforces? Show examples of the results you would expect if your transform was successful; given the examples here.

– jdv
Nov 14 '18 at 20:56







It is completely unclear what you want to do. How, exactly, does the content in airstrikes tell you how to transform reinforces? Show examples of the results you would expect if your transform was successful; given the examples here.

– jdv
Nov 14 '18 at 20:56















Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Since you haven't specified a problem, we're at a loss.

– Prune
Nov 14 '18 at 20:57





Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Since you haven't specified a problem, we're at a loss.

– Prune
Nov 14 '18 at 20:57













I have updated the description. Hope it helps

– DonutSteve
Nov 14 '18 at 20:58





I have updated the description. Hope it helps

– DonutSteve
Nov 14 '18 at 20:58












1 Answer
1






active

oldest

votes


















2














Use index to find where to strike, then remove the character the usual way:



>>> reinforce = "abcdefg"
>>> airstrike = " * "
>>> strike_at = airstrike.index('*')
>>> reinforce[:strike_at]+reinforce[strike_at+1:]
'abcefg'


of course, you need to make sure strike_at+1 is a legal index (see try and except).






share|improve this answer
























  • I thought I needed to do some kind of error handling to check if it was out of bounds. Index seems like a smart solution. I will try it out

    – DonutSteve
    Nov 14 '18 at 21:01











  • @DonutSteve You do. That is why I suggested googling/searching try/except. You could compare to len(airstrike), but that is less idiomatic in Python.

    – kabanus
    Nov 14 '18 at 21:04











  • Oh yeah, I was just stating I had thought about it. So it is nice to see I was on the right track

    – DonutSteve
    Nov 14 '18 at 21:09











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308507%2fremoving-a-range-of-characters-at-specific-indexes-in-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Use index to find where to strike, then remove the character the usual way:



>>> reinforce = "abcdefg"
>>> airstrike = " * "
>>> strike_at = airstrike.index('*')
>>> reinforce[:strike_at]+reinforce[strike_at+1:]
'abcefg'


of course, you need to make sure strike_at+1 is a legal index (see try and except).






share|improve this answer
























  • I thought I needed to do some kind of error handling to check if it was out of bounds. Index seems like a smart solution. I will try it out

    – DonutSteve
    Nov 14 '18 at 21:01











  • @DonutSteve You do. That is why I suggested googling/searching try/except. You could compare to len(airstrike), but that is less idiomatic in Python.

    – kabanus
    Nov 14 '18 at 21:04











  • Oh yeah, I was just stating I had thought about it. So it is nice to see I was on the right track

    – DonutSteve
    Nov 14 '18 at 21:09
















2














Use index to find where to strike, then remove the character the usual way:



>>> reinforce = "abcdefg"
>>> airstrike = " * "
>>> strike_at = airstrike.index('*')
>>> reinforce[:strike_at]+reinforce[strike_at+1:]
'abcefg'


of course, you need to make sure strike_at+1 is a legal index (see try and except).






share|improve this answer
























  • I thought I needed to do some kind of error handling to check if it was out of bounds. Index seems like a smart solution. I will try it out

    – DonutSteve
    Nov 14 '18 at 21:01











  • @DonutSteve You do. That is why I suggested googling/searching try/except. You could compare to len(airstrike), but that is less idiomatic in Python.

    – kabanus
    Nov 14 '18 at 21:04











  • Oh yeah, I was just stating I had thought about it. So it is nice to see I was on the right track

    – DonutSteve
    Nov 14 '18 at 21:09














2












2








2







Use index to find where to strike, then remove the character the usual way:



>>> reinforce = "abcdefg"
>>> airstrike = " * "
>>> strike_at = airstrike.index('*')
>>> reinforce[:strike_at]+reinforce[strike_at+1:]
'abcefg'


of course, you need to make sure strike_at+1 is a legal index (see try and except).






share|improve this answer













Use index to find where to strike, then remove the character the usual way:



>>> reinforce = "abcdefg"
>>> airstrike = " * "
>>> strike_at = airstrike.index('*')
>>> reinforce[:strike_at]+reinforce[strike_at+1:]
'abcefg'


of course, you need to make sure strike_at+1 is a legal index (see try and except).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 20:59









kabanuskabanus

11.6k31339




11.6k31339













  • I thought I needed to do some kind of error handling to check if it was out of bounds. Index seems like a smart solution. I will try it out

    – DonutSteve
    Nov 14 '18 at 21:01











  • @DonutSteve You do. That is why I suggested googling/searching try/except. You could compare to len(airstrike), but that is less idiomatic in Python.

    – kabanus
    Nov 14 '18 at 21:04











  • Oh yeah, I was just stating I had thought about it. So it is nice to see I was on the right track

    – DonutSteve
    Nov 14 '18 at 21:09



















  • I thought I needed to do some kind of error handling to check if it was out of bounds. Index seems like a smart solution. I will try it out

    – DonutSteve
    Nov 14 '18 at 21:01











  • @DonutSteve You do. That is why I suggested googling/searching try/except. You could compare to len(airstrike), but that is less idiomatic in Python.

    – kabanus
    Nov 14 '18 at 21:04











  • Oh yeah, I was just stating I had thought about it. So it is nice to see I was on the right track

    – DonutSteve
    Nov 14 '18 at 21:09

















I thought I needed to do some kind of error handling to check if it was out of bounds. Index seems like a smart solution. I will try it out

– DonutSteve
Nov 14 '18 at 21:01





I thought I needed to do some kind of error handling to check if it was out of bounds. Index seems like a smart solution. I will try it out

– DonutSteve
Nov 14 '18 at 21:01













@DonutSteve You do. That is why I suggested googling/searching try/except. You could compare to len(airstrike), but that is less idiomatic in Python.

– kabanus
Nov 14 '18 at 21:04





@DonutSteve You do. That is why I suggested googling/searching try/except. You could compare to len(airstrike), but that is less idiomatic in Python.

– kabanus
Nov 14 '18 at 21:04













Oh yeah, I was just stating I had thought about it. So it is nice to see I was on the right track

– DonutSteve
Nov 14 '18 at 21:09





Oh yeah, I was just stating I had thought about it. So it is nice to see I was on the right track

– DonutSteve
Nov 14 '18 at 21:09




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308507%2fremoving-a-range-of-characters-at-specific-indexes-in-string%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly