FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Python: Multiplying sequences




Sequences in Python can be multiplied, but while 36*10 yields 360 [36]*10 yields [36, 36, 36, 36, 36, 36, 36, 36, 36, 36].

Again, this can be used for strings as well:

Code:

>>> "-"*78
'------------------------------------------------------------------------------'


And how about tuples ? Does it work for tuples as well ?

Code:

>>> (36)*10
360


Doesn't look like. Well, in this case the brackets are interpreted as brackets like typically used in formulas to define priorities in terms of what to compute first.

How can we change that and convince Python to multiply a tuple ?

Code:

>>> (36,)*10
(36, 36, 36, 36, 36, 36, 36, 36, 36, 36)


Or:

Code:

>>> tuple([36])*10
(36, 36, 36, 36, 36, 36, 36, 36, 36, 36)




0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.