[Tutor] I don't understand this code

2010-07-14 Thread ZUXOXUS
Hi,

I am a true beginner in programming, and im learning with
inventwithpython.com.

There's something I dont understand, and i would really appreciate any help.

In chapter 9, the one about the Hangman game, I don't get the block of code
in line 61

59.  words = 'ant baboon badger bat bear'
60.

   1. def getRandomWord(wordList):
   2. # This function returns a random string from the passed list of
   strings.
   3. wordIndex = random.randint(0, len(wordList) - 1)
   4. return wordList[wordIndex]


The thing is, the "passed list of strings" is called "words", not
"wordList", so I see it shouldn't work.

On the other hand, the variable "wordList" is defined nowhere!

The code is ok, because the program runs ok. So there is somethings that i
dont get.

Thank you very much in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I don't understand this code

2010-07-16 Thread ZUXOXUS
Hey,

thanks for your help!

I couldn't understand because I didn't see that further down in the code, as
Serdar Tumgoren said, the function was called:

secretWord = getRandomWord(words)

without that line I couldn't get it.

Thank you very much, everybody, great explanations.

Alan, I have been checking your tutorial too, great job.



2010/7/13 ZUXOXUS 

> Hi,
>
> I am a true beginner in programming, and im learning with
> inventwithpython.com.
>
> There's something I dont understand, and i would really appreciate any
> help.
>
> In chapter 9, the one about the Hangman game, I don't get the block of code
> in line 61
>
> 59.  words = 'ant baboon badger bat bear'
> 60.
>
>1. def getRandomWord(wordList):
>2. # This function returns a random string from the passed list of
>strings.
>3. wordIndex = random.randint(0, len(wordList) - 1)
>4. return wordList[wordIndex]
>
>
> The thing is, the "passed list of strings" is called "words", not
> "wordList", so I see it shouldn't work.
>
> On the other hand, the variable "wordList" is defined nowhere!
>
> The code is ok, because the program runs ok. So there is somethings that i
> dont get.
>
> Thank you very much in advance.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread ZUXOXUS
Hi all pythoners

I've got a probably easy to answer question.

Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 'ultra'.

They are in a list, or in a sequence or whatever, say a bag of words

And now I want to know how many couples I can do with them, and I want the
program to show me the actual couples: 'manman', 'manbat', 'mansuper',
'manultra', 'batbat', 'batman', 'batsuper', etc.

But hey, why building up new words from just two strings? I also want to
know the possible combinations of three words, four words, and perhaps, why
not, five words.

So, is it easy to do?

Sorry, I'm new in programing, and am probably far from being a math-master

I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
then what? I don't know how to say: take every element and paste it to
another one from the bag, and with another one, and with another one,...

If it's too complex, I dont need the whole code recipe, just need some
clues, or perhaps a useful link

Thank you very much in advance!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread ZUXOXUS
Sharp thanks, but:

I try to reproduce the example from the table, but:

>>> import itertools
>>> combinations('ABC', 2)
Traceback (most recent call last):
  File "", line 1, in 
combinations('ABC', 2)
NameError: name 'combinations' is not defined
>>>

If im not mistaken, it should return AB, AC, BA, etc.

I'm using Python 3.1


2010/7/28 Mark Lawrence 

> On 27/07/2010 23:20, ZUXOXUS wrote:
>
>> Hi all pythoners
>>
>> I've got a probably easy to answer question.
>>
>> Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
>> 'ultra'.
>>
>> They are in a list, or in a sequence or whatever, say a bag of words
>>
>> And now I want to know how many couples I can do with them, and I want the
>> program to show me the actual couples: 'manman', 'manbat', 'mansuper',
>> 'manultra', 'batbat', 'batman', 'batsuper', etc.
>>
>> But hey, why building up new words from just two strings? I also want to
>> know the possible combinations of three words, four words, and perhaps,
>> why
>> not, five words.
>>
>> So, is it easy to do?
>>
>> Sorry, I'm new in programing, and am probably far from being a math-master
>>
>> I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
>> then what? I don't know how to say: take every element and paste it to
>> another one from the bag, and with another one, and with another one,...
>>
>> If it's too complex, I dont need the whole code recipe, just need some
>> clues, or perhaps a useful link
>>
>> Thank you very much in advance!
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
> The lazy way.
>
> http://docs.python.org/library/itertools.html
> Look for combinations().
>
> HTH.
>
> Mark Lawrence.
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread ZUXOXUS
Mac,

this is what I get:

>>> for prod in itertools.product('ABC', 2):
print(prod)

Traceback (most recent call last):
  File "", line 1, in 
for prod in itertools.product('ABC', 2):
TypeError: 'int' object is not iterable


hmm, what might be that 'int' object? 2?


2010/7/28 ZUXOXUS 

> Sharp thanks, but:
>
> I try to reproduce the example from the table, but:
>
> >>> import itertools
> >>> combinations('ABC', 2)
> Traceback (most recent call last):
>   File "", line 1, in 
> combinations('ABC', 2)
> NameError: name 'combinations' is not defined
> >>>
>
> If im not mistaken, it should return AB, AC, BA, etc.
>
> I'm using Python 3.1
>
>
> 2010/7/28 Mark Lawrence 
>
> On 27/07/2010 23:20, ZUXOXUS wrote:
>>
>>> Hi all pythoners
>>>
>>> I've got a probably easy to answer question.
>>>
>>> Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
>>> 'ultra'.
>>>
>>> They are in a list, or in a sequence or whatever, say a bag of words
>>>
>>> And now I want to know how many couples I can do with them, and I want
>>> the
>>> program to show me the actual couples: 'manman', 'manbat', 'mansuper',
>>> 'manultra', 'batbat', 'batman', 'batsuper', etc.
>>>
>>> But hey, why building up new words from just two strings? I also want to
>>> know the possible combinations of three words, four words, and perhaps,
>>> why
>>> not, five words.
>>>
>>> So, is it easy to do?
>>>
>>> Sorry, I'm new in programing, and am probably far from being a
>>> math-master
>>>
>>> I'm clueless, I think probably the code have some FOR I IN SEQUENCE...
>>> but
>>> then what? I don't know how to say: take every element and paste it to
>>> another one from the bag, and with another one, and with another one,...
>>>
>>> If it's too complex, I dont need the whole code recipe, just need some
>>> clues, or perhaps a useful link
>>>
>>> Thank you very much in advance!
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>> The lazy way.
>>
>> http://docs.python.org/library/itertools.html
>> Look for combinations().
>>
>> HTH.
>>
>> Mark Lawrence.
>>
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread ZUXOXUS
Oh, I think i got it:

>>> for prod in itertools.product('ABC', 'ABC'):
print(prod)

('A', 'A')
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'B')
('B', 'C')
('C', 'A')
('C', 'B')
('C', 'C')

Thank you very much!!

2010/7/28 ZUXOXUS 

> Mac,
>
> this is what I get:
>
> >>> for prod in itertools.product('ABC', 2):
> print(prod)
>
> Traceback (most recent call last):
>   File "", line 1, in 
> for prod in itertools.product('ABC', 2):
> TypeError: 'int' object is not iterable
>
>
> hmm, what might be that 'int' object? 2?
>
>
> 2010/7/28 ZUXOXUS 
>
> Sharp thanks, but:
>>
>> I try to reproduce the example from the table, but:
>>
>> >>> import itertools
>> >>> combinations('ABC', 2)
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> combinations('ABC', 2)
>> NameError: name 'combinations' is not defined
>> >>>
>>
>> If im not mistaken, it should return AB, AC, BA, etc.
>>
>> I'm using Python 3.1
>>
>>
>> 2010/7/28 Mark Lawrence 
>>
>> On 27/07/2010 23:20, ZUXOXUS wrote:
>>>
>>>> Hi all pythoners
>>>>
>>>> I've got a probably easy to answer question.
>>>>
>>>> Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
>>>> 'ultra'.
>>>>
>>>> They are in a list, or in a sequence or whatever, say a bag of words
>>>>
>>>> And now I want to know how many couples I can do with them, and I want
>>>> the
>>>> program to show me the actual couples: 'manman', 'manbat', 'mansuper',
>>>> 'manultra', 'batbat', 'batman', 'batsuper', etc.
>>>>
>>>> But hey, why building up new words from just two strings? I also want to
>>>> know the possible combinations of three words, four words, and perhaps,
>>>> why
>>>> not, five words.
>>>>
>>>> So, is it easy to do?
>>>>
>>>> Sorry, I'm new in programing, and am probably far from being a
>>>> math-master
>>>>
>>>> I'm clueless, I think probably the code have some FOR I IN SEQUENCE...
>>>> but
>>>> then what? I don't know how to say: take every element and paste it to
>>>> another one from the bag, and with another one, and with another one,...
>>>>
>>>> If it's too complex, I dont need the whole code recipe, just need some
>>>> clues, or perhaps a useful link
>>>>
>>>> Thank you very much in advance!
>>>>
>>>> ___
>>>> Tutor maillist  -  Tutor@python.org
>>>> To unsubscribe or change subscription options:
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>>
>>> The lazy way.
>>>
>>> http://docs.python.org/library/itertools.html
>>> Look for combinations().
>>>
>>> HTH.
>>>
>>> Mark Lawrence.
>>>
>>>
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-28 Thread ZUXOXUS
2010/7/28 Dave Angel 

>
>
> ZUXOXUS wrote:
>
>> Oh, I think i got it:
>>
>>
>>
>>> for prod in itertools.product('ABC', 'ABC'):
>>>>>
>>>>>
>>>> print(prod)
>>
>> ('A', 'A')
>> ('A', 'B')
>> ('A', 'C')
>> ('B', 'A')
>> ('B', 'B')
>> ('B', 'C')
>> ('C', 'A')
>> ('C', 'B')
>> ('C', 'C')
>>
>> Thank you very much!!
>>
>> 2010/7/28 ZUXOXUS 
>>
>>
> You're top-posting, which loses all the context. In this forum, put your
> comments after whatever lines you're quoting.
>
> Your latest version gets the product of two.  But if you want an arbitrary
> number, instead of repeating the iterable ('ABC' in your case), you can use
> a repeat count.  That's presumably what you were trying to do in your
> earlier incantation.  But you forgot the 'repeat' keyword:
>
> for prod in itertools.product('ABC', repeat=4):
>   
>
> will give you all the four-tuples.
>
> DaveA
>
>
>

> Hey
>

Sorry for the top-posting, I forgot this rule

Thanks for the reminder, Dave Angel, and for the 'repeat' keyword!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-28 Thread ZUXOXUS
>
> 2010/7/28 Dave Angel 
> 
>
> Your latest version gets the product of two.  But if you want an arbitrary
>> number, instead of repeating the iterable ('ABC' in your case), you can
>> use
>> a repeat count.  That's presumably what you were trying to do in your
>> earlier incantation.  But you forgot the 'repeat' keyword:
>>
>> for prod in itertools.product('ABC', repeat=4):
>>  
>>
>> will give you all the four-tuples.
>>
>> DaveA
>>
>>
>>
>>
>
> 
>
> Thanks for the reminder, Dave Angel, and for the 'repeat' keyword!
>
>
>
Since you're new to Python, it's probably useful to expound a little bit, on
how you could have figured it out from the help documentation.

itertools.product(/*iterables/[, /repeat/])

  Cartesian product of input iterables.

  Equivalent to nested for-loops in a generator expression. For
  example, product(A, B) returns the same as ((x,y) for x in A for y
  in B).

  The nested loops cycle like an odometer with the rightmost element
  advancing on every iteration. This pattern creates a lexicographic
  ordering so that if the input’s iterables are sorted, the product
  tuples are emitted in sorted order.

  To compute the product of an iterable with itself, specify the
  number of repetitions with the optional /repeat/ keyword argument.
  For example, product(A, repeat=4) means the same as product(A, A, A, A).



Now that example at the end is exactly what you need here. But let's look at
the first line.

See the *iterables in the formal parameter list. The leading * means you can
put 1, 2, or as many iterables as you like, and they'll each be treated as
an independent dimension in the cartesian product. So when you put the
arguments,
...product("ABC", 2)

the 2 is treated as an iterable, which it isn't. Thus your error. When there
are an arbitrary number of such arguments, followed by another optional
parameter, there's no way the compiler could guess in which sense you wanted
the 2 to be used. So you have to use that parameter's name as a keyword in
your call. If you have a function declared as
def product(*iterables, repeat):
.

then you can call it with
count = 4
product(list1, list2, repeat=count)

and by specifying the 'repeat' keyword, the system knows to stop copying
your arguments into the iterables collection.

Actually, I suspect that if you specify a repeat count, you can only supply
one iterable, but I'm really talking about the language here.

DaveA



Wow, Thank you DaveA, that was very useful.

However, as it usually happens, answers trigger new questions.

My doubt now is whether I can change the way python show the combinations.

I mean, here's what python actually does:

>>> for prod in itertools.product('abc', repeat=3):
print(prod)

('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'a')
('a', 'b', 'b')
('a', 'b', 'c')
[...] etc.


what if I want the combinations listed in a... well, in a list, kind of like
this:

('aaa', 'aab', aac', 'aba', 'abb', 'abc' [...]etc.)

can I do that?

I have checked how the function works (see below), perhaps I have to just
change couple of lines of the code and voilá, the result displayed as I
want... But unfortunately I'm too newbie for this, or this is too hardcore:

def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)


Any ideas will be very much appreciated.

2010/7/28 Dave Angel 

> ZUXOXUS wrote:
>
>> 2010/7/28 Dave Angel 
>> 
>>
>>  Your latest version gets the product of two.  But if you want an
>>> arbitrary
>>> number, instead of repeating the iterable ('ABC' in your case), you can
>>> use
>>> a repeat count.  That's presumably what you were trying to do in your
>>> earlier incantation.  But you forgot the 'repeat' keyword:
>>>
>>> for prod in itertools.product('ABC', repeat=4):
>>>  
>>>
>>> will give you all the four-tuples.
>>>
>>> DaveA
>>>
>>>
>>>
>>>
>>
>> 
>>
>> Thanks for the reminder, Dave Angel, and for the 'repeat' keyword!
>

Re: [Tutor] Calculating and returning possible combinations ofelements from a given set

2010-07-28 Thread ZUXOXUS
2010/7/28 Alan Gauld 

>
> "ZUXOXUS"  wrote
>
>
>
>  My doubt now is whether I can change the way python show the combinations.
>>
>
> Python will display the compbinations however you tell it to.
> The function generates the combinations the display is up to you.
> In this case you are simply printing the results as they come.
> But you can put them in a list if you prefer.
>
>  prodList = []
>>>>
>>>> for prod in itertools.product('abc', repeat=3):
>>>>
>>> ...   prodList.append(prod)
> ...
>
>> print prodList
>>>>
>>>
> You can manipulate prod however you like before putting it inthe list.
> Once you have the list you can sort that list to get anyorder you want.
> And once you have your soted and formatted list you can print it out
> using whatever formatting you want.
>
> It is always good to separate the generation of data fropm the
> storage of data from the display of data.
>
>
>  I have checked how the function works (see below), perhaps I have to just
>> change couple of lines of the code and voilá, the result displayed as I
>> want... But unfortunately I'm too newbie for this, or this is too
>> hardcore:
>>
>
> Its hardly ever a good idea to modify the standard library functions.
> You can write a wrapper around them if you like - and indeed thats normal.
> But changing them is almost always a very bad  idea!
>
> def myProduct(*args, **kwds):
># do something with input data
># call itertools.product(args, kwds)
># do something with the output
># return a result
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


Hi Alan and everybody

Well, that is not exactly what I expected, but can help

>>> lista = []
>>> for prod in itertools.product('aei', repeat=2):
lista.append(prod)
print(lista)

[('a', 'a')]
[('a', 'a'), ('a', 'e')]
[('a', 'a'), ('a', 'e'), ('a', 'i')]
[('a', 'a'), ('a', 'e'), ('a', 'i'), ('e', 'a')]
[('a', 'a'), ('a', 'e'), ('a', 'i'), ('e', 'a'), ('e', 'e')]
[('a', 'a'), ('a', 'e'), ('a', 'i'), ('e', 'a'), ('e', 'e'), ('e', 'i')]
[('a', 'a'), ('a', 'e'), ('a', 'i'), ('e', 'a'), ('e', 'e'), ('e', 'i'),
('i', 'a')]
[('a', 'a'), ('a', 'e'), ('a', 'i'), ('e', 'a'), ('e', 'e'), ('e', 'i'),
('i', 'a'), ('i', 'e')]
[('a', 'a'), ('a', 'e'), ('a', 'i'), ('e', 'a'), ('e', 'e'), ('e', 'i'),
('i', 'a'), ('i', 'e'), ('i', 'i')]
>>>


Now I only need to put together in a single string all the elements that are
grouped in parentheses, I think I can do that

Thank you very much!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-29 Thread ZUXOXUS
2010/7/28 Dave Angel 

> ZUXOXUS wrote:
>
>> 
>>
>> My doubt now is whether I can change the way python show the combinations.
>>
>> I mean, here's what python actually does:
>>
>>
>>
>>> for prod in itertools.product('abc', repeat=3):
>>>>>
>>>>>
>>>> print(prod)
>>
>> ('a', 'a', 'a')
>> ('a', 'a', 'b')
>> ('a', 'a', 'c')
>> ('a', 'b', 'a')
>> ('a', 'b', 'b')
>> ('a', 'b', 'c')
>> [...] etc.
>>
>>
>> what if I want the combinations listed in a... well, in a list, kind of
>> like
>> this:
>>
>> ('aaa', 'aab', aac', 'aba', 'abb', 'abc' [...]etc.)
>>
>> can I do that?
>>
>> I have checked how the function works (see below), perhaps I have to just
>> change couple of lines of the code and voilá, the result displayed as I
>> want... But unfortunately I'm too newbie for this, or this is too
>> hardcore:
>>
>> def product(*args, **kwds):
>># product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
>># product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
>>pools = map(tuple, args) * kwds.get('repeat', 1)
>>result = [[]]
>>for pool in pools:
>>result = [x+[y] for x in result for y in pool]
>>for prod in result:
>>yield tuple(prod)
>>
>>
>> Any ideas will be very much appreciated.
>>
>> 
>>
> Well itertools.product() already returns an iterator that's equivalent to a
> list of tuples.   You can print that list simply by doing something like:
> print list(itertools.product('abc', repeat=3))
>
> So your question is how you can transform such a list into a list of
> strings instead.
>
> so try each of the following.
>
>
> for prod in itertools.product('abc', repeat=3):
>   print "".join(prod)
>
> print ["".join(prod) for prod in itertools.product('abc', repeat=3)]
>
> DaveA
>
>
***

Hi DaveA, the second option returns exactly the result I wanted:

>>> print(["".join(prod) for prod in itertools.product('abc', repeat=3)])
['aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc', 'baa',
'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa', 'cab', 'cac',
'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']

Thank you very much.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor