473,396 Members | 2,158 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.

[1,2,3] exactly same as [1,2,3,] ?

mh
x=[1,2,3]
and
x=[1,2,3,]

are exactly the same, right?

I'm generating some python data, and it's less error prone
to not treat the last element specially, but I want to be
sure I'm generating an equivalent data structure.

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
Aug 28 '08 #1
15 965
mh@pixar.com wrote:
x=[1,2,3]
and
x=[1,2,3,]

are exactly the same, right?
When confronted with this type of question, I ask the interpreter:

{{{
mac:~ pmcnett$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>[1,2,3] == [1,2,3,]
True
}}}

Paul
Aug 28 '08 #2
mh
Paul McNett <p@ulmcnett.comwrote:
When confronted with this type of question, I ask the interpreter:
>>[1,2,3] == [1,2,3,]
True
Well I guess that's a pretty authoritative answer... thanks!

--
Mark Harrison
Pixar Animation Studios
Aug 28 '08 #3
On Fri, Aug 29, 2008 at 9:28 AM, Paul McNett <p@ulmcnett.comwrote:
When confronted with this type of question, I ask the interpreter:

{{{
mac:~ pmcnett$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>[1,2,3] == [1,2,3,]
True
}}}
I must point out though that although they contain
the same elements/data, they are not the same
object/instance.

{{{
#!python
>>x = [1, 2, 3]
y = [1, 2, 3]
id(x)
3083095148L
>>id(y)
3082953324L
>>x == y
True
}}}

If you view the documentation for a list:
{{{
#!sh
$ pydoc list
}}}

list's have an __eq__ that is used to compare the
equality of 2 lists.

cheers
James
--
--
-- "Problems are solved by method"
Aug 28 '08 #4
James Mills wrote:
I must point out though that although they contain
the same elements/data, they are not the same
object/instance.
True, but the OP wanted equality:
I want to be
sure I'm generating an equivalent data structure.
Paul
Aug 28 '08 #5
On Aug 28, 6:35*pm, "James Mills" <prolo...@shortcircuit.net.au>
wrote:
I must point out though that although they contain
the same elements/data, they are not the same
object/instance.

{{{
#!python
>x = [1, 2, 3]
y = [1, 2, 3]
id(x)
3083095148L
>id(y)
3082953324L
>x == y
True
}}}
Umm, yeah, but that's true of ANY two mutable objects you create, and
has absolutely nothing to do with whether the syntax which generated
the list contains a trailing comma or not. To wit:
>>[1,2,3] is [1,2,3]
False

Regards,
Pat
Aug 29 '08 #6


mh@pixar.com wrote:
x=[1,2,3]
and
x=[1,2,3,]

are exactly the same, right?
Yes, so you can write something like either your second example or

l = [
kjasldfjs,
kjsalfj,
ksjdflasj,
]

and insert items without worrying about leaving out the comma (less of a
problem with 'horizontal' list), or delete the last line and not have to
worry about deleting the comma on the line before.

Aug 29 '08 #7
mh@pixar.com wrote:
x=[1,2,3]
and
x=[1,2,3,]

are exactly the same, right?

I'm generating some python data, and it's less error prone
to not treat the last element specially, but I want to be
sure I'm generating an equivalent data structure.

Many TIA!
Mark
>>x=[1,2,3,]
repr(x)
[1,2,3]

Aug 29 '08 #8
Terry Reedy <tj*****@udel.eduwrote:
Yes, so you can write something like either your second example or

l = [
kjasldfjs,
kjsalfj,
ksjdflasj,
]

and insert items without worrying about leaving out the comma (less of a
problem with 'horizontal' list), or delete the last line and not have to
worry about deleting the comma on the line before.
Exactly. This is one of those little pieces of syntactic sugar which makes
python so nice to work with. The alternative is (in C, for example)
abominations like this:

const char* l[] = {"foo"
, "bar"
, "baz"
};

and even those are not quite as good because you still have to special-case
the first entry.
Aug 29 '08 #9
On 2008-08-29, Roy Smith <ro*@panix.comwrote:
Exactly. This is one of those little pieces of syntactic
sugar which makes python so nice to work with. The
alternative is (in C, for example) abominations like this:

const char* l[] = {"foo"
, "bar"
, "baz"
};

and even those are not quite as good because you still have to
special-case the first entry.
It's probably a spec violation, but I've never seen a C
compiler that objected to a comma after the last item in an
initializer list. (At least not at the warning levels I use,
which tend to be on the picky side.)

--
Grant Edwards grante Yow! There's enough money
at here to buy 5000 cans of
visi.com Noodle-Roni!
Aug 29 '08 #10
On Thu, 28 Aug 2008 16:28:03 -0700, Paul McNett wrote:
mh@pixar.com wrote:
>x=[1,2,3]
and
x=[1,2,3,]

are exactly the same, right?

When confronted with this type of question, I ask the interpreter:

{{{
mac:~ pmcnett$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple
Computer, Inc. build 5363)] on darwin Type "help", "copyright",
"credits" or "license" for more information.
>>[1,2,3] == [1,2,3,]
True
}}}


Putting on my pedantic hat...

In this case you're right about the two lists being the same, and I'm a
great believer in checking things in the interactive interpreter, but you
need to take care. Just because two objects compare as equal doesn't
*necessarily* mean they are the same:

>>1.0 == 1
True
>>1.0 == decimal.Decimal('1.0')
False
>>1.0 == float(decimal.Decimal('1.0'))
True
>>collections.defaultdict(999) == {}
True

--
Steven
Aug 29 '08 #11
In article <QO******************************@posted.visi>,
Grant Edwards <gr****@visi.comwrote:
On 2008-08-29, Roy Smith <ro*@panix.comwrote:
Exactly. This is one of those little pieces of syntactic
sugar which makes python so nice to work with. The
alternative is (in C, for example) abominations like this:

const char* l[] = {"foo"
, "bar"
, "baz"
};

and even those are not quite as good because you still have to
special-case the first entry.

It's probably a spec violation, but I've never seen a C
compiler that objected to a comma after the last item in an
initializer list. (At least not at the warning levels I use,
which tend to be on the picky side.)
Yowza, you're right (at least for the one case I tried). This must be a
new development (where "new development" is defined as, "It wasn't legal in
the original K&R C I learned when I was a pup").

Still, I have seem people do that in code.
Aug 29 '08 #12
On Aug 29, 10:11*am, Roy Smith <r...@panix.comwrote:
In article <QOCdnfa_yarxliXVnZ2dnUVZ_i2dn...@posted.visi>,
*Grant Edwards <gra...@visi.comwrote:


On 2008-08-29, Roy Smith <r...@panix.comwrote:
Exactly. *This is one of those little pieces of syntactic
sugar which makes python so nice to work with. *The
alternative is (in C, for example) abominations like this:
const char* l[] = {"foo"
* * * * * * * * *, "bar"
* * * * * * * * *, "baz"
* * * * * * * * *};
and even those are not quite as good because you still have to
special-case the first entry.
It's probably a spec violation, but I've never seen a C
compiler that objected to a comma after the last item in an
initializer list. *(At least not at the warning levels I use,
which tend to be on the picky side.)

Yowza, you're right (at least for the one case I tried). *This must be a
new development (where "new development" is defined as, "It wasn't legal in
the original K&R C I learned when I was a pup").
That's the difference between a specification and
an implementation, isn't it?
>
Still, I have seem people do that in code.- Hide quoted text -

- Show quoted text -
Aug 29 '08 #13
Steven D'Aprano wrote:
On Thu, 28 Aug 2008 16:28:03 -0700, Paul McNett wrote:
>mh@pixar.com wrote:
>>x=[1,2,3]
and
x=[1,2,3,]

are exactly the same, right?
When confronted with this type of question, I ask the interpreter:

{{{
mac:~ pmcnett$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple
Computer, Inc. build 5363)] on darwin Type "help", "copyright",
"credits" or "license" for more information.
> >>[1,2,3] == [1,2,3,]
True
}}}

Putting on my pedantic hat...

In this case you're right about the two lists being the same, and I'm a
great believer in checking things in the interactive interpreter, but you
need to take care. Just because two objects compare as equal doesn't
*necessarily* mean they are the same:
True.
>>>1.0 == 1
True
>>>1.0 == decimal.Decimal('1.0')
False
>>>1.0 == float(decimal.Decimal('1.0'))
True
These are comparing different types.

>>>collections.defaultdict(999) == {}
True
I try this and get:

TypeError: first arument must be callable
Paul
Aug 29 '08 #14
Roy Smith wrote:
Yowza, you're right (at least for the one case I tried). This must be a
new development (where "new development" is defined as, "It wasn't legal in
the original K&R C I learned when I was a pup").
the C 89 grammar appears to be:

initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }

initializer-list:
designation-opt initializer
initializer-list , designation-opt initializer

so a trailing comma has been allowed for around twenty years.

</F>

Aug 30 '08 #15
In article <ma*************************************@python.or g>,
Fredrik Lundh <fr*****@pythonware.comwrote:
Roy Smith wrote:
Yowza, you're right (at least for the one case I tried). This must be a
new development (where "new development" is defined as, "It wasn't legal in
the original K&R C I learned when I was a pup").

the C 89 grammar appears to be:

initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }

initializer-list:
designation-opt initializer
initializer-list , designation-opt initializer

so a trailing comma has been allowed for around twenty years.

</F>
C89 came out about 10 years after I first learned C :-)
Aug 30 '08 #16

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

Similar topics

14
by: mjkahn | last post by:
I've read (and read!) that you shouldn't store objects in Session variables. I've read these reasons: - The object takes up memory that may not be freed until the session times out. Better to...
4
by: cayblood | last post by:
Hello, I have the following class declaration in an include file: class Node { public: Node(string name = ""); Node(const Node& node); Node& operator=(const Node& node); bool...
22
by: Fred Ma | last post by:
I'm using the expression "int a = ceil( SomeDouble )". The man page says that ceil returns the smallest integer that is not less than SomeDouble, represented as a double. However, my...
16
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
1
by: TJ | last post by:
Dear Sir/Madam I made one user control using C#(.NET Runtime is 1.1). It is embeded in Internet Explorer Basically, this control is very similiar to FTP client program. So it should be connecte...
5
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
2
by: Matthew Wilson | last post by:
The random.jumpahead documentation says this: Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
7
by: blaine | last post by:
Hey everyone, For the regular expression gurus... I'm trying to write a string matching algorithm for genomic sequences. I'm pulling out Genes from a large genomic pattern, with certain start...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.