472,373 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

convert list of lists to list

Is there a way to convert list_of_listsA to list_of_listsB, where one
list in listof lists A is one element of listB?

list_of_listsA:
[['klas*', '*', '*'],
['mooi*', '*', '*', '*'],
['koe'],
['arm*', '*', '*(haar)'],
['groei*', '*', '*', '*', '*']]

listB:
['klas* * *', 'mooi* * * *, 'koe', 'arm* * * (haar)', 'groei* * * *
*']

Thankx!
Jul 22 '08 #1
4 1918
antar2 wrote:
Is there a way to convert list_of_listsA to list_of_listsB, where one
list in listof lists A is one element of listB?
list_of_listsA:
[['klas*', '*', '*'],
['mooi*', '*', '*', '*'],
['koe'],
['arm*', '*', '*(haar)'],
['groei*', '*', '*', '*', '*']]

listB:
['klas* * *', 'mooi* * * *, 'koe', 'arm* * * (haar)', 'groei* * * *
*']
if there's only one level of recursion, and the lists aren't too long,
you can simply do:

sum(list_of_lists, [])

(this has quadratic performance, so don't use it for large structures)

for recursive solutions, see:

http://www.google.com/search?q=flatten+lists+python

</F>

Jul 22 '08 #2
antar2 wrote:
Is there a way to convert list_of_listsA to list_of_listsB, where one
list in listof lists A is one element of listB?

list_of_listsA:
[['klas*', '*', '*'],
['mooi*', '*', '*', '*'],
['koe'],
['arm*', '*', '*(haar)'],
['groei*', '*', '*', '*', '*']]

listB:
['klas* * *', 'mooi* * * *, 'koe', 'arm* * * (haar)', 'groei* * * *
*']
>>outer = [['klas*', '*', '*'],
.... ['mooi*', '*', '*', '*'],
.... ['koe'],
.... ['arm*', '*', '*(haar)'],
.... ['groei*', '*', '*', '*', '*']]
>>[" ".join(inner) for inner in outer]
['klas* * *', 'mooi* * * *', 'koe', 'arm* * *(haar)', 'groei* * * * *']

Peter
Jul 22 '08 #3
Fredrik Lundh wrote:
>['klas* * *', 'mooi* * * *, 'koe', 'arm* * * (haar)', 'groei* * * *
*']

if there's only one level of recursion, and the lists aren't too long,
you can simply do:

sum(list_of_lists, [])
oops. that's what you get for taking the subject line too literally...

Jul 22 '08 #4
On Jul 22, 8:25*am, Fredrik Lundh <fred...@pythonware.comwrote:
if there's only one level of recursion, and the lists aren't too long,
you can simply do:

* * *sum(list_of_lists, [])

(this has quadratic performance, so don't use it for large structures)
For linear performance, you can use itertools:

list(itertools.chain(*list_of_lists))

Raymond
Jul 22 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jochen Hub | last post by:
Hi, I am a real beginner in Python and I was wondering if there is a way to convert a string (which contains a list) to a "real" list. I need this since I would like to give a list as an...
19
by: Oliver Eichler | last post by:
Hi, I can find examples to convert lists into a list of tuples like: >>> map(None,,) but how is it done vice versa? Oliver
16
by: flyaflya | last post by:
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3)
0
by: Andre Winarko | last post by:
How do you convert a date dd/mm/yyyy from a .txt file into mysql date format when using LOAD DATA INFILE ? __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free,...
2
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...
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
3
by: rh0dium | last post by:
Hi all, I am using pexpect to drive another tool. Some of the data I get back would be better suited as a list and I want to know a simple way to parse the data to push it into a list. For...
36
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...
2
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty well, but I am having trouble with generic lists,...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.