I have a very large list of integers representing data needed for a
histogram that I'm going to plot using pylab. However, most of these
values (85%-95%) are zero and I would like to remove them to reduce
the amount of memory I'm using and save time when it comes to plotting
the data. To do this, I'm trying to find the best way to remove all of
the zero values and produce a list of indices of where the non-zero
values used to be.
For example, if my original list is [0,0,1,2,1,0,0] I would like to
produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices
of where the non-zero values used to be). Removing non-zero values is
very easy but determining the indicies is where I'm having difficulty.
Thanks in advance for any help 7 6282
Try this:
>>li=[0,0,1,2,1,0,0] li
[0, 0, 1, 2, 1, 0, 0]
>>[i for i in range(len(li)) if li[i] != 0]
[2, 3, 4]
Cheers,
Raj
On Tue, Jul 8, 2008 at 10:26 PM, Benjamin Goudey <bw******@gmail.comwrote:
I have a very large list of integers representing data needed for a
histogram that I'm going to plot using pylab. However, most of these
values (85%-95%) are zero and I would like to remove them to reduce
the amount of memory I'm using and save time when it comes to plotting
the data. To do this, I'm trying to find the best way to remove all of
the zero values and produce a list of indices of where the non-zero
values used to be.
For example, if my original list is [0,0,1,2,1,0,0] I would like to
produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices
of where the non-zero values used to be). Removing non-zero values is
very easy but determining the indicies is where I'm having difficulty.
Thanks in advance for any help
-- http://mail.python.org/mailman/listinfo/python-list
--
"For him who has conquered the mind, the mind is the best of friends;
but for one who has failed to do so, his very mind will be the
greatest enemy."
Rajanikanth
Benjamin Goudey <bw******@gmail.comwrites:
For example, if my original list is [0,0,1,2,1,0,0] I would like to
produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices
of where the non-zero values used to be). Removing non-zero values
is very easy but determining the indicies is where I'm having
difficulty.
The built-in 'enumerate' function is made for this. Creating the new
list can be done efficiently and clearly with a list comprehension.
Further, presumably you want to do further operations on these new
series of data; wouldn't it be better to have the values and original
indices actually correlated together?
This code produces a two-element tuple for each original non-zero
value:
>>sparse_data = [0, 0, 1, 2, 1, 0, 0] nonzero_data = [(index, value)
... for (index, value) in enumerate(sparse_data)
... if value != 0]
>>nonzero_data
[(2, 1), (3, 2), (4, 1)]
>>for (index, value) in nonzero_data:
... print "Value %(value)r was originally at index %(index)r" % vars()
...
Value 1 was originally at index 2
Value 2 was originally at index 3
Value 1 was originally at index 4
--
\ “Experience is that marvelous thing that enables you to |
`\ recognize a mistake when you make it again.” —Franklin P. Jones |
_o__) |
Ben Finney
This could work:
l = [0,0,1,2,1,0,0]
indexes, values = zip(*((index,value) for index,value in enumerate(l) if value
!= 0))
But I guess it would be a little less cryptic (and maybe a lot more efficient)
if there were an unzip function instead of using the zip(*sequence) trick..
I think a more readable way would be:
indexes = [index for index,value in enumerate(l) if value != 0]
values = [value for value in l if value != 0]
Cheers.
--
Luis Zarrabeitia
Facultad de Matemtica y Computacin, UH http://profesores.matcom.uh.cu/~kyrie
Quoting Benjamin Goudey <bw******@gmail.com>:
I have a very large list of integers representing data needed for a
histogram that I'm going to plot using pylab. However, most of these
values (85%-95%) are zero and I would like to remove them to reduce
the amount of memory I'm using and save time when it comes to plotting
the data. To do this, I'm trying to find the best way to remove all of
the zero values and produce a list of indices of where the non-zero
values used to be.
For example, if my original list is [0,0,1,2,1,0,0] I would like to
produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices
of where the non-zero values used to be). Removing non-zero values is
very easy but determining the indicies is where I'm having difficulty.
Thanks in advance for any help
-- http://mail.python.org/mailman/listinfo/python-list
2008/7/9 Benjamin Goudey <bw******@gmail.com>:
I have a very large list of integers representing data needed for a
histogram that I'm going to plot using pylab. However, most of these
values (85%-95%) are zero and I would like to remove them to reduce
the amount of memory I'm using and save time when it comes to plotting
the data. To do this, I'm trying to find the best way to remove all of
the zero values and produce a list of indices of where the non-zero
values used to be.
For example, if my original list is [0,0,1,2,1,0,0] I would like to
produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices
of where the non-zero values used to be). Removing non-zero values is
very easy but determining the indicies is where I'm having difficulty.
Thanks in advance for any help
>>l = [0, 0, 1, 2, 1, 0, 0] zip(*[(item, index) for (index, item) in enumerate(l) if item != 0])
[(1, 2, 1), (2, 3, 4)]
-- http://mail.python.org/mailman/listinfo/python-list
--
Wbr, Andrii Mishkovskyi.
He's got a heart of a little child, and he keeps it in a jar on his desk.
On Jul 9, 7:48*am, "Rajanikanth Jammalamadaka" <rajanika...@gmail.com>
wrote:
Try this:
>li=[0,0,1,2,1,0,0] li
[0, 0, 1, 2, 1, 0, 0]>>[i for i in range(len(li)) if li[i] != 0]
[2, 3, 4]
Cheers,
Raj
On Tue, Jul 8, 2008 at 10:26 PM, Benjamin Goudey <bwgou...@gmail.comwrote:
I have a very large list of integers representing data needed for a
histogram that I'm going to plot using pylab. However, most of these
values (85%-95%) are zero and I would like to remove them to reduce
the amount of memory I'm using and save time when it comes to plotting
the data. To do this, I'm trying to find the best way to remove all of
the zero values and produce a list of indices of where the non-zero
values used to be.
For example, if my original list is [0,0,1,2,1,0,0] I would like to
produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices
of where the non-zero values used to be). Removing non-zero values is
very easy but determining the indicies is where I'm having difficulty.
Thanks in advance for any help
-- http://mail.python.org/mailman/listinfo/python-list
--
"For him who has conquered the mind, the mind is the best of friends;
but for one who has failed to do so, his very mind will be the
greatest enemy."
Rajanikanth
That's a waste
>>li=[0,0,1,2,1,0,0] [i for i in li if i]
That's all you need. :)
On Jul 9, 7:48*am, "Rajanikanth Jammalamadaka" <rajanika...@gmail.com>
wrote:
Try this:
>li=[0,0,1,2,1,0,0] li
[0, 0, 1, 2, 1, 0, 0]>>[i for i in range(len(li)) if li[i] != 0]
[2, 3, 4]
Cheers,
Raj
On Tue, Jul 8, 2008 at 10:26 PM, Benjamin Goudey <bwgou...@gmail.comwrote:
I have a very large list of integers representing data needed for a
histogram that I'm going to plot using pylab. However, most of these
values (85%-95%) are zero and I would like to remove them to reduce
the amount of memory I'm using and save time when it comes to plotting
the data. To do this, I'm trying to find the best way to remove all of
the zero values and produce a list of indices of where the non-zero
values used to be.
For example, if my original list is [0,0,1,2,1,0,0] I would like to
produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices
of where the non-zero values used to be). Removing non-zero values is
very easy but determining the indicies is where I'm having difficulty.
Thanks in advance for any help
-- http://mail.python.org/mailman/listinfo/python-list
--
"For him who has conquered the mind, the mind is the best of friends;
but for one who has failed to do so, his very mind will be the
greatest enemy."
Rajanikanth
Whoops, misread the question
li =[0,0,1,2,1,0,0]
[(index,data) for index,data in enumerate(li) if data]
On Jul 9, 12:26*am, Benjamin Goudey <bwgou...@gmail.comwrote:
I have a very large list of integers representing data needed for a
histogram that I'm going to plot using pylab. However, most of these
values (85%-95%) are zero and I would like to remove them to reduce
the amount of memory I'm using and save time when it comes to plotting
the data. To do this, I'm trying to find the best way to remove all of
the zero values and produce a list of indices of where the non-zero
values used to be.
For example, if my original list is [0,0,1,2,1,0,0] I would like to
produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices
of where the non-zero values used to be). Removing non-zero values is
very easy but determining the indicies is where I'm having difficulty.
>>sparse_data = [0, 0, 1, 2, 1, 0, 0] values,locns = zip(*[ (x,i) for i,x in enumerate(sparse_data) if x ]) print values
(1, 2, 1)
>>print locns
(2, 3, 4)
>>>
-- Paul This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Colin Green |
last post by:
OK here's is what I wish to do. I have an XML file that I want to read
into an XmlDocument. I then want to be able to interrogate the
XmlNodes to find both their start AND end character positions...
|
by: LongBow |
last post by:
Hello all,
From doing a google serach in the newsgroups I found out that a
string can't be returned from a function, but using a char* I should
be able to do it. I have spent most of the day...
|
by: Carramba |
last post by:
hi!
the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question
Iam trying to make program were I enter string and serach char.
and funktion prints out witch position char is...
|
by: Sasidhar Parvatham |
last post by:
Hi All,
How do I compare two strings and get all the positions where there is a
difference between them?
Thanks,
Sasidhar
|
by: micklee74 |
last post by:
hi
say i have string like this
astring = 'abcd efgd 1234 fsdf gfds abcde 1234'
if i want to find which postion is 1234, how can i achieve this...? i
want to use index() but it only give me the...
| |
by: Joseph Geretz |
last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or
Datatables between application tiers.
http://support.microsoft.com/kb/306134
...
|
by: littlevikinggirl |
last post by:
Hi,
I posted a badly worded question last week so got no replies and am
still struggling to figure out the problem myself. I have a table
containing two fields, Location and Serial Number. I...
|
by: bullockbefriending bard |
last post by:
Given:
class Z(object):
various defs, etc.
class ZList(list):
various defs, etc.
i would like to be able to replace
|
by: pauldepstein |
last post by:
Below is posted from a link for Stanford students in computer
science.
QUOTE BEGINS HERE
Because of the risk of misuse, some experts recommend never returning
a
reference from a function or...
|
by: Algobardo |
last post by:
Good morning,
this is the first time i write on this forum because i googled and i've seen related post with no solution.
I will expose briefly the problem.
I'm using c# 3.5 and i'm trying using...
|
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: 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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |