I want to create a list of lists, each of which is identical, but which
can be modified independently i.e: x = [ [0], [0], [0] ] x[0].append(1) x
[[0, 1], [0], [0]]
The above construct works if I have only few items, but if I have many,
I'd prefer to write N =3 x =N*[[0]] x
[[0], [0], [0]]
If I now try extending the lists indepently, I cannot, as they all
point to the same list object x[0].append(1) x
[[0, 1], [0, 1], [0, 1]]
Is there a simple way to create a list of independent lists?
Thanks in advance
Thomas Philips 3 1901
Ciao, tk****@hotmail.com! Che stavi dicendo? Is there a simple way to create a list of independent lists?
N=3
x=[[0] for e in range(N)]
--
Up da 1 giorno, 3 ore, 43 minuti e 10 secondi
> The above construct works if I have only few items, but if I have many, I'd prefer to write
N =3 x =N*[[0]] x [[0], [0], [0]]
If I now try extending the lists indepently, I cannot, as they all point to the same list object x[0].append(1) x [[0, 1], [0, 1], [0, 1]]
Is there a simple way to create a list of independent lists?
My first thought would be N = 10 x = [[0] for _ in range(N)] x[0].append(1) x
[[0, 1], [0], [0], [0], [0], [0], [0], [0], [0], [0]]
HTH,
-tkc tk****@hotmail.com wrote: I want to create a list of lists, each of which is identical, but which can be modified independently i.e:
x = [ [0], [0], [0] ] x[0].append(1) x [[0, 1], [0], [0]]
The above construct works if I have only few items, but if I have many, I'd prefer to write N =3 x =N*[[0]] x [[0], [0], [0]]
If I now try extending the lists indepently, I cannot, as they all point to the same list object x[0].append(1) x
[[0, 1], [0, 1], [0, 1]]
Is there a simple way to create a list of independent lists?
Try this...
x, y, value = 3, 3, 0
L = [[value]*x for i in xrange(y)]
Cheers,
Ron This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Glen Able |
last post by:
Hi,
I have a collection of lists, something like this:
std::list<Obj*> lists;
Now I add an Obj to one of the lists:
Obj* gary;
lists.push_front(gary);
|
by: Booser |
last post by:
// Merge sort using circular linked list
// By Jason Hall <booser108@yahoo.com>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//#define debug
|
by: KraftDiner |
last post by:
I need a matrix of 256 x 256 unsigned short values...
The matrix can be implemented as a list of lists or a tuple...
So for example:
, , ]
or
((1,2,3),(4,5,6), (7,8,9))
This is what I have so...
|
by: Cox |
last post by:
Hello:
My address jsmith435@cox.net is subscribed to at least the PHP General
mailing list. I have for days now been trying to unsubscribe from all PHP
mail lists. I have followed the...
|
by: Michael M. |
last post by:
How to find the longst element list of lists?
I think, there should be an easier way then this:
s1 =
s2 =
s3 =
if len(s1) >= len(s2) and len(s1) >= len(s3):
sx1=s1 ## s1 ist längster
|
by: Dongsheng Ruan |
last post by:
with a cell class like this:
#!/usr/bin/python
import sys
class Cell:
def __init__( self, data, next=None ):
self.data = data
|
by: SM |
last post by:
I've always wonder if there is diference when declaring and
initializing a varible inside/outside a loop.
What's a better practice?
Declaring and initializing variables inside a loop routine,...
|
by: AZRebelCowgirl73 |
last post by:
This is what I have so far:
My program!
import java.util.*;
import java.lang.*;
import java.io.*;
import ch06.lists.*;
public class UIandDB {
|
by: pereges |
last post by:
Hi, I am wondering which of the two data structures (link list or
array) would be better in my situation. I have to create a list of
rays for my ray tracing program.
the data structure of ray...
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |