How do you change certain elements in a list? I'm looking to do the
Python equivalent of this Ruby code:
- first = [1, 2]
= [1, 2]
- second = first
= [1, 2]
- first.map! {|i| i + 1}
= [2, 3]
- first
= [2, 3]
- second
= [2, 3]
I need to change a list, in place, so other variables referencing that
list also see the change.
Thank you... 3 2418
On Nov 6, 3:11 pm, "Just Another Victim of the Ambient Morality"
<ihates...@hotmail.comwrote:
How do you change certain elements in a list? I'm looking to do the
Python equivalent of this Ruby code:
- first = [1, 2]
= [1, 2]
- second = first
= [1, 2]
- first.map! {|i| i + 1}
= [2, 3]
- first
= [2, 3]
- second
= [2, 3]
I need to change a list, in place, so other variables referencing that
list also see the change.
Thank you...
You can "point" one variable name at another so that they both
reference the same list:
second = first
To change a list "in place", I usually do something like this:
first[someElement] = something-else
or
# change the first element in the list
first[0] = 55
See also http://docs.python.org/tut/node7.html http://www.diveintopython.org/native...pes/lists.html http://docs.python.org/lib/built-in-funcs.html
Mike
On Nov 6, 4:11 pm, "Just Another Victim of the Ambient Morality"
<ihates...@hotmail.comwrote:
How do you change certain elements in a list? I'm looking to do the
Python equivalent of this Ruby code:
- first = [1, 2]
= [1, 2]
- second = first
= [1, 2]
- first.map! {|i| i + 1}
= [2, 3]
- first
= [2, 3]
- second
= [2, 3]
I need to change a list, in place, so other variables referencing that
list also see the change.
Thank you...
>>first = [1,2] second = first for i in range(len(first)):
first[i] += 1
>>first
[2, 3]
>>second
[2, 3]
-Dan
Just Another Victim of the Ambient Morality a écrit :
How do you change certain elements in a list? I'm looking to do the
Python equivalent of this Ruby code:
- first = [1, 2]
= [1, 2]
- second = first
= [1, 2]
- first.map! {|i| i + 1}
= [2, 3]
- first
= [2, 3]
- second
= [2, 3]
I need to change a list, in place, so other variables referencing that
list also see the change.
first = [1,2]
second = first
first[:] = [i+1 for i in first]
HTH This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matthias Teege |
last post by:
Moin,
how do I change the list-style-type in nested unordered lists depending
on the level?
Many thanks
Matthias
|
by: Evangelista Sami |
last post by:
Hi all
i have implemented a list type as an array of pointer like this
typedef struct {
int nb_elements;
void **elements;
} list;
to avoid having a pointer for each element as it is done...
|
by: Alan |
last post by:
Hi all,
Before the invoke list of one delegate is called, is thers one general
method to go inside the procedure in order to make some changes such as
removing some delegates from the list. If so,...
|
by: juli jul |
last post by:
Hello,
How can I pass over all the elements that are in the first column of
listview?
I need to do it in a loop and to present each cell value,how?
Thank you!
*** Sent via Developersdex...
|
by: mccoyn |
last post by:
I am making a class dervied from ComboBox. The height of the items in the list will be changed by the program. I've gotten this to work with OnMeasureItem, but there is a large space left behind. ...
|
by: manstey |
last post by:
how do I detect a change in a list of class instances?
from copy import deepcopy
class CaListOfObj(list):
""" subclass of list """
def __init__(self, *args, **kwargs):
list.__init__(self,...
|
by: python101 |
last post by:
I have a list of integers s= to be converted to s=
I tried
s=
for i in s:
str(a)
'2'
|
by: ethanhines |
last post by:
I am using MS Access 2007, I want to change the size of the Check Box that is displayed when a Value List is displayed for multiple selection. I changed the font size but I need to change the Check...
|
by: Alan J. Salmoni |
last post by:
Hello everyone,
I searched through groups to find an appropriate answer to this one
but could only find these which didn't meet my program's needs:...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
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...
|
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...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |