Tuples

Fredrik Lundh fredrik at pythonware.com
Tue Feb 1 13:30:23 EST 2000


Remco Gerlich wrote:
> I was making some functions for a chess program earlier. For a few
> minutes, the idea was to represent the board by a 64-tuple.
> 
> But that's not very smart, is it?
> 
> Given 64-tuple x, is there an easy way to get the tuple with values
> x[6] and x[21] swapped, for instance? Apart from
> 
> a1,b1,c1,d1,e1,f1,g1,h1,a2,b2,c2,d2,e2,f2,g2,h2, \
> a3,b3,c3,d3,e3,f3,g3,h3,a4,b4,c4,d4,e4,f4,g4,h4, \
> a5,b5,c5,d5,e5,f5,g5,h5,a6,b6,c6,d6,e6,f6,g6,h6, \
> a7,b7,c7,d7,e7,f7,g7,h7,a8,b8,c8,d8,e8,f8,g8,h8 = x
> 
> x = (a1,b1,c1,d1,e1,f1,f3,h1,a2,b2,c2,d2,e2,f2,g2,h2,
>      a3,b3,c3,d3,e3,g1,g3,h3,a4,b4,c4,d4,e4,f4,g4,h4,
> a5,b5,c5,d5,e5,f5,g5,h5,a6,b6,c6,d6,e6,f6,g6,h6,
> a7,b7,c7,d7,e7,f7,g7,h7,a8,b8,c8,d8,e8,f8,g8,h8)
> 
> Or even
> 
> x = (x[0],x[1],x[2],x[3],x[4].....
> etc
> 
> Tuples are immutable, but are there any nice builtin functions that
> return new tuples, say with one element changed, or something like that?

T = 1, 2, 3
L = list(T)
... modify list in place
T = tuple(L)

(note: this is more efficient than it may seem -- the
list() and tuple() functions copy references, not the
objects themselves).

> My boards are lists now.

sounds reasonable (unless you prefer dictionaries,
of course).

</F>





More information about the Python-list mailing list