473,659 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning the positions of a list that are non-zero

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
Jul 9 '08 #1
7 6294
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
Jul 9 '08 #2
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_dat a = [(index, value)
... for (index, value) in enumerate(spars e_data)
... if value != 0]
>>nonzero_dat a
[(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
Jul 9 '08 #3

This could work:

l = [0,0,1,2,1,0,0]
indexes, values = zip(*((index,va lue) 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
Jul 9 '08 #4
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.
Jul 9 '08 #5
On Jul 9, 7:48*am, "Rajanikant h Jammalamadaka" <rajanika...@gm ail.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. :)
Jul 9 '08 #6
On Jul 9, 7:48*am, "Rajanikant h Jammalamadaka" <rajanika...@gm ail.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]
Jul 9 '08 #7
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,locn s = zip(*[ (x,i) for i,x in enumerate(spars e_data) if x ])
print values
(1, 2, 1)
>>print locns
(2, 3, 4)
>>>
-- Paul
Jul 9 '08 #8

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

Similar topics

1
2214
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 within the original file. So e.g. <tagA><tagB>sometext</tagB></tagA> ^ ^ ^ ^ ^ ^ 0 6 12 19 26 33
12
2748
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 trying to get this to work, but been unable to solve my mistake. What the function should return is a file name used for creating logs files. It will look something like JN122345.log
3
1847
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 found this is done if funktion serach_char. so far all good what I want do next is: return, from funktion, pointer value to array were positions ( of found char) is stored. and print that array from main. but I only manage to print memory adress to...
4
28024
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
14
22557
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 first occurence. I want to know the positions of both "1234" thanks
15
13502
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 http://msdn2.microsoft.com/en-us/library/ms996381.aspx Formerly, with classic Microsoft DNA architecture, the ADO Recordset was a primary transport medium, recommended for transmitting data between application tiers. In fact, there are whole books written on the subject.
5
1691
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 want to display some of the serial number values on a form which will have a diagram overlaid (to map out the "locations"). I have been trying to write an If statement in the controlsource properties of the text boxes to say "if location=position1...
4
1564
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
23
2927
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 method. QUOTE ENDS HERE I have never heard anyone else say that it is a problem for a function
0
1713
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 a web service. Unfortunatly i get a NULL response and i can't understand why. RpcRsat.RSATWSPortType rpt = new RpcRsat.RSATWSPortTypeClient(); RpcRsat.retrieve_seqResponse res = rpt.retrieve_seq(req);
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7360
agi2029
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.