How to slice multi dimensional list in python under certain condition?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I want to slice a multidimensional list under certain condition. I have a sensor which gives a pair of (quality, angle, distance) as a multidimensional list.
ex.
a = [(10,0,3),(10,10,6),(10,15,4),(10,20,5),(10,3,3),(10,5,6)]
now if the distance is greater than 5 from that point I need to detect the angle also. Now within the 10-degree+ angle, I need to slice the array, doesn't matter what the distance is.
so my result would be:
b= [(10,10,6),(10,15,4),(10,20,5)]
as the distance is 6 and the angle range is between 10 to 10+10=20.
I will be so glad if you could give me an idea how to find the index of that particular list which fulfills the condition, so that i can sliec the list.
python
add a comment |
I want to slice a multidimensional list under certain condition. I have a sensor which gives a pair of (quality, angle, distance) as a multidimensional list.
ex.
a = [(10,0,3),(10,10,6),(10,15,4),(10,20,5),(10,3,3),(10,5,6)]
now if the distance is greater than 5 from that point I need to detect the angle also. Now within the 10-degree+ angle, I need to slice the array, doesn't matter what the distance is.
so my result would be:
b= [(10,10,6),(10,15,4),(10,20,5)]
as the distance is 6 and the angle range is between 10 to 10+10=20.
I will be so glad if you could give me an idea how to find the index of that particular list which fulfills the condition, so that i can sliec the list.
python
2
"if the distance is greater then 5 from that point " what point?
– Daniel Mesejo
Jan 3 at 15:24
as you can see the 2nd value is 10,10,6 (quality,angle,distance). so distance is greater than 5. so i have take the value from angle 10 to 20 @DanielMesejo
– Kazi
Jan 3 at 15:30
1
please clarify a few examples of your desired output, because this is not clear at all
– Josh Friedlander
Jan 3 at 15:34
If the last value of the tuple is the distance, why do you take( 10, 15, 4)?
– Daniel Mesejo
Jan 3 at 15:34
because my concern is whenever I have found the value which is greater than 5, then from that point I will see the angle and slice the array within 10 plus angle. @DanielMesejo
– Kazi
Jan 3 at 15:38
add a comment |
I want to slice a multidimensional list under certain condition. I have a sensor which gives a pair of (quality, angle, distance) as a multidimensional list.
ex.
a = [(10,0,3),(10,10,6),(10,15,4),(10,20,5),(10,3,3),(10,5,6)]
now if the distance is greater than 5 from that point I need to detect the angle also. Now within the 10-degree+ angle, I need to slice the array, doesn't matter what the distance is.
so my result would be:
b= [(10,10,6),(10,15,4),(10,20,5)]
as the distance is 6 and the angle range is between 10 to 10+10=20.
I will be so glad if you could give me an idea how to find the index of that particular list which fulfills the condition, so that i can sliec the list.
python
I want to slice a multidimensional list under certain condition. I have a sensor which gives a pair of (quality, angle, distance) as a multidimensional list.
ex.
a = [(10,0,3),(10,10,6),(10,15,4),(10,20,5),(10,3,3),(10,5,6)]
now if the distance is greater than 5 from that point I need to detect the angle also. Now within the 10-degree+ angle, I need to slice the array, doesn't matter what the distance is.
so my result would be:
b= [(10,10,6),(10,15,4),(10,20,5)]
as the distance is 6 and the angle range is between 10 to 10+10=20.
I will be so glad if you could give me an idea how to find the index of that particular list which fulfills the condition, so that i can sliec the list.
python
python
edited Jan 3 at 15:50
Kazi
asked Jan 3 at 15:11


KaziKazi
144112
144112
2
"if the distance is greater then 5 from that point " what point?
– Daniel Mesejo
Jan 3 at 15:24
as you can see the 2nd value is 10,10,6 (quality,angle,distance). so distance is greater than 5. so i have take the value from angle 10 to 20 @DanielMesejo
– Kazi
Jan 3 at 15:30
1
please clarify a few examples of your desired output, because this is not clear at all
– Josh Friedlander
Jan 3 at 15:34
If the last value of the tuple is the distance, why do you take( 10, 15, 4)?
– Daniel Mesejo
Jan 3 at 15:34
because my concern is whenever I have found the value which is greater than 5, then from that point I will see the angle and slice the array within 10 plus angle. @DanielMesejo
– Kazi
Jan 3 at 15:38
add a comment |
2
"if the distance is greater then 5 from that point " what point?
– Daniel Mesejo
Jan 3 at 15:24
as you can see the 2nd value is 10,10,6 (quality,angle,distance). so distance is greater than 5. so i have take the value from angle 10 to 20 @DanielMesejo
– Kazi
Jan 3 at 15:30
1
please clarify a few examples of your desired output, because this is not clear at all
– Josh Friedlander
Jan 3 at 15:34
If the last value of the tuple is the distance, why do you take( 10, 15, 4)?
– Daniel Mesejo
Jan 3 at 15:34
because my concern is whenever I have found the value which is greater than 5, then from that point I will see the angle and slice the array within 10 plus angle. @DanielMesejo
– Kazi
Jan 3 at 15:38
2
2
"if the distance is greater then 5 from that point " what point?
– Daniel Mesejo
Jan 3 at 15:24
"if the distance is greater then 5 from that point " what point?
– Daniel Mesejo
Jan 3 at 15:24
as you can see the 2nd value is 10,10,6 (quality,angle,distance). so distance is greater than 5. so i have take the value from angle 10 to 20 @DanielMesejo
– Kazi
Jan 3 at 15:30
as you can see the 2nd value is 10,10,6 (quality,angle,distance). so distance is greater than 5. so i have take the value from angle 10 to 20 @DanielMesejo
– Kazi
Jan 3 at 15:30
1
1
please clarify a few examples of your desired output, because this is not clear at all
– Josh Friedlander
Jan 3 at 15:34
please clarify a few examples of your desired output, because this is not clear at all
– Josh Friedlander
Jan 3 at 15:34
If the last value of the tuple is the distance, why do you take( 10, 15, 4)?
– Daniel Mesejo
Jan 3 at 15:34
If the last value of the tuple is the distance, why do you take( 10, 15, 4)?
– Daniel Mesejo
Jan 3 at 15:34
because my concern is whenever I have found the value which is greater than 5, then from that point I will see the angle and slice the array within 10 plus angle. @DanielMesejo
– Kazi
Jan 3 at 15:38
because my concern is whenever I have found the value which is greater than 5, then from that point I will see the angle and slice the array within 10 plus angle. @DanielMesejo
– Kazi
Jan 3 at 15:38
add a comment |
2 Answers
2
active
oldest
votes
You could write a function (take
) like this:
a = [(10, 0, 3), (10, 10, 6), (10, 15, 4), (10, 20, 5), (10, 3, 3), (10, 5, 6)]
def take(lst, th=5):
idx = next(i for i, e in enumerate(lst) if e[2] > th) # get the index of the first with distance > th
quality, angle, distance = lst[idx] # unpack in quality, angle, distance
return [e for e in lst[idx:] if angle <= e[1] <= angle + 10] # filter the list starting from idx
result = take(a)
print(result)
Output
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
thank you so much....
– Kazi
Jan 3 at 15:49
2
Nice solution @daniel! you have my +1
– yatu
Jan 3 at 15:51
Thanks you @yatu!
– Daniel Mesejo
Jan 3 at 15:54
add a comment |
If using Pandas
is an option, here is a way to do it:
i = 5
j = 10
df = pd.DataFrame(a, columns = ('quality', 'angle', 'distance'))
print(df)
quality angle distance
0 10 0 3
1 10 10 6
2 10 15 4
3 10 20 5
4 10 3 3
5 10 5 6
Here ix1
is the index of the first occurrence of the first condition on the distance
, and ix2
the index of the last succeeding rows that fullfil the condition imposed on angle
:
ix1 = df[df['distance'] > i].iloc[0].name
ix2 = (~(df.loc[ix1:, 'angle'] >= j)).idxmax()-1
l = df.loc[ix1:ix2,:]
list(l.to_records(index=False))
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
thank you so much.
– Kazi
Jan 3 at 15:49
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%2f54024998%2fhow-to-slice-multi-dimensional-list-in-python-under-certain-condition%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
You could write a function (take
) like this:
a = [(10, 0, 3), (10, 10, 6), (10, 15, 4), (10, 20, 5), (10, 3, 3), (10, 5, 6)]
def take(lst, th=5):
idx = next(i for i, e in enumerate(lst) if e[2] > th) # get the index of the first with distance > th
quality, angle, distance = lst[idx] # unpack in quality, angle, distance
return [e for e in lst[idx:] if angle <= e[1] <= angle + 10] # filter the list starting from idx
result = take(a)
print(result)
Output
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
thank you so much....
– Kazi
Jan 3 at 15:49
2
Nice solution @daniel! you have my +1
– yatu
Jan 3 at 15:51
Thanks you @yatu!
– Daniel Mesejo
Jan 3 at 15:54
add a comment |
You could write a function (take
) like this:
a = [(10, 0, 3), (10, 10, 6), (10, 15, 4), (10, 20, 5), (10, 3, 3), (10, 5, 6)]
def take(lst, th=5):
idx = next(i for i, e in enumerate(lst) if e[2] > th) # get the index of the first with distance > th
quality, angle, distance = lst[idx] # unpack in quality, angle, distance
return [e for e in lst[idx:] if angle <= e[1] <= angle + 10] # filter the list starting from idx
result = take(a)
print(result)
Output
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
thank you so much....
– Kazi
Jan 3 at 15:49
2
Nice solution @daniel! you have my +1
– yatu
Jan 3 at 15:51
Thanks you @yatu!
– Daniel Mesejo
Jan 3 at 15:54
add a comment |
You could write a function (take
) like this:
a = [(10, 0, 3), (10, 10, 6), (10, 15, 4), (10, 20, 5), (10, 3, 3), (10, 5, 6)]
def take(lst, th=5):
idx = next(i for i, e in enumerate(lst) if e[2] > th) # get the index of the first with distance > th
quality, angle, distance = lst[idx] # unpack in quality, angle, distance
return [e for e in lst[idx:] if angle <= e[1] <= angle + 10] # filter the list starting from idx
result = take(a)
print(result)
Output
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
You could write a function (take
) like this:
a = [(10, 0, 3), (10, 10, 6), (10, 15, 4), (10, 20, 5), (10, 3, 3), (10, 5, 6)]
def take(lst, th=5):
idx = next(i for i, e in enumerate(lst) if e[2] > th) # get the index of the first with distance > th
quality, angle, distance = lst[idx] # unpack in quality, angle, distance
return [e for e in lst[idx:] if angle <= e[1] <= angle + 10] # filter the list starting from idx
result = take(a)
print(result)
Output
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
answered Jan 3 at 15:45


Daniel MesejoDaniel Mesejo
18.8k21533
18.8k21533
thank you so much....
– Kazi
Jan 3 at 15:49
2
Nice solution @daniel! you have my +1
– yatu
Jan 3 at 15:51
Thanks you @yatu!
– Daniel Mesejo
Jan 3 at 15:54
add a comment |
thank you so much....
– Kazi
Jan 3 at 15:49
2
Nice solution @daniel! you have my +1
– yatu
Jan 3 at 15:51
Thanks you @yatu!
– Daniel Mesejo
Jan 3 at 15:54
thank you so much....
– Kazi
Jan 3 at 15:49
thank you so much....
– Kazi
Jan 3 at 15:49
2
2
Nice solution @daniel! you have my +1
– yatu
Jan 3 at 15:51
Nice solution @daniel! you have my +1
– yatu
Jan 3 at 15:51
Thanks you @yatu!
– Daniel Mesejo
Jan 3 at 15:54
Thanks you @yatu!
– Daniel Mesejo
Jan 3 at 15:54
add a comment |
If using Pandas
is an option, here is a way to do it:
i = 5
j = 10
df = pd.DataFrame(a, columns = ('quality', 'angle', 'distance'))
print(df)
quality angle distance
0 10 0 3
1 10 10 6
2 10 15 4
3 10 20 5
4 10 3 3
5 10 5 6
Here ix1
is the index of the first occurrence of the first condition on the distance
, and ix2
the index of the last succeeding rows that fullfil the condition imposed on angle
:
ix1 = df[df['distance'] > i].iloc[0].name
ix2 = (~(df.loc[ix1:, 'angle'] >= j)).idxmax()-1
l = df.loc[ix1:ix2,:]
list(l.to_records(index=False))
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
thank you so much.
– Kazi
Jan 3 at 15:49
add a comment |
If using Pandas
is an option, here is a way to do it:
i = 5
j = 10
df = pd.DataFrame(a, columns = ('quality', 'angle', 'distance'))
print(df)
quality angle distance
0 10 0 3
1 10 10 6
2 10 15 4
3 10 20 5
4 10 3 3
5 10 5 6
Here ix1
is the index of the first occurrence of the first condition on the distance
, and ix2
the index of the last succeeding rows that fullfil the condition imposed on angle
:
ix1 = df[df['distance'] > i].iloc[0].name
ix2 = (~(df.loc[ix1:, 'angle'] >= j)).idxmax()-1
l = df.loc[ix1:ix2,:]
list(l.to_records(index=False))
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
thank you so much.
– Kazi
Jan 3 at 15:49
add a comment |
If using Pandas
is an option, here is a way to do it:
i = 5
j = 10
df = pd.DataFrame(a, columns = ('quality', 'angle', 'distance'))
print(df)
quality angle distance
0 10 0 3
1 10 10 6
2 10 15 4
3 10 20 5
4 10 3 3
5 10 5 6
Here ix1
is the index of the first occurrence of the first condition on the distance
, and ix2
the index of the last succeeding rows that fullfil the condition imposed on angle
:
ix1 = df[df['distance'] > i].iloc[0].name
ix2 = (~(df.loc[ix1:, 'angle'] >= j)).idxmax()-1
l = df.loc[ix1:ix2,:]
list(l.to_records(index=False))
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
If using Pandas
is an option, here is a way to do it:
i = 5
j = 10
df = pd.DataFrame(a, columns = ('quality', 'angle', 'distance'))
print(df)
quality angle distance
0 10 0 3
1 10 10 6
2 10 15 4
3 10 20 5
4 10 3 3
5 10 5 6
Here ix1
is the index of the first occurrence of the first condition on the distance
, and ix2
the index of the last succeeding rows that fullfil the condition imposed on angle
:
ix1 = df[df['distance'] > i].iloc[0].name
ix2 = (~(df.loc[ix1:, 'angle'] >= j)).idxmax()-1
l = df.loc[ix1:ix2,:]
list(l.to_records(index=False))
[(10, 10, 6), (10, 15, 4), (10, 20, 5)]
edited Jan 3 at 15:45
answered Jan 3 at 15:40


yatuyatu
15.8k41642
15.8k41642
thank you so much.
– Kazi
Jan 3 at 15:49
add a comment |
thank you so much.
– Kazi
Jan 3 at 15:49
thank you so much.
– Kazi
Jan 3 at 15:49
thank you so much.
– Kazi
Jan 3 at 15:49
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%2f54024998%2fhow-to-slice-multi-dimensional-list-in-python-under-certain-condition%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
2
"if the distance is greater then 5 from that point " what point?
– Daniel Mesejo
Jan 3 at 15:24
as you can see the 2nd value is 10,10,6 (quality,angle,distance). so distance is greater than 5. so i have take the value from angle 10 to 20 @DanielMesejo
– Kazi
Jan 3 at 15:30
1
please clarify a few examples of your desired output, because this is not clear at all
– Josh Friedlander
Jan 3 at 15:34
If the last value of the tuple is the distance, why do you take( 10, 15, 4)?
– Daniel Mesejo
Jan 3 at 15:34
because my concern is whenever I have found the value which is greater than 5, then from that point I will see the angle and slice the array within 10 plus angle. @DanielMesejo
– Kazi
Jan 3 at 15:38