Hi,
is there a one-liner to accomplish the following task?
>From the list
l = ['string1', 'string2', 'string3']
generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]
Any help would be appreciated.
Thanks
Francesco 5 1100
cesco wrote:
l = ['string1', 'string2', 'string3']
l2=[l[:i+1] for i in range(len(l))]
On 9/16/07, cesco <fd**********@gmail.comwrote:
Hi,
is there a one-liner to accomplish the following task?
From the list
l = ['string1', 'string2', 'string3']
generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]
Any help would be appreciated.
Thanks
Francesco
>>l = [1,2,3,4,5]
>>[l[:i] for i in range(len(l))]
[[], [1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
>>>
well almost works except for the first empty list. [Are you sure you
dont want it?]
Corrected
>>[l[:i+1] for i in range(len(l)-1)]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
Though I wonder if there is as neat a way as the first?
Rustom Mody wrote:
On 9/16/07, cesco <fd**********@gmail.comwrote:
>Hi,
is there a one-liner to accomplish the following task?
>From the list
l = ['string1', 'string2', 'string3'] generate the list of lists l = [['string1'], ['string1', 'string2'], ['string1', 'string2', 'string3']]
Any help would be appreciated.
Thanks Francesco
>>>l = [1,2,3,4,5]
>>>[l[:i] for i in range(len(l))]
[[], [1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
well almost works except for the first empty list. [Are you sure you
dont want it?]
Corrected
>>>[l[:i+1] for i in range(len(l)-1)]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
Though I wonder if there is as neat a way as the first?
>>[l[:i] for i in range(1, len(l))]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
>>>
seems a slightly neater way to meet the requirement.
regards
steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Sorry, the dog ate my .sigline
cesco wrote:
Hi,
is there a one-liner to accomplish the following task?
>>From the list
l = ['string1', 'string2', 'string3']
generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]
Any help would be appreciated.
Thanks
Francesco
l = [l, l]
Ross
On Mon, 17 Sep 2007 20:52:14 +1000, Ross Wilson wrote:
cesco wrote:
>Hi,
is there a one-liner to accomplish the following task?
>>>From the list
l = ['string1', 'string2', 'string3'] generate the list of lists l = [['string1'], ['string1', 'string2'], ['string1', 'string2', 'string3']]
Any help would be appreciated.
Thanks Francesco
l = [l, l]
Ross
Did you *try* your suggestion before posting?
>>l = ['string1', 'string2', 'string3'] assert [l, l] == [['string1'], ['string1', 'string2'],
.... ['string1', 'string2', 'string3']]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
What you've suggested doesn't even come close to what the Original Poster
is asking for.
--
Steven This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chris Ritchey |
last post by:
Hmmm I might scare people away from this one just by the title, or
draw people in with a chalange :)
I'm writting this program in c++, however I'm using char* instead of
the string class, I am...
|
by: Chris Ritchey |
last post by:
Hmmm I might scare people away from this one just by the title, or
draw people in with a chalange :)
I'm writting this program in c++, however I'm using char* instead of
the string class, I am...
|
by: Girish Sahani |
last post by:
hello ppl,
Consider a list like . Here 'a','b','c' are
objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1
and b.1 cannot exist together. From this list i want to generate...
|
by: Girish Sahani |
last post by:
hello ppl,
Consider a list like . Here 'a','b','c' are
objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1
and b.1 cannot exist together. From this list i want to generate...
|
by: Grant Edwards |
last post by:
Could whoever is responsible for the gateway that is grabbing
my postings off of Usenet and e-mailing them out please fix the
headers in the mail messages so that I don't get the bounce
messages?...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: 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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |