473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to do [1]*4 for a tuple

I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when I
have to do the following:

'%s some value %s and some other text %s' % (a)*3

as apposed to

'%s some value %s and some other text %s' % (a, a, a)

In this case I always end up doing

'%s some value %s and some other text %s' % [a]*3

VJ
Jul 18 '05 #1
2 1048
Vineet Jain wrote in news:mailman.375.1081213535.20120.python-
li**@python.org:
I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when
I have to do the following:
(1,) * 4 (1, 1, 1, 1)
'%s some value %s and some other text %s' % (a)*3
a = "<aaa>"
'%s some value %s and some other text %s' % ((a,)*3) '<aaa> some value <aaa> and some other text <aaa>'
as apposed to

'%s some value %s and some other text %s' % (a, a, a)

In this case I always end up doing

'%s some value %s and some other text %s' % [a]*3


tuples are comma seperated items, parenthesis just bracket:
b = 1, 3
b (1, 3) type( b ) <type 'tuple'> c = 1,
c (1,) type( c ) <type 'tuple'>


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 18 '05 #2
On Mon, 5 Apr 2004 21:05:34 -0400, "Vineet Jain" <vi****@eswap.com>
wrote:
I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when I
have to do the following:

'%s some value %s and some other text %s' % (a)*3

as apposed to

'%s some value %s and some other text %s' % (a, a, a)

In this case I always end up doing

'%s some value %s and some other text %s' % [a]*3


You have the answer to your question already, but you may be better
off using a dictionary with the % operator to get named association...
a={'a':'aaa'}
print '%(a)s some value %(a)s and some other text %(a)s' % a

aaa some value aaa and some other text aaa

This works particularly well when not all substituted strings are the
same, and when you may want to change format strings in such a way
that the substitutions may not be in the same order, or may not all be
used, etc. In other words, it can be very useful when the format
strings are supplied externally.

It can be handy when the support guys want to decide the wording of
error messages (and change their minds every other day) for instance.
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Carlo v. Dango | last post by:
Hello there. I have a function which as an argument takes a tuple and either returns that tuple or a mutated version of it. The problem is that tuples are imutable, hence I have to create a new...
9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
15
by: Steve M | last post by:
Hello, I'm trying to figure out the index position of a tuple member. I know the member name, but I need to know the members index position. I know that if I use the statement print tuple that...
37
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
77
by: Nick Maclaren | last post by:
Why doesn't the tuple type have an index method? It seems such a bizarre restriction that there must be some reason for it. Yes, I know it's a fairly rare requirement. Regards, Nick...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
4
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like...
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.