Conditional list manipulation
$begingroup$
I want to change a numerical list of the (examplary) form {1,3/2,5} to {1,{1,2},5}.
First solution
{3, 5/2, 1 } /. Rational[a_ , b_] -> {Floor[a /b], Ceiling[a/b ]}
(*{3, {2, 3}, 1}*)
works fine. Second try
{3, 5/2, 1} /. (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]} )
(*{{3, 2, 1}, {3, 3, 1}}*)
fails.
What's wrong with my second version?
Thanks!
conditional
$endgroup$
add a comment |
$begingroup$
I want to change a numerical list of the (examplary) form {1,3/2,5} to {1,{1,2},5}.
First solution
{3, 5/2, 1 } /. Rational[a_ , b_] -> {Floor[a /b], Ceiling[a/b ]}
(*{3, {2, 3}, 1}*)
works fine. Second try
{3, 5/2, 1} /. (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]} )
(*{{3, 2, 1}, {3, 3, 1}}*)
fails.
What's wrong with my second version?
Thanks!
conditional
$endgroup$
add a comment |
$begingroup$
I want to change a numerical list of the (examplary) form {1,3/2,5} to {1,{1,2},5}.
First solution
{3, 5/2, 1 } /. Rational[a_ , b_] -> {Floor[a /b], Ceiling[a/b ]}
(*{3, {2, 3}, 1}*)
works fine. Second try
{3, 5/2, 1} /. (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]} )
(*{{3, 2, 1}, {3, 3, 1}}*)
fails.
What's wrong with my second version?
Thanks!
conditional
$endgroup$
I want to change a numerical list of the (examplary) form {1,3/2,5} to {1,{1,2},5}.
First solution
{3, 5/2, 1 } /. Rational[a_ , b_] -> {Floor[a /b], Ceiling[a/b ]}
(*{3, {2, 3}, 1}*)
works fine. Second try
{3, 5/2, 1} /. (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]} )
(*{{3, 2, 1}, {3, 3, 1}}*)
fails.
What's wrong with my second version?
Thanks!
conditional
conditional
asked Feb 2 at 13:00
Ulrich NeumannUlrich Neumann
9,736617
9,736617
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Replace with level specification 1 gives the expected result:
Replace[{3, 5/2, 1}, (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]}), 1]
{3, {2, 3}, 1}
With ReplaceAll the pattern in_ /; (! IntegerQ[in]) matches {3, 5/2, 1} and it is applied to it to give {Floor[{3, 5/2, 1}], Ceiling[{3, 5/2, 1}]}.
$endgroup$
$begingroup$
Thanks, I got it!
$endgroup$
– Ulrich Neumann
Feb 2 at 13:18
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fmathematica.stackexchange.com%2fquestions%2f190714%2fconditional-list-manipulation%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
$begingroup$
Replace with level specification 1 gives the expected result:
Replace[{3, 5/2, 1}, (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]}), 1]
{3, {2, 3}, 1}
With ReplaceAll the pattern in_ /; (! IntegerQ[in]) matches {3, 5/2, 1} and it is applied to it to give {Floor[{3, 5/2, 1}], Ceiling[{3, 5/2, 1}]}.
$endgroup$
$begingroup$
Thanks, I got it!
$endgroup$
– Ulrich Neumann
Feb 2 at 13:18
add a comment |
$begingroup$
Replace with level specification 1 gives the expected result:
Replace[{3, 5/2, 1}, (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]}), 1]
{3, {2, 3}, 1}
With ReplaceAll the pattern in_ /; (! IntegerQ[in]) matches {3, 5/2, 1} and it is applied to it to give {Floor[{3, 5/2, 1}], Ceiling[{3, 5/2, 1}]}.
$endgroup$
$begingroup$
Thanks, I got it!
$endgroup$
– Ulrich Neumann
Feb 2 at 13:18
add a comment |
$begingroup$
Replace with level specification 1 gives the expected result:
Replace[{3, 5/2, 1}, (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]}), 1]
{3, {2, 3}, 1}
With ReplaceAll the pattern in_ /; (! IntegerQ[in]) matches {3, 5/2, 1} and it is applied to it to give {Floor[{3, 5/2, 1}], Ceiling[{3, 5/2, 1}]}.
$endgroup$
Replace with level specification 1 gives the expected result:
Replace[{3, 5/2, 1}, (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]}), 1]
{3, {2, 3}, 1}
With ReplaceAll the pattern in_ /; (! IntegerQ[in]) matches {3, 5/2, 1} and it is applied to it to give {Floor[{3, 5/2, 1}], Ceiling[{3, 5/2, 1}]}.
answered Feb 2 at 13:12
kglrkglr
190k10206424
190k10206424
$begingroup$
Thanks, I got it!
$endgroup$
– Ulrich Neumann
Feb 2 at 13:18
add a comment |
$begingroup$
Thanks, I got it!
$endgroup$
– Ulrich Neumann
Feb 2 at 13:18
$begingroup$
Thanks, I got it!
$endgroup$
– Ulrich Neumann
Feb 2 at 13:18
$begingroup$
Thanks, I got it!
$endgroup$
– Ulrich Neumann
Feb 2 at 13:18
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fmathematica.stackexchange.com%2fquestions%2f190714%2fconditional-list-manipulation%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
