Question

I recently started coding in Python 2.7. I'm a molecular biologist. I'm writing a script that involves creating lists like this one:

mylist = [[0, 4, 6, 1], 102]

These lists are incremented by adding an item to mylist[0] and summing a value to mylist[1].

To do this, I use the code:

def addres(oldpep, res):
    return [oldpep[0] + res[0], oldpep[1] + res[1]]

Which works well. Since mylist[0] can become a bit long, and I have millions of these lists to take care of, I thought that using append or extend might make my code faster, so I tried:

def addres(pep, res):
    pep[0].extend(res[0])
    pep[1] += res[1]
    return pep

Which in my mind should give the same result. It does give the same result when I try it on an arbitrary list. But when I feed it to the million of lists, it gives me a very different result. So... what's the difference between the two? All the rest of the script is exactly the same. Thank you! Roberto

Était-ce utile?

La solution

The difference is that the second version of addres modifies the list that you passed in as pep, where the first version returns a new one.

>>> mylist = [[0, 4, 6, 1], 102]
>>> list2 = [[3, 1, 2], 205]
>>> addres(mylist, list2)
[[0, 4, 6, 1, 3, 1, 2], 307]
>>> mylist
[[0, 4, 6, 1, 3, 1, 2], 307]

If you need to not modify the original lists, I don't think you're going to really going to get a faster Python implementation of addres than the first one you wrote. You might be able to deal with the modification, though, or come up with a somewhat different approach to speed up your code if that's the problem you're facing.

Autres conseils

List are objects in python which are passed by reference.

a=list()

This doesn't mean that a is the list but a is pointing towards a list just created.

In first example, you are using list element and creating a new list, an another object while in the second one you are modifying the list content itself.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top