Remove number from list in which difference between two number are less then specific number
a = [2, 5, 6, 12, 21, 25, 32, 41]
This is my list
and I want to remove all the numbers which are not in difference of 7.
python-3.x
add a comment |
a = [2, 5, 6, 12, 21, 25, 32, 41]
This is my list
and I want to remove all the numbers which are not in difference of 7.
python-3.x
1
Can you please share the output of the above list.
– Mayank Bansal
Jan 2 at 12:32
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:26
add a comment |
a = [2, 5, 6, 12, 21, 25, 32, 41]
This is my list
and I want to remove all the numbers which are not in difference of 7.
python-3.x
a = [2, 5, 6, 12, 21, 25, 32, 41]
This is my list
and I want to remove all the numbers which are not in difference of 7.
python-3.x
python-3.x
edited Jan 2 at 15:12


Diptangsu Goswami
782921
782921
asked Jan 2 at 12:19
Manohar SonwanManohar Sonwan
34
34
1
Can you please share the output of the above list.
– Mayank Bansal
Jan 2 at 12:32
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:26
add a comment |
1
Can you please share the output of the above list.
– Mayank Bansal
Jan 2 at 12:32
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:26
1
1
Can you please share the output of the above list.
– Mayank Bansal
Jan 2 at 12:32
Can you please share the output of the above list.
– Mayank Bansal
Jan 2 at 12:32
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:26
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:26
add a comment |
2 Answers
2
active
oldest
votes
Before diving into the answers, let's go over what we're dealing with.
You got a list 'A', which you need to loop through to get a list of numbers that are greater than the previous value + 7
If you boil down the question, you get left with two main goals
- We need a loop
- and we have a list with the final answer
There are two generic ways of approaching this question. In a loop, we populate a new list. The second way is to manipulate the original list.
Although the First approach requires additional memory, I'll be using the First approach for simplicity.
a = [2, 5, 6, 12, 21, 25, 32, 41] # your original list
b = # Empty list that will contain final product
for i in range(len(a)):
if len(b) == 0: # if the list is empty, we add first item from 'a' (In our example, it'll be 2)
b.append(a[i])
else:
if a[i] > b[len(b)-1]+7 or a[i] < b[len(b)-1]-7: # for every value of a, we compare the last digit from list b
b.append(a[i])
thank you, it was very helpful
– Manohar Sonwan
Jan 9 at 12:28
add a comment |
As far as I have understood your question, in your output list, only those elements should be there whose sum is 7. So that can be achieved by
i=1;
while i<len(a):
if(a[i]-a[i-1] < 7):
a.remove(a[i])
else:
i+=1
print(a)
This is I want the output:
– Manohar Sonwan
Jan 2 at 13:21
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:22
@ManoharSonwan I have updated my Answer.
– Mayank Bansal
Jan 2 at 13:41
My question is, a difference between any two number should be greater than 7.
– Manohar Sonwan
Jan 2 at 13:51
this gives output [2, 6, 12, 21, 32, 41], here difference between 2, 6 is 4 and 6 to 12 is 6
– Manohar Sonwan
Jan 2 at 13:52
|
show 4 more comments
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%2f54006266%2fremove-number-from-list-in-which-difference-between-two-number-are-less-then-spe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Before diving into the answers, let's go over what we're dealing with.
You got a list 'A', which you need to loop through to get a list of numbers that are greater than the previous value + 7
If you boil down the question, you get left with two main goals
- We need a loop
- and we have a list with the final answer
There are two generic ways of approaching this question. In a loop, we populate a new list. The second way is to manipulate the original list.
Although the First approach requires additional memory, I'll be using the First approach for simplicity.
a = [2, 5, 6, 12, 21, 25, 32, 41] # your original list
b = # Empty list that will contain final product
for i in range(len(a)):
if len(b) == 0: # if the list is empty, we add first item from 'a' (In our example, it'll be 2)
b.append(a[i])
else:
if a[i] > b[len(b)-1]+7 or a[i] < b[len(b)-1]-7: # for every value of a, we compare the last digit from list b
b.append(a[i])
thank you, it was very helpful
– Manohar Sonwan
Jan 9 at 12:28
add a comment |
Before diving into the answers, let's go over what we're dealing with.
You got a list 'A', which you need to loop through to get a list of numbers that are greater than the previous value + 7
If you boil down the question, you get left with two main goals
- We need a loop
- and we have a list with the final answer
There are two generic ways of approaching this question. In a loop, we populate a new list. The second way is to manipulate the original list.
Although the First approach requires additional memory, I'll be using the First approach for simplicity.
a = [2, 5, 6, 12, 21, 25, 32, 41] # your original list
b = # Empty list that will contain final product
for i in range(len(a)):
if len(b) == 0: # if the list is empty, we add first item from 'a' (In our example, it'll be 2)
b.append(a[i])
else:
if a[i] > b[len(b)-1]+7 or a[i] < b[len(b)-1]-7: # for every value of a, we compare the last digit from list b
b.append(a[i])
thank you, it was very helpful
– Manohar Sonwan
Jan 9 at 12:28
add a comment |
Before diving into the answers, let's go over what we're dealing with.
You got a list 'A', which you need to loop through to get a list of numbers that are greater than the previous value + 7
If you boil down the question, you get left with two main goals
- We need a loop
- and we have a list with the final answer
There are two generic ways of approaching this question. In a loop, we populate a new list. The second way is to manipulate the original list.
Although the First approach requires additional memory, I'll be using the First approach for simplicity.
a = [2, 5, 6, 12, 21, 25, 32, 41] # your original list
b = # Empty list that will contain final product
for i in range(len(a)):
if len(b) == 0: # if the list is empty, we add first item from 'a' (In our example, it'll be 2)
b.append(a[i])
else:
if a[i] > b[len(b)-1]+7 or a[i] < b[len(b)-1]-7: # for every value of a, we compare the last digit from list b
b.append(a[i])
Before diving into the answers, let's go over what we're dealing with.
You got a list 'A', which you need to loop through to get a list of numbers that are greater than the previous value + 7
If you boil down the question, you get left with two main goals
- We need a loop
- and we have a list with the final answer
There are two generic ways of approaching this question. In a loop, we populate a new list. The second way is to manipulate the original list.
Although the First approach requires additional memory, I'll be using the First approach for simplicity.
a = [2, 5, 6, 12, 21, 25, 32, 41] # your original list
b = # Empty list that will contain final product
for i in range(len(a)):
if len(b) == 0: # if the list is empty, we add first item from 'a' (In our example, it'll be 2)
b.append(a[i])
else:
if a[i] > b[len(b)-1]+7 or a[i] < b[len(b)-1]-7: # for every value of a, we compare the last digit from list b
b.append(a[i])
edited Jan 10 at 13:04
answered Jan 2 at 14:58


HannestHannest
513
513
thank you, it was very helpful
– Manohar Sonwan
Jan 9 at 12:28
add a comment |
thank you, it was very helpful
– Manohar Sonwan
Jan 9 at 12:28
thank you, it was very helpful
– Manohar Sonwan
Jan 9 at 12:28
thank you, it was very helpful
– Manohar Sonwan
Jan 9 at 12:28
add a comment |
As far as I have understood your question, in your output list, only those elements should be there whose sum is 7. So that can be achieved by
i=1;
while i<len(a):
if(a[i]-a[i-1] < 7):
a.remove(a[i])
else:
i+=1
print(a)
This is I want the output:
– Manohar Sonwan
Jan 2 at 13:21
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:22
@ManoharSonwan I have updated my Answer.
– Mayank Bansal
Jan 2 at 13:41
My question is, a difference between any two number should be greater than 7.
– Manohar Sonwan
Jan 2 at 13:51
this gives output [2, 6, 12, 21, 32, 41], here difference between 2, 6 is 4 and 6 to 12 is 6
– Manohar Sonwan
Jan 2 at 13:52
|
show 4 more comments
As far as I have understood your question, in your output list, only those elements should be there whose sum is 7. So that can be achieved by
i=1;
while i<len(a):
if(a[i]-a[i-1] < 7):
a.remove(a[i])
else:
i+=1
print(a)
This is I want the output:
– Manohar Sonwan
Jan 2 at 13:21
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:22
@ManoharSonwan I have updated my Answer.
– Mayank Bansal
Jan 2 at 13:41
My question is, a difference between any two number should be greater than 7.
– Manohar Sonwan
Jan 2 at 13:51
this gives output [2, 6, 12, 21, 32, 41], here difference between 2, 6 is 4 and 6 to 12 is 6
– Manohar Sonwan
Jan 2 at 13:52
|
show 4 more comments
As far as I have understood your question, in your output list, only those elements should be there whose sum is 7. So that can be achieved by
i=1;
while i<len(a):
if(a[i]-a[i-1] < 7):
a.remove(a[i])
else:
i+=1
print(a)
As far as I have understood your question, in your output list, only those elements should be there whose sum is 7. So that can be achieved by
i=1;
while i<len(a):
if(a[i]-a[i-1] < 7):
a.remove(a[i])
else:
i+=1
print(a)
edited Jan 2 at 14:24
answered Jan 2 at 12:52
Mayank BansalMayank Bansal
408215
408215
This is I want the output:
– Manohar Sonwan
Jan 2 at 13:21
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:22
@ManoharSonwan I have updated my Answer.
– Mayank Bansal
Jan 2 at 13:41
My question is, a difference between any two number should be greater than 7.
– Manohar Sonwan
Jan 2 at 13:51
this gives output [2, 6, 12, 21, 32, 41], here difference between 2, 6 is 4 and 6 to 12 is 6
– Manohar Sonwan
Jan 2 at 13:52
|
show 4 more comments
This is I want the output:
– Manohar Sonwan
Jan 2 at 13:21
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:22
@ManoharSonwan I have updated my Answer.
– Mayank Bansal
Jan 2 at 13:41
My question is, a difference between any two number should be greater than 7.
– Manohar Sonwan
Jan 2 at 13:51
this gives output [2, 6, 12, 21, 32, 41], here difference between 2, 6 is 4 and 6 to 12 is 6
– Manohar Sonwan
Jan 2 at 13:52
This is I want the output:
– Manohar Sonwan
Jan 2 at 13:21
This is I want the output:
– Manohar Sonwan
Jan 2 at 13:21
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:22
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:22
@ManoharSonwan I have updated my Answer.
– Mayank Bansal
Jan 2 at 13:41
@ManoharSonwan I have updated my Answer.
– Mayank Bansal
Jan 2 at 13:41
My question is, a difference between any two number should be greater than 7.
– Manohar Sonwan
Jan 2 at 13:51
My question is, a difference between any two number should be greater than 7.
– Manohar Sonwan
Jan 2 at 13:51
this gives output [2, 6, 12, 21, 32, 41], here difference between 2, 6 is 4 and 6 to 12 is 6
– Manohar Sonwan
Jan 2 at 13:52
this gives output [2, 6, 12, 21, 32, 41], here difference between 2, 6 is 4 and 6 to 12 is 6
– Manohar Sonwan
Jan 2 at 13:52
|
show 4 more comments
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%2f54006266%2fremove-number-from-list-in-which-difference-between-two-number-are-less-then-spe%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
1
Can you please share the output of the above list.
– Mayank Bansal
Jan 2 at 12:32
This is I want the output: a = [2, 12, 21,32,41]
– Manohar Sonwan
Jan 2 at 13:26