Getting a combination using a function












-1















I got a problem here. The question is that assuming Z=3, and Y= 4, how do I create a function that returns me a random combination of maybe zzzyyyy or zzyyyzy or any permutation? I did the code below but im quite confused.



def combinations(iterable,r):
pool= tuple(iterable)
n= len(pool)
for indices in permutations(range(n), r):
if sorted(indices)==listed(indices):
yield tuple(pool[i] for i in indices)
return

print(combinations('AAABBB', 4))


but i got only



output: <generator object combinations at 0x103f81518>


Thanks!!










share|improve this question

























  • yeap thanks. i just edited the question

    – Jonathan
    Nov 22 '18 at 6:35











  • i really did try and im new to this. how am i supposed to convince you that i did try.. ah forget it im not here to argue with u

    – Jonathan
    Nov 22 '18 at 6:45











  • edited and still cant figure it out

    – Jonathan
    Nov 22 '18 at 7:03











  • well honestly i have no clue what you're saying cos its already properly spaced. can just give me a mark down.

    – Jonathan
    Nov 22 '18 at 7:08
















-1















I got a problem here. The question is that assuming Z=3, and Y= 4, how do I create a function that returns me a random combination of maybe zzzyyyy or zzyyyzy or any permutation? I did the code below but im quite confused.



def combinations(iterable,r):
pool= tuple(iterable)
n= len(pool)
for indices in permutations(range(n), r):
if sorted(indices)==listed(indices):
yield tuple(pool[i] for i in indices)
return

print(combinations('AAABBB', 4))


but i got only



output: <generator object combinations at 0x103f81518>


Thanks!!










share|improve this question

























  • yeap thanks. i just edited the question

    – Jonathan
    Nov 22 '18 at 6:35











  • i really did try and im new to this. how am i supposed to convince you that i did try.. ah forget it im not here to argue with u

    – Jonathan
    Nov 22 '18 at 6:45











  • edited and still cant figure it out

    – Jonathan
    Nov 22 '18 at 7:03











  • well honestly i have no clue what you're saying cos its already properly spaced. can just give me a mark down.

    – Jonathan
    Nov 22 '18 at 7:08














-1












-1








-1








I got a problem here. The question is that assuming Z=3, and Y= 4, how do I create a function that returns me a random combination of maybe zzzyyyy or zzyyyzy or any permutation? I did the code below but im quite confused.



def combinations(iterable,r):
pool= tuple(iterable)
n= len(pool)
for indices in permutations(range(n), r):
if sorted(indices)==listed(indices):
yield tuple(pool[i] for i in indices)
return

print(combinations('AAABBB', 4))


but i got only



output: <generator object combinations at 0x103f81518>


Thanks!!










share|improve this question
















I got a problem here. The question is that assuming Z=3, and Y= 4, how do I create a function that returns me a random combination of maybe zzzyyyy or zzyyyzy or any permutation? I did the code below but im quite confused.



def combinations(iterable,r):
pool= tuple(iterable)
n= len(pool)
for indices in permutations(range(n), r):
if sorted(indices)==listed(indices):
yield tuple(pool[i] for i in indices)
return

print(combinations('AAABBB', 4))


but i got only



output: <generator object combinations at 0x103f81518>


Thanks!!







function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:15









Andreas

1,8312918




1,8312918










asked Nov 22 '18 at 6:25









JonathanJonathan

467




467













  • yeap thanks. i just edited the question

    – Jonathan
    Nov 22 '18 at 6:35











  • i really did try and im new to this. how am i supposed to convince you that i did try.. ah forget it im not here to argue with u

    – Jonathan
    Nov 22 '18 at 6:45











  • edited and still cant figure it out

    – Jonathan
    Nov 22 '18 at 7:03











  • well honestly i have no clue what you're saying cos its already properly spaced. can just give me a mark down.

    – Jonathan
    Nov 22 '18 at 7:08



















  • yeap thanks. i just edited the question

    – Jonathan
    Nov 22 '18 at 6:35











  • i really did try and im new to this. how am i supposed to convince you that i did try.. ah forget it im not here to argue with u

    – Jonathan
    Nov 22 '18 at 6:45











  • edited and still cant figure it out

    – Jonathan
    Nov 22 '18 at 7:03











  • well honestly i have no clue what you're saying cos its already properly spaced. can just give me a mark down.

    – Jonathan
    Nov 22 '18 at 7:08

















yeap thanks. i just edited the question

– Jonathan
Nov 22 '18 at 6:35





yeap thanks. i just edited the question

– Jonathan
Nov 22 '18 at 6:35













i really did try and im new to this. how am i supposed to convince you that i did try.. ah forget it im not here to argue with u

– Jonathan
Nov 22 '18 at 6:45





i really did try and im new to this. how am i supposed to convince you that i did try.. ah forget it im not here to argue with u

– Jonathan
Nov 22 '18 at 6:45













edited and still cant figure it out

– Jonathan
Nov 22 '18 at 7:03





edited and still cant figure it out

– Jonathan
Nov 22 '18 at 7:03













well honestly i have no clue what you're saying cos its already properly spaced. can just give me a mark down.

– Jonathan
Nov 22 '18 at 7:08





well honestly i have no clue what you're saying cos its already properly spaced. can just give me a mark down.

– Jonathan
Nov 22 '18 at 7:08












1 Answer
1






active

oldest

votes


















0














You can simply use the permutations function in itertools. It will generate all possible permutation of the given list (or string). Then use randrange in random to generate the random number in the range of 0 to the list length, to select any random element in the list.



from itertools import permutations
import random

def build_string(z_num, y_num):
string = ''
string += 'Z'*z_num
string += 'Y'*y_num
return string

string = build_string(3, 4)
perms = [''.join(p) for p in permutations('ZZZYYYY')]
print(perms[random.randrange(0, len(perms))]) # print random permutation





share|improve this answer
























  • Thanks Andres. Apologies for losing my temper. Am still very new to this and am trying to figure out a lot of things. It's crazy difficult

    – Jonathan
    Nov 22 '18 at 7:23











  • I would suggest you to start from simpler task or project. Also, to get better understanding regarding python capabilities, so that you would know what to search in Google or in here. I found that usually most questions has already been answered in one way or another in StackOverflow

    – Andreas
    Nov 22 '18 at 7:24











  • thanks. how long did it take you to learn python?

    – Jonathan
    Nov 22 '18 at 7:27











  • This is not a suitable place for that, we can move to chat.stackoverflow.com if you have other unrelated questions.

    – Andreas
    Nov 22 '18 at 7:29











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425006%2fgetting-a-combination-using-a-function%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









0














You can simply use the permutations function in itertools. It will generate all possible permutation of the given list (or string). Then use randrange in random to generate the random number in the range of 0 to the list length, to select any random element in the list.



from itertools import permutations
import random

def build_string(z_num, y_num):
string = ''
string += 'Z'*z_num
string += 'Y'*y_num
return string

string = build_string(3, 4)
perms = [''.join(p) for p in permutations('ZZZYYYY')]
print(perms[random.randrange(0, len(perms))]) # print random permutation





share|improve this answer
























  • Thanks Andres. Apologies for losing my temper. Am still very new to this and am trying to figure out a lot of things. It's crazy difficult

    – Jonathan
    Nov 22 '18 at 7:23











  • I would suggest you to start from simpler task or project. Also, to get better understanding regarding python capabilities, so that you would know what to search in Google or in here. I found that usually most questions has already been answered in one way or another in StackOverflow

    – Andreas
    Nov 22 '18 at 7:24











  • thanks. how long did it take you to learn python?

    – Jonathan
    Nov 22 '18 at 7:27











  • This is not a suitable place for that, we can move to chat.stackoverflow.com if you have other unrelated questions.

    – Andreas
    Nov 22 '18 at 7:29
















0














You can simply use the permutations function in itertools. It will generate all possible permutation of the given list (or string). Then use randrange in random to generate the random number in the range of 0 to the list length, to select any random element in the list.



from itertools import permutations
import random

def build_string(z_num, y_num):
string = ''
string += 'Z'*z_num
string += 'Y'*y_num
return string

string = build_string(3, 4)
perms = [''.join(p) for p in permutations('ZZZYYYY')]
print(perms[random.randrange(0, len(perms))]) # print random permutation





share|improve this answer
























  • Thanks Andres. Apologies for losing my temper. Am still very new to this and am trying to figure out a lot of things. It's crazy difficult

    – Jonathan
    Nov 22 '18 at 7:23











  • I would suggest you to start from simpler task or project. Also, to get better understanding regarding python capabilities, so that you would know what to search in Google or in here. I found that usually most questions has already been answered in one way or another in StackOverflow

    – Andreas
    Nov 22 '18 at 7:24











  • thanks. how long did it take you to learn python?

    – Jonathan
    Nov 22 '18 at 7:27











  • This is not a suitable place for that, we can move to chat.stackoverflow.com if you have other unrelated questions.

    – Andreas
    Nov 22 '18 at 7:29














0












0








0







You can simply use the permutations function in itertools. It will generate all possible permutation of the given list (or string). Then use randrange in random to generate the random number in the range of 0 to the list length, to select any random element in the list.



from itertools import permutations
import random

def build_string(z_num, y_num):
string = ''
string += 'Z'*z_num
string += 'Y'*y_num
return string

string = build_string(3, 4)
perms = [''.join(p) for p in permutations('ZZZYYYY')]
print(perms[random.randrange(0, len(perms))]) # print random permutation





share|improve this answer













You can simply use the permutations function in itertools. It will generate all possible permutation of the given list (or string). Then use randrange in random to generate the random number in the range of 0 to the list length, to select any random element in the list.



from itertools import permutations
import random

def build_string(z_num, y_num):
string = ''
string += 'Z'*z_num
string += 'Y'*y_num
return string

string = build_string(3, 4)
perms = [''.join(p) for p in permutations('ZZZYYYY')]
print(perms[random.randrange(0, len(perms))]) # print random permutation






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 7:20









AndreasAndreas

1,8312918




1,8312918













  • Thanks Andres. Apologies for losing my temper. Am still very new to this and am trying to figure out a lot of things. It's crazy difficult

    – Jonathan
    Nov 22 '18 at 7:23











  • I would suggest you to start from simpler task or project. Also, to get better understanding regarding python capabilities, so that you would know what to search in Google or in here. I found that usually most questions has already been answered in one way or another in StackOverflow

    – Andreas
    Nov 22 '18 at 7:24











  • thanks. how long did it take you to learn python?

    – Jonathan
    Nov 22 '18 at 7:27











  • This is not a suitable place for that, we can move to chat.stackoverflow.com if you have other unrelated questions.

    – Andreas
    Nov 22 '18 at 7:29



















  • Thanks Andres. Apologies for losing my temper. Am still very new to this and am trying to figure out a lot of things. It's crazy difficult

    – Jonathan
    Nov 22 '18 at 7:23











  • I would suggest you to start from simpler task or project. Also, to get better understanding regarding python capabilities, so that you would know what to search in Google or in here. I found that usually most questions has already been answered in one way or another in StackOverflow

    – Andreas
    Nov 22 '18 at 7:24











  • thanks. how long did it take you to learn python?

    – Jonathan
    Nov 22 '18 at 7:27











  • This is not a suitable place for that, we can move to chat.stackoverflow.com if you have other unrelated questions.

    – Andreas
    Nov 22 '18 at 7:29

















Thanks Andres. Apologies for losing my temper. Am still very new to this and am trying to figure out a lot of things. It's crazy difficult

– Jonathan
Nov 22 '18 at 7:23





Thanks Andres. Apologies for losing my temper. Am still very new to this and am trying to figure out a lot of things. It's crazy difficult

– Jonathan
Nov 22 '18 at 7:23













I would suggest you to start from simpler task or project. Also, to get better understanding regarding python capabilities, so that you would know what to search in Google or in here. I found that usually most questions has already been answered in one way or another in StackOverflow

– Andreas
Nov 22 '18 at 7:24





I would suggest you to start from simpler task or project. Also, to get better understanding regarding python capabilities, so that you would know what to search in Google or in here. I found that usually most questions has already been answered in one way or another in StackOverflow

– Andreas
Nov 22 '18 at 7:24













thanks. how long did it take you to learn python?

– Jonathan
Nov 22 '18 at 7:27





thanks. how long did it take you to learn python?

– Jonathan
Nov 22 '18 at 7:27













This is not a suitable place for that, we can move to chat.stackoverflow.com if you have other unrelated questions.

– Andreas
Nov 22 '18 at 7:29





This is not a suitable place for that, we can move to chat.stackoverflow.com if you have other unrelated questions.

– Andreas
Nov 22 '18 at 7:29


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425006%2fgetting-a-combination-using-a-function%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin