Dropping lists using pattern [duplicate]
up vote
3
down vote
favorite
This question already has an answer here:
Matching a case when one element in a pair is Null
2 answers
I have the following list of lists.
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
I want to drop all the lists with 0
as the 3rd entry i.e. {m,n,0}
. How can I do this?
list-manipulation pattern-matching
marked as duplicate by Jason B., Henrik Schumacher, Kuba♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 3 at 21:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
3
down vote
favorite
This question already has an answer here:
Matching a case when one element in a pair is Null
2 answers
I have the following list of lists.
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
I want to drop all the lists with 0
as the 3rd entry i.e. {m,n,0}
. How can I do this?
list-manipulation pattern-matching
marked as duplicate by Jason B., Henrik Schumacher, Kuba♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 3 at 21:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
This question already has an answer here:
Matching a case when one element in a pair is Null
2 answers
I have the following list of lists.
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
I want to drop all the lists with 0
as the 3rd entry i.e. {m,n,0}
. How can I do this?
list-manipulation pattern-matching
This question already has an answer here:
Matching a case when one element in a pair is Null
2 answers
I have the following list of lists.
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
I want to drop all the lists with 0
as the 3rd entry i.e. {m,n,0}
. How can I do this?
This question already has an answer here:
Matching a case when one element in a pair is Null
2 answers
list-manipulation pattern-matching
list-manipulation pattern-matching
asked Dec 3 at 16:53
Physics Moron
253111
253111
marked as duplicate by Jason B., Henrik Schumacher, Kuba♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 3 at 21:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jason B., Henrik Schumacher, Kuba♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 3 at 21:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Easy (but probably not the fastest) way:
DeleteCases[data, {_, _, 0}]
{{2, 0, 1}, {5, 0, 3}}
This might be a bit faster for longer lists as it entirely avoids pattern matching:
Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]
Here a speed test:
data = RandomInteger[{0, 5}, {1000000, 3}];
a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
AbsoluteTiming // First
a == b
0.407339
0.027375
True
add a comment |
up vote
1
down vote
Another way
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
Pick[data, Unitize@data[[;; , 3]], 1]
{{2, 0, 1}, {5, 0, 3}}
And a timing comparison to DeleteCases
SeedRandom[1234]
Block[
{data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
times = {
AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
};
{times, m1 == m2}
]
{{0.403632, 0.031339}, True}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Easy (but probably not the fastest) way:
DeleteCases[data, {_, _, 0}]
{{2, 0, 1}, {5, 0, 3}}
This might be a bit faster for longer lists as it entirely avoids pattern matching:
Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]
Here a speed test:
data = RandomInteger[{0, 5}, {1000000, 3}];
a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
AbsoluteTiming // First
a == b
0.407339
0.027375
True
add a comment |
up vote
4
down vote
accepted
Easy (but probably not the fastest) way:
DeleteCases[data, {_, _, 0}]
{{2, 0, 1}, {5, 0, 3}}
This might be a bit faster for longer lists as it entirely avoids pattern matching:
Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]
Here a speed test:
data = RandomInteger[{0, 5}, {1000000, 3}];
a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
AbsoluteTiming // First
a == b
0.407339
0.027375
True
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Easy (but probably not the fastest) way:
DeleteCases[data, {_, _, 0}]
{{2, 0, 1}, {5, 0, 3}}
This might be a bit faster for longer lists as it entirely avoids pattern matching:
Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]
Here a speed test:
data = RandomInteger[{0, 5}, {1000000, 3}];
a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
AbsoluteTiming // First
a == b
0.407339
0.027375
True
Easy (but probably not the fastest) way:
DeleteCases[data, {_, _, 0}]
{{2, 0, 1}, {5, 0, 3}}
This might be a bit faster for longer lists as it entirely avoids pattern matching:
Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]
Here a speed test:
data = RandomInteger[{0, 5}, {1000000, 3}];
a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
AbsoluteTiming // First
a == b
0.407339
0.027375
True
edited Dec 3 at 18:30
answered Dec 3 at 16:58
Henrik Schumacher
46.8k466134
46.8k466134
add a comment |
add a comment |
up vote
1
down vote
Another way
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
Pick[data, Unitize@data[[;; , 3]], 1]
{{2, 0, 1}, {5, 0, 3}}
And a timing comparison to DeleteCases
SeedRandom[1234]
Block[
{data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
times = {
AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
};
{times, m1 == m2}
]
{{0.403632, 0.031339}, True}
add a comment |
up vote
1
down vote
Another way
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
Pick[data, Unitize@data[[;; , 3]], 1]
{{2, 0, 1}, {5, 0, 3}}
And a timing comparison to DeleteCases
SeedRandom[1234]
Block[
{data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
times = {
AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
};
{times, m1 == m2}
]
{{0.403632, 0.031339}, True}
add a comment |
up vote
1
down vote
up vote
1
down vote
Another way
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
Pick[data, Unitize@data[[;; , 3]], 1]
{{2, 0, 1}, {5, 0, 3}}
And a timing comparison to DeleteCases
SeedRandom[1234]
Block[
{data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
times = {
AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
};
{times, m1 == m2}
]
{{0.403632, 0.031339}, True}
Another way
data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
Pick[data, Unitize@data[[;; , 3]], 1]
{{2, 0, 1}, {5, 0, 3}}
And a timing comparison to DeleteCases
SeedRandom[1234]
Block[
{data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
times = {
AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
};
{times, m1 == m2}
]
{{0.403632, 0.031339}, True}
answered Dec 3 at 18:41
That Gravity Guy
2,1011515
2,1011515
add a comment |
add a comment |