473,324 Members | 2,002 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,324 software developers and data experts.

How to efficiently proceed addition and subtraction in python list?

Hello all:

I have a list AAA = [1, 2, 3] and would like to subtract one from list
AAA
so AAA' = [0, 1, 2]

What should I do?
Thank you
-Daniel

Sep 18 '06 #1
9 2350
I have a list AAA = [1, 2, 3] and would like to subtract one from list
AAA
so AAA' = [0, 1, 2]

What should I do?

Sounds like a list comprehension to me:
>>a = [1,2,3]
a_prime = [x-1 for x in a]
a_prime
[0, 1, 2]

-tkc

Sep 18 '06 #2
try this
a=[1, 2, 3]
b=a[:]

"Daniel Mark" <da***********@gmail.com???
news:11**********************@m73g2000cwd.googlegr oups.com ???...
Hello all:

I have a list AAA = [1, 2, 3] and would like to subtract one from list
AAA
so AAA' = [0, 1, 2]

What should I do?
Thank you
-Daniel

Sep 19 '06 #3
"Daniel Mark" <da***********@gmail.comwrites:
I have a list AAA = [1, 2, 3] and would like to subtract one from list
AAA
so AAA' = [0, 1, 2]

What should I do?
BBB = [x-1 for x in AAA]
Sep 19 '06 #4
Ant

Tim Chase wrote:
I have a list AAA = [1, 2, 3] and would like to subtract one from list
AAA
so AAA' = [0, 1, 2]

What should I do?


Sounds like a list comprehension to me:
Also the built in function 'map' would work:
>>a = [1,2,3]
b = map(lambda x: x-1, a)
b
[0, 1, 2]

List comprehensions are more pythonic, but map would probably be faster
if performance was a (real) issue.

Sep 19 '06 #5
Ant wrote:
Tim Chase wrote:
>>>I have a list AAA = [1, 2, 3] and would like to subtract one from list
AAA
so AAA' = [0, 1, 2]

What should I do?


Sounds like a list comprehension to me:


Also the built in function 'map' would work:

>>>>a = [1,2,3]
b = map(lambda x: x-1, a)
b

[0, 1, 2]

List comprehensions are more pythonic, but map would probably be faster
if performance was a (real) issue.
And statements like that are probably going to annoy me ;-)
>>t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
t.timeit()
2.4686168214116599
>>t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
t.timeit()
0.9930245324475635
>>>
Any timing prediction involving the word "probably" isn't worth the
paper it's written on (or even less if it's posted in a newsgroup ;-).
If it's "probably" faster, and if performance is *really* important, you
need to benchmark both options to remove the "probably". As the above
test makes clear, your assertions are certainly untrue for 2.4.2 on Windows.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 19 '06 #6
Steve Holden wrote:
And statements like that are probably going to annoy me ;-)
>>t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
>>t.timeit()
2.4686168214116599
>>t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
>>t.timeit()
0.9930245324475635
>>>
And now someone's probably reading this and thinking that list comprehensions
are *always* faster than "map"...

</F>

Sep 19 '06 #7
Fredrik Lundh wrote:
Steve Holden wrote:

>>And statements like that are probably going to annoy me ;-)
>>t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
t.timeit()
2.4686168214116599
>>t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
t.timeit()
0.9930245324475635
>>>


And now someone's probably reading this and thinking that list comprehensions
are *always* faster than "map"...
You are so right. So, let me repeat the important part that Fredrik
didn't quote:

"""if performance is *really* important, you
need to benchmark both options"""

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 19 '06 #8
Ant

Steve Holden wrote:
Fredrik Lundh wrote:
Steve Holden wrote:
....
"""if performance is *really* important, you
need to benchmark both options"""
Fair point.

Sep 19 '06 #9
On 18 Sep 2006 15:43:31 -0700, Daniel Mark <da***********@gmail.comwrote:
Hello all:

I have a list AAA = [1, 2, 3] and would like to subtract one from list
AAA
so AAA' = [0, 1, 2]
You've had some excellent suggestions as to how to go about this
assuming that by "efficient" you mean in terms of CPU. If, instead,
you are concerned with memory usage, you might instead do something
like:

for index, value in enumerate(AAA):
AAA[index] = value-1

--
Cheers,
Simon B,
si***@brunningonline.net
Sep 19 '06 #10

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

Similar topics

9
by: Paul | last post by:
Can anyone suggest an efficient way to eliminate duplicate entries in a list? The naive approach below works fine, but is very slow with lists containing tens of thousands of entries: def...
14
by: Richard | last post by:
I have a large list of two element tuples. I want two separate lists: One list with the first element of every tuple, and the second list with the second element of every tuple. Each tuple...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
22
by: fynali | last post by:
Hi all, I have two files: - PSP0000320.dat (quite a large list of mobile numbers), - CBR0000319.dat (a subset of the above, a list of barred bumbers) # head PSP0000320.dat CBR0000319.dat...
5
by: Paul | last post by:
Why is the addition here adding the number as a string but the subtraction works fine? function boxchange(box) { iv= dbform.ut.value if (box.checked == true) { iv = iv - box.value } else {...
3
by: muler | last post by:
hi all, After reading this excerpt from "The C# Programming Language", (By Anders) I tried to check it out. Unfortunately, I'm getting compile errors. Can anyone illustrate this with an...
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
31
by: krypto.wizard | last post by:
How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer.
3
dlite922
by: dlite922 | last post by:
Hey guys, For the love of all things holy I can't figure out how to add two numbers together in smarty. I've checked the manual, there's no main section, if it's in there, it's probably lost...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.