Hi experts,
it's very common that I have a list and I want to print it with commas
in between. How do I do this in an easy manner, whithout having the
annoying comma in the end?
<code>
list = [1,2,3,4,5,6]
# the easy way
for element in list:
print element, ',',
print
# this is what I really want. is there some way better?
if (len(list) 0):
print list[0],
for element in list[1:]:
print ',', element,
</code>
Thx,
Ernesto
Nov 5 '06
14 1456
Peter van Kampen schrieb:
On 2006-11-06, Fredrik Lundh <fr*****@python ware.comwrote:
>I've collected a bunch of list pydioms and other notes here:
http://effbot.org/zone/python-list.htm
"""
A = B = [] # both names will point to the same list
"""
I've been bitten by this once or twice in the past, but I have always
wondered what it was useful for? Can anybody enlighten me?
Do you never have a situation where you want to assign the same value
to two variables?
Or are you objecting to the fact that both names point to the same object?
It couldn't be otherwise. Consider:
X = []
A = B = X
What should this do? Copy "X" and assign one copy to A, one to B?
Georg
At Wednesday 8/11/2006 16:51, Peter van Kampen wrote:
>""" A = B = [] # both names will point to the same list """
I've been bitten by this once or twice in the past, but I have always wondered what it was useful for? Can anybody enlighten me?
As an optimization, inside a method, you can bind an instance
attribute and a local name to the same object:
def some_action(sel f):
self.items = items = []
// following many references to self.items,
// but using items instead.
Names in the local namespace are resolved at compile time, so using
items is a lot faster than looking for "items" inside self's
namespace each time it's used.
--
Gabriel Genellina
Softlab SRL
_______________ _______________ _______________ _____
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Ernesto García García wrote:
list = [1,2,3,4,5,6]
Just a nit-pick: It's considered an anti-idiom
to hide builtins just as list by using it as a
name for a variable.
>>list=[1,2,3,4,5] tuple = (1,2,3,4,5) if list == list(tuple): print "equal"
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'list' object is not callable
>>#ouch!
....
>>l=list del list if l == list(tuple): print "equal"
....
equal
>>if tuple(l)==tuple : print "equal"
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'tuple' object is not callable
>>t=tuple del tuple if tuple(l)==t: print "equal"
....
equal
On 2006-11-08, Georg Brandl <g.************ *@gmx.netwrote:
Peter van Kampen schrieb:
>On 2006-11-06, Fredrik Lundh <fr*****@python ware.comwrote:
>>I've collected a bunch of list pydioms and other notes here:
http://effbot.org/zone/python-list.htm """ A = B = [] # both names will point to the same list """
I've been bitten by this once or twice in the past, but I have always wondered what it was useful for? Can anybody enlighten me?
Do you never have a situation where you want to assign the same value
to two variables?
In the sense that I might want two empty lists or maybe 2 ints that
are initialised as 0 (zero). That's what I (incorrectly) thought A = B
= [] did.
Or are you objecting to the fact that both names point to the same
object?
I'm not objecting to anything, merely wondering when I could/should
use this idiom. There's a nice example in another response (self.items
= items = [])
It couldn't be otherwise. Consider:
X = []
A = B = X
What should this do? Copy "X" and assign one copy to A, one to B?
Ah yes, I see the error in my ways...I skipped the A = B part too
lightly.
Thanks,
PterK
--
Peter van Kampen
pterk -- at -- datatailors.com
On 2006-11-08, Gabriel Genellina <ga******@yahoo .com.arwrote:
At Wednesday 8/11/2006 16:51, Peter van Kampen wrote:
>>""" A = B = [] # both names will point to the same list """
I've been bitten by this once or twice in the past, but I have always wondered what it was useful for? Can anybody enlighten me?
As an optimization, inside a method, you can bind an instance
attribute and a local name to the same object:
def some_action(sel f):
self.items = items = []
// following many references to self.items,
// but using items instead.
Names in the local namespace are resolved at compile time, so using
items is a lot faster than looking for "items" inside self's
namespace each time it's used.
Nice example.
Thanks,
PterK
--
Peter van Kampen
pterk -- at -- datatailors.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kyle E |
last post by:
I wrote a program that asks a user questions and records the answers and
prints them out at the end. Pretty simple... but I have a few things that I
don't like about it.
----------------------------------------------
print "Do you have a P.O. Box?"
poan = raw_input ("> ")
if poan == "yes":
print "What is your P.O. Box number?"
pobox = input ("> ")
----------------------------------------------
|
by: Marek Mänd |
last post by:
<style type="text/css">
q:after{content:',"'}
</style>
<q>This will be the shame of CSS</q> claimed Marek Mänd and added that
<q>consumers expect to create generated content via CSS where there
would be no comma right after HERE</q>
|
by: Grey Plastic |
last post by:
How do I pass text that contains a comma into a macro? For example, I
have
#define ATTRIBUTE(name,type) ...
and I want to do
ATTRIBUTE(transitions, map<int,int>);
However, the last comma is being interpretted as the macro argument
|
by: darren |
last post by:
Hello,
I am posting a html form to a php processing page, which produces a csv file
from the form data. However if the user inputs a comma then it screws up my
data.
So i'd like to know how to strip my data of commas before submitting to the
php processor.
Or maybe not strip the commas but handle them better.
|
by: Jon Maz |
last post by:
Hi,
A quick one:
If you are forming a dynamic sql statement using parameters from a web form,
you would normally double up any single inverted commas inputted by the user
to stop sql injection. But if you are using command parameters to build the
sql statement (as below), is this still necessary?
cmd.Parameters.Add("@subcategory", SqlDbType.VarChar).Value =
| |
by: conspireagainst |
last post by:
I'm having quite a time with this particular problem:
I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces and commas, and can use quotes to encapsulate multiple words.
An example:
tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6"
So, as we can see here anything is allowed, but the problem is that splitting on commas obviously destroys tag4...
|
by: bowtie |
last post by:
if a code is written like below what does it mean:
project1.openform form1 ,,,,
i'm mainly concerned about the commas wat do they imply and what can be put after these commas
|
by: riccrom123 |
last post by:
Hi, I have a csv file where the commas are skipped by the characther \ for example
field1,field2,field3\,field3,field4
I would like to extract the fields and obtain
field1
field2
field3field3
field4
|
by: vdesio |
last post by:
I am new to ASP. I am using the free ezscheduler asp program to create a calendar for my website. The idea is for employees to schedule their work shifts online. I have modified some of the programmin from its original format.
Here is my problem. The information that people enter are going into a database as comma delimited. When the information is pulled from the database and placed on the website it leaves the commas from the fields. I do...
|
by: Robert Dodier |
last post by:
Hello,
I'd like to split a string by commas, but only at the "top level" so
to speak. An element can be a comma-less substring, or a
quoted string, or a substring which looks like a function call.
If some element contains commas, I don't want to split it.
Examples:
'foo, bar, baz' ='foo' 'bar' 'baz'
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |