Filling array with sequence
I am having number N and i want to make amount of arrays .for example with N=2
I need
(0,0),(0.5,0),(0,0.5),(1,1) ,(1,0.5), (0.5,1)
for N=3
its like
(0,0,0),(0.5,0,0)...(0.5,0.5,0)....(1,0.5,0.5)...(1,1,1)
which are contain all combinations of 0, 0.5, 1
.
I tried to use cycle for ,but didn't find the way to solve the problem with any N.I prefer python numpy
or java if its real.
python arrays list sequence
add a comment |
I am having number N and i want to make amount of arrays .for example with N=2
I need
(0,0),(0.5,0),(0,0.5),(1,1) ,(1,0.5), (0.5,1)
for N=3
its like
(0,0,0),(0.5,0,0)...(0.5,0.5,0)....(1,0.5,0.5)...(1,1,1)
which are contain all combinations of 0, 0.5, 1
.
I tried to use cycle for ,but didn't find the way to solve the problem with any N.I prefer python numpy
or java if its real.
python arrays list sequence
1
Doesn't seem to be anything to do with Java. Please remove the [Java] tag.
– OldCurmudgeon
Feb 12 '18 at 15:24
You should start with some idea, before starting to type the code.
– Betlista
Feb 12 '18 at 15:24
Sorry,it was missclick,removed
– Станислав Китаев
Feb 12 '18 at 15:33
add a comment |
I am having number N and i want to make amount of arrays .for example with N=2
I need
(0,0),(0.5,0),(0,0.5),(1,1) ,(1,0.5), (0.5,1)
for N=3
its like
(0,0,0),(0.5,0,0)...(0.5,0.5,0)....(1,0.5,0.5)...(1,1,1)
which are contain all combinations of 0, 0.5, 1
.
I tried to use cycle for ,but didn't find the way to solve the problem with any N.I prefer python numpy
or java if its real.
python arrays list sequence
I am having number N and i want to make amount of arrays .for example with N=2
I need
(0,0),(0.5,0),(0,0.5),(1,1) ,(1,0.5), (0.5,1)
for N=3
its like
(0,0,0),(0.5,0,0)...(0.5,0.5,0)....(1,0.5,0.5)...(1,1,1)
which are contain all combinations of 0, 0.5, 1
.
I tried to use cycle for ,but didn't find the way to solve the problem with any N.I prefer python numpy
or java if its real.
python arrays list sequence
python arrays list sequence
edited Nov 22 '18 at 2:31
Cœur
18.4k9109148
18.4k9109148
asked Feb 12 '18 at 15:21
Станислав КитаевСтанислав Китаев
427
427
1
Doesn't seem to be anything to do with Java. Please remove the [Java] tag.
– OldCurmudgeon
Feb 12 '18 at 15:24
You should start with some idea, before starting to type the code.
– Betlista
Feb 12 '18 at 15:24
Sorry,it was missclick,removed
– Станислав Китаев
Feb 12 '18 at 15:33
add a comment |
1
Doesn't seem to be anything to do with Java. Please remove the [Java] tag.
– OldCurmudgeon
Feb 12 '18 at 15:24
You should start with some idea, before starting to type the code.
– Betlista
Feb 12 '18 at 15:24
Sorry,it was missclick,removed
– Станислав Китаев
Feb 12 '18 at 15:33
1
1
Doesn't seem to be anything to do with Java. Please remove the [Java] tag.
– OldCurmudgeon
Feb 12 '18 at 15:24
Doesn't seem to be anything to do with Java. Please remove the [Java] tag.
– OldCurmudgeon
Feb 12 '18 at 15:24
You should start with some idea, before starting to type the code.
– Betlista
Feb 12 '18 at 15:24
You should start with some idea, before starting to type the code.
– Betlista
Feb 12 '18 at 15:24
Sorry,it was missclick,removed
– Станислав Китаев
Feb 12 '18 at 15:33
Sorry,it was missclick,removed
– Станислав Китаев
Feb 12 '18 at 15:33
add a comment |
1 Answer
1
active
oldest
votes
You can use itertools.product
to generate all the combinations.
def f(n):
return list(itertools.product((0, .5, 1), repeat=n))
print(f(2))
# [(0, 0), (0, 0.5), (0, 1), (0.5, 0), (0.5, 0.5), (0.5, 1), (1, 0), (1, 0.5), (1, 1)]
Edit:
If you only want the combinations of adjacent elements, we can use the pairwise
recipe from the itertools
documentation.
from itertools import tee, chain, product
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def f(n):
values = (0, .5, 1)
return list(chain.from_iterable(product(x, repeat=n) for x in pairwise(values)))
print(f(n))
# [(0, 0), (0, 0.5), (0.5, 0), (0.5, 0.5), (0.5, 0.5), (0.5, 1), (1, 0.5), (1, 1)]
Tnank you,everything works correctly.
– Станислав Китаев
Feb 12 '18 at 15:32
I thought of it, but OP's example is not having(0, 1)
in it. OPs example seems to have the adjacent elements only
– Moinuddin Quadri
Feb 12 '18 at 15:33
I will try to use other itertools things,they are giving for types of combinations.
– Станислав Китаев
Feb 12 '18 at 15:38
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%2f48749711%2ffilling-array-with-sequence%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
You can use itertools.product
to generate all the combinations.
def f(n):
return list(itertools.product((0, .5, 1), repeat=n))
print(f(2))
# [(0, 0), (0, 0.5), (0, 1), (0.5, 0), (0.5, 0.5), (0.5, 1), (1, 0), (1, 0.5), (1, 1)]
Edit:
If you only want the combinations of adjacent elements, we can use the pairwise
recipe from the itertools
documentation.
from itertools import tee, chain, product
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def f(n):
values = (0, .5, 1)
return list(chain.from_iterable(product(x, repeat=n) for x in pairwise(values)))
print(f(n))
# [(0, 0), (0, 0.5), (0.5, 0), (0.5, 0.5), (0.5, 0.5), (0.5, 1), (1, 0.5), (1, 1)]
Tnank you,everything works correctly.
– Станислав Китаев
Feb 12 '18 at 15:32
I thought of it, but OP's example is not having(0, 1)
in it. OPs example seems to have the adjacent elements only
– Moinuddin Quadri
Feb 12 '18 at 15:33
I will try to use other itertools things,they are giving for types of combinations.
– Станислав Китаев
Feb 12 '18 at 15:38
add a comment |
You can use itertools.product
to generate all the combinations.
def f(n):
return list(itertools.product((0, .5, 1), repeat=n))
print(f(2))
# [(0, 0), (0, 0.5), (0, 1), (0.5, 0), (0.5, 0.5), (0.5, 1), (1, 0), (1, 0.5), (1, 1)]
Edit:
If you only want the combinations of adjacent elements, we can use the pairwise
recipe from the itertools
documentation.
from itertools import tee, chain, product
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def f(n):
values = (0, .5, 1)
return list(chain.from_iterable(product(x, repeat=n) for x in pairwise(values)))
print(f(n))
# [(0, 0), (0, 0.5), (0.5, 0), (0.5, 0.5), (0.5, 0.5), (0.5, 1), (1, 0.5), (1, 1)]
Tnank you,everything works correctly.
– Станислав Китаев
Feb 12 '18 at 15:32
I thought of it, but OP's example is not having(0, 1)
in it. OPs example seems to have the adjacent elements only
– Moinuddin Quadri
Feb 12 '18 at 15:33
I will try to use other itertools things,they are giving for types of combinations.
– Станислав Китаев
Feb 12 '18 at 15:38
add a comment |
You can use itertools.product
to generate all the combinations.
def f(n):
return list(itertools.product((0, .5, 1), repeat=n))
print(f(2))
# [(0, 0), (0, 0.5), (0, 1), (0.5, 0), (0.5, 0.5), (0.5, 1), (1, 0), (1, 0.5), (1, 1)]
Edit:
If you only want the combinations of adjacent elements, we can use the pairwise
recipe from the itertools
documentation.
from itertools import tee, chain, product
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def f(n):
values = (0, .5, 1)
return list(chain.from_iterable(product(x, repeat=n) for x in pairwise(values)))
print(f(n))
# [(0, 0), (0, 0.5), (0.5, 0), (0.5, 0.5), (0.5, 0.5), (0.5, 1), (1, 0.5), (1, 1)]
You can use itertools.product
to generate all the combinations.
def f(n):
return list(itertools.product((0, .5, 1), repeat=n))
print(f(2))
# [(0, 0), (0, 0.5), (0, 1), (0.5, 0), (0.5, 0.5), (0.5, 1), (1, 0), (1, 0.5), (1, 1)]
Edit:
If you only want the combinations of adjacent elements, we can use the pairwise
recipe from the itertools
documentation.
from itertools import tee, chain, product
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def f(n):
values = (0, .5, 1)
return list(chain.from_iterable(product(x, repeat=n) for x in pairwise(values)))
print(f(n))
# [(0, 0), (0, 0.5), (0.5, 0), (0.5, 0.5), (0.5, 0.5), (0.5, 1), (1, 0.5), (1, 1)]
edited Feb 12 '18 at 15:44
answered Feb 12 '18 at 15:27


Patrick HaughPatrick Haugh
29.2k92747
29.2k92747
Tnank you,everything works correctly.
– Станислав Китаев
Feb 12 '18 at 15:32
I thought of it, but OP's example is not having(0, 1)
in it. OPs example seems to have the adjacent elements only
– Moinuddin Quadri
Feb 12 '18 at 15:33
I will try to use other itertools things,they are giving for types of combinations.
– Станислав Китаев
Feb 12 '18 at 15:38
add a comment |
Tnank you,everything works correctly.
– Станислав Китаев
Feb 12 '18 at 15:32
I thought of it, but OP's example is not having(0, 1)
in it. OPs example seems to have the adjacent elements only
– Moinuddin Quadri
Feb 12 '18 at 15:33
I will try to use other itertools things,they are giving for types of combinations.
– Станислав Китаев
Feb 12 '18 at 15:38
Tnank you,everything works correctly.
– Станислав Китаев
Feb 12 '18 at 15:32
Tnank you,everything works correctly.
– Станислав Китаев
Feb 12 '18 at 15:32
I thought of it, but OP's example is not having
(0, 1)
in it. OPs example seems to have the adjacent elements only– Moinuddin Quadri
Feb 12 '18 at 15:33
I thought of it, but OP's example is not having
(0, 1)
in it. OPs example seems to have the adjacent elements only– Moinuddin Quadri
Feb 12 '18 at 15:33
I will try to use other itertools things,they are giving for types of combinations.
– Станислав Китаев
Feb 12 '18 at 15:38
I will try to use other itertools things,they are giving for types of combinations.
– Станислав Китаев
Feb 12 '18 at 15:38
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%2f48749711%2ffilling-array-with-sequence%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
Doesn't seem to be anything to do with Java. Please remove the [Java] tag.
– OldCurmudgeon
Feb 12 '18 at 15:24
You should start with some idea, before starting to type the code.
– Betlista
Feb 12 '18 at 15:24
Sorry,it was missclick,removed
– Станислав Китаев
Feb 12 '18 at 15:33