This is normal Python behaviour:

$ python
Python 3.4.5 (default, Oct 30 2016, 12:40:50) 
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more informa.
>>> l=[1,2]
>>> b=l
>>> b.append(5)
>>> b
[1, 2, 5]
>>> l
[1, 2, 5]
>>> 

basically, l and b are pointers (as in C :-))
see e.g. http://henry.precheur.org/python/copy_list

On Friday, November 18, 2016 at 5:22:27 PM UTC, tomdean1939 wrote:
>
> I assign the contents of one variable to another variable and then append 
> to that variable.  The original variable is changed.
>
>     for u in oddone:
>         thishand = fourofkind
>         print "  append ",u," to ",thishand
>         thishand.append(u)
>         Hands.append(thishand)
>
> after this, fourofkind is changed.  
>
> What am I doing wrong?
>
> Tom Dean
>
> Complete code:
>
> print "Poker and probability"
> print "A card in a poker deck is characterized by a suit (hearts, 
> diamonds,"
> print "spades, or clubs) and a value (2, 3, ..., 10, jack, queen, king, 
> ace)."
> print "The game is played with a full deck, which consists of the 
> Cartesian"
> print "product of the set of suits and the set of values:"
> print "    Cards = Suits X Values = {s,v | s in Suites and v in Values"
>
> Suits = Set(["Hearts", "Diamonds", "Spades", "Clubs"])
> Values = Set(["2", "3", "4", "5", "6", "7", "8", "9", "10",
>               "Jack", "Queen", "King", "Ace"])
> Cards = cartesian_product([Values, Suits])
>
> Suits.cardinality()
> Values.cardinality()
> Cards.cardinality()
>
> Cards.random_element()
> Set([Cards.random_element(), Cards.random_element()]) 
> Hands = Subsets(Cards, 5)
> Hands.random_element()
>
> binomial(52,5)
> Hands.cardinality()
>
> print "Construct a set of all flushes"
> Flushes = cartesian_product([Subsets(Values, 5), Suits])
> Flushes.cardinality()
> print "The probability of obtaining a flush when drawing a hand at random"
> print "is therefore:"
> Flushes.cardinality()  / Hands.cardinality()
> 1000.0 * Flushes.cardinality()  / Hands.cardinality()
> def is_flush(hand):
>     return len(set(suit for (val, suit) in hand)) == 1
>
> print "Count the number of flushes in",
> n = 10000
> nflush = 0
> for i in range(n):                            # long time
>    hand = Hands.random_element()
>    if is_flush(hand):
>        nflush += 1
> print n, "hands: ",nflush                               # random
>
>
> print "A hand containing four cards of the same value is called a four of 
> a"
> print "kind. Construct the set of four of a kind hands (Hint: use 
> Arrangements"
> print "to choose a pair of distinct values at random, then choose a suit 
> for"
> print "the first value). Calculate the number of four of a kind hand, list"
> print "them, and then determine the probability of obtaining a four of a"
> print "kind when drawing a hand at random."
> print
> print "Example, four Kings"
> print "King Hearts",
> print " King Clubs",
> print " King Spades",
> print " King Diamonds",
> print " 2 Hearts"
> print "King Hearts",
> print " King Clubs",
> print " King Spades",
> print " King Diamonds",
> print " 2 Clubs"
> print "King Hearts",
> print " King Clubs",
> print " King Spades",
> print " King Diamonds",
> print " 2 Spades"
> print "King Hearts",
> print " King Clubs",
> print " King Spades",
> print " King Diamonds",
> print " 2 Diamonds"
>
> tmp = Arrangements(Values,2).list()
>
> var('Hands, fourof, oddone, thishand,fourofkind')
> Hands = []
> for s in tmp:
>     print "--> ",s
>     fourof = cartesian_product([{s[0]},Suits])
>     oddone = cartesian_product([{s[1]},Suits])
>     fourofkind = []
>     for t in fourof:
>         fourofkind.append(t)
>     print " fourofkind ",fourofkind
>     for u in oddone:
>         thishand = fourofkind
>         print "  append ",u," to ",thishand
>         thishand.append(u)
>         Hands.append(thishand)
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to