About 233,000 results
Open links in new tab
  1. python - Finding and replacing elements in a list - Stack Overflow

    @outis: map+lambda is less readable and slower than the equivalent listcomp. You can squeeze some performance out of map when the mapping function is a built-in implemented in C and the input is large enough for map's per-item benefits to overcome the slightly higher fixed overhead, but when map needs a Python level function (e.g. a lambda) an equivalent genexpr/listcomp could inline (avoiding ...

  2. How to replace values at specific indexes of a python list?

    for a_element, m_element in zip(a, m): s[a_element] = m_element To make it work the way you were trying to do it, you would have to get a list of indices to iterate over. This is doable: we can use range(len(a)) for example. But don't do that! That's not how we do things in Python.

  3. python - Find and replace string values in list - Stack Overflow

    I found this example quite useful for a more complicated case where I couldn't sort out the list comprehension. Or more specifically, for a case where I didn't have time to sort out the list comprehension and then was able to later replace the code with comprehension when I …

  4. python - Replace the last element in a list with another list - Stack ...

    Jul 9, 2012 · If your removal criteria is based on position (as in this case) or any other kind of criteria, there is always a better way than "find the element I want to remove then call remove". list.remove is needlessly slow if you've already found the element by some other means, and frequently does the wrong thing in such cases, as explained by DSM's ...

  5. python - How to replace elements in a list using dictionary lookup ...

    Jun 25, 2013 · import pandas as pd A = ['A','B','A','C','D'] #list we want to replace with a dictionary lookup B = {'A':1,'B':2,'C':3,'D':4} #dictionary lookup, dict values in B will be mapped to entries in A C = (pd.Series(A)).map(B) #convert the list to a pandas series temporarily before mapping D = list(C) # we transform the mapped values (a series object ...

  6. Replacing selected elements in a list in Python - Stack Overflow

    Jan 16, 2013 · You can use the six module to simulate the behavior of map in Python 3.x from Python 2.x, and in combination with collections.deque (again as suggested by @abarnert), you can achieve the same output without creating the additional list in memory because a deque that can contain a maximum of 0 items will discard everything it receives from the ...

  7. Replace values in list using Python - Stack Overflow

    @gath: Don't aspire to write one-liners for every purpose. Sometimes, they increase readability or performance, but often they don't. As to hints: Get to know the tools that Python ofters, especially list (and for Python 3 also dict) comprehensions, the ternary operator, anonymous (lambda) functions, and functions like map, zip, filter, reduce ...

  8. Replace elements in a list of lists python - Stack Overflow

    Sep 24, 2018 · I'm going to use a simple example, but basically x is another variable and isn't linked to the list element. You have to change the list element directly in order to alter the list. l=[1,2,3,4] for x in l: x=x+1 This doesn't change the list. l=[1,2,3,4] for i,x in enumerate(l): l[i]=x+1 this changes the list

  9. python: replace elements in list with conditional

    Sep 21, 2015 · Because Python will evaluated the x>=3 as True and since True is equal to 1 so the second element of x will be converted to 3. For such purpose you need to use a list comprehension : >>> [3 if i >=3 else i for i in x] [3, 3, 3, 2, 1] And if you want to know that why x >= 3 evaluates as True, see the following documentation:

  10. Replace element with a list in python - Stack Overflow

    Feb 6, 2013 · Replace element of a list in python. 0. Replacing elements in a list, Python. 3. Substitute elements in a ...

Refresh