Returning the positions of a list that are non-zero

  • Thread starter Benjamin Goudey
  • Start date
B

Benjamin Goudey

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
 
R

Rajanikanth Jammalamadaka

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 != 0]

[2, 3, 4]

Cheers,

Raj

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



--
"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
 
L

Luis Zarrabeitia

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.
 
A

Andrii V. Mishkovskyi

2008/7/9 Benjamin Goudey said:
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)]

 
C

Chris

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 != 0]

[2, 3, 4]

Cheers,

Raj



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

--
"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. :)
 
C

Chris

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 != 0]

[2, 3, 4]

Cheers,

Raj



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

--
"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]
 
P

Paul McGuire

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top