473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Nested lists, simple though


Im a bit new to python. Anyway working on a little project of mine and i
have nested lists

ie

Answer = [['computer', 'radeon', 'nvidia'], ['motherboard', 'asus']]

and so forth..,
Anyway the amount of [[]] do increase over time. Im just wondering is there
a simple way to add these together so they become 1 simple list, so it would
be ['computer'....'asus'] etc without the nested list. Its random the
amount each time so i cant just go a[0]+a[1].
Thank you if you can help
--
View this message in context: http://www.nabble.com/Nested-lists%2...p16799674.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #1
7 2166
On Apr 21, 12:25 am, Zethex <zet...@hotmail.comwrote:
Anyway the amount of [[]] do increase over time. Im just wondering is there
a simple way to add these together so they become 1 simple list, so it would
be ['computer'....'asus'] etc without the nested list. Its random the
amount each time so i cant just go a[0]+a[1].
Thank you if you can help

Does this answer your question?
>>a = [1,2,3]
a.extend([4,5,6])
a
[1, 2, 3, 4, 5, 6]

Jun 27 '08 #2
On Apr 20, 3:25*pm, Zethex <zet...@hotmail.comwrote:
Im a bit new to python. *Anyway working on a little project of mine and i
have nested lists

ie

Answer = [['computer', 'radeon', 'nvidia'], ['motherboard', 'asus']]

and so forth..,
Anyway the amount of [[]] do increase over time. *Im just wondering is there
a simple way to add these together so they become 1 simple list, so it would
be ['computer'....'asus'] etc without the nested list. *Its random the
amount each time so i cant just go a[0]+a[1].
Thank you if you can help
--
View this message in context:http://www.nabble.com/Nested-lists%2...6799674p167996...
Sent from the Python - python-list mailing list archive at Nabble.com.
The first idea that comes to mind is reduce(lambda x, y: x + y,
list_of_lists, [])
Jun 27 '08 #3
On Apr 20, 6:50*pm, Jason Scheirer <jason.schei...@gmail.comwrote:
On Apr 20, 3:25*pm, Zethex <zet...@hotmail.comwrote:
Im a bit new to python. *Anyway working on a little project of mine and i
have nested lists
ie
Answer = [['computer', 'radeon', 'nvidia'], ['motherboard', 'asus']]
and so forth..,
Anyway the amount of [[]] do increase over time. *Im just wondering isthere
a simple way to add these together so they become 1 simple list, so it would
be ['computer'....'asus'] etc without the nested list. *Its random the
amount each time so i cant just go a[0]+a[1].
Thank you if you can help
--
The first idea that comes to mind is reduce(lambda x, y: x + y,
list_of_lists, [])
s/first/worst/

There, I fixed it for you.
Jun 27 '08 #4
On 2008-04-20, Zethex <ze****@hotmail.comwrote:
Im a bit new to python. Anyway working on a little project of mine and i
have nested lists
What you want to do is usually called "flattening".

http://mail.python.org/pipermail/tut...ry/002914.html
http://aspn.activestate.com/ASPN/Coo.../Recipe/363051
http://aspn.activestate.com/ASPN/Coo.../Recipe/121294

--
Grant Edwards grante Yow! HAIR TONICS, please!!
at
visi.com
Jun 27 '08 #5
On Apr 21, 12:25 am, Zethex <zet...@hotmail.comwrote:
Anyway the amount of [[]] do increase over time.
You can flatten a nested list using a closure and recursion:

def flatten(lst):
tmp = []
def _flatten(lst):
for elem in lst:
if type(elem) != list:
tmp.append(elem)
else:
_flatten(elem)
_flatten(lst)
return tmp
However, CPython does not recurse very fast, but Cython and Pyrex do.
First, get rid of the closure, it is not supported in Cython or Pyrex
(at least not yet). Then change the recursive function to a cdef, i.e.
a normal C function which is called without the overhead of Python's
attribute lookup:

cdef _flatten(lst, tmp):
for elem in lst:
if type(elem) != list:
tmp.append(elem)
else:
_flatten(elem, tmp)

def flatten(lst):
tmp = []
_flatten(lst, tmp)
return tmp

Compile this with Cython or Pyrex, and you get a very fast nested list
flattener.

This also shows how easy it is to boost the performance of Python code
using Cython.

Jun 27 '08 #6
On Apr 21, 2:35 am, sturlamolden <sturlamol...@yahoo.nowrote:
This also shows how easy it is to boost the performance of Python code
using Cython.
We can improve this further by getting rid of the tmp.append attribue
lookup:

cdef _flatten(lst, append):
for elem in lst:
if type(elem) != list:
append(elem)
else:
_flatten(elem, append)

def flatten(lst):
tmp = []
_flatten(lst, tmp.append)
return tmp

Jun 27 '08 #7
The first idea that comes to mind is reduce(lambda x, y: x + y,
list_of_lists, [])
Which is not helping for arbitrary nested lists, as the OP wanted.

Diez
Jun 27 '08 #8

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

Similar topics

5
by: naturalborncyborg | last post by:
Hi, I'm using nested lists as arrays and having some problems with that approach. In my puzzle class there is a swapelement method which doesn't work out. Any help and comments on the code will be...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
5
by: John Topley | last post by:
Hi, I'm doing some work in an intranet environment where I'm forced to use IE 6.0. Is it possible to style (unordered) nested lists so that the inner list items have a different appearance to...
4
by: steven | last post by:
Hi, I want to make nested lists like <ul id="nested"> <li>level1 item</li> <ul> <li>level2 item</li> <li>level2 item</li> </ul> <li>level1 item</li>
6
by: Licheng Fang | last post by:
I wanna use nested lists as an array, but here's the problem: >>> a = *3]*3 >>> a , , ] >>> a = 1 >>> a , , ] Could anybody please explain to me why three values were change? I'm
4
by: morrhamen | last post by:
Hello! This is actually a reply to post: http://www.thescripts.com/forum/threadnav100612-1-10.html from July 2005, to which I can't reply. The following code does "Automated numbering of...
0
by: TAL651 | last post by:
Hello, I am trying to get an effect where I use nested lists, but they are arranged horizontally, like so: Item 1......1.a ...............1.b Item 2......2.a ...............2.b
4
by: RossRGK | last post by:
I'm having trouble getting my head around a solution for a situation where I need to flexibly format some text with a varying number of embedded fields. Here's a simplified description of my...
0
Norris
by: Norris | last post by:
Hi everyone ! In first sorry for my bad english, I'm french. I try to make a custom TreeView and a custom TreeNode. For storing child nodes in my customTreeNode, I use a List(Of MyTreeNode). I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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 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.