473,473 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

extracting lists from a sequence

1 New Member
Expand|Select|Wrap|Line Numbers
  1. def dis(self):
  2.         temp=[]
  3.         list=[]
  4.         for i in combilist:
  5.             print list
  6.             if i!=0:
  7.                 temp.append(i)
  8.             else:
  9.                 print temp
  10.                 list.append(temp)
  11.                 print list
  12.                 while temp:
  13.                     temp.pop()
  14.         return
combilist is....[1,0,2,0,3,4,5,0]
list should comes out as [[1],[2],[3,4,5]]
and temp is used for temporarily storage...

but the output is comin out to be sumthin unexpected.....
please tell me wat's the problem with the code...
why is the list not appending correctly????
Jul 5 '07 #1
3 1090
dshimer
136 Recognized Expert New Member
This is meant to be more informational than fancy and will simply deal with the loop itself as you have written it.

Expand|Select|Wrap|Line Numbers
  1.  >>> l=[]    #final list
  2. >>> t=[]    #temp list
  3. >>> for i in combilist:
  4. ...     if i!=0:
  5. ...         t.append(i)
  6. ...     elif t:    # only append temp list if there is something in it
  7. ...         l.append(t)
  8. ...         t=[]    # after it is appended, set it to empty
  9. ...         
  10. >>> print l
  11. [[1], [2], [3, 4, 5]]
Jul 5 '07 #2
elbin
27 New Member
This is meant to be more informational than fancy and will simply deal with the loop itself as you have written it.

Expand|Select|Wrap|Line Numbers
  1.  >>> l=[]    #final list
  2. >>> t=[]    #temp list
  3. >>> for i in combilist:
  4. ...     if i!=0:
  5. ...         t.append(i)
  6. ...     elif t:    # only append temp list if there is something in it
  7. ...         l.append(t)
  8. ...         t=[]    # after it is appended, set it to empty
  9. ...         
  10. >>> print l
  11. [[1], [2], [3, 4, 5]]
There is only one problem that may arise from that, and it is that you lose the last list of numbers if combilist does not end with 0. You can just add it if you are not sure if it will be there all the time:

Expand|Select|Wrap|Line Numbers
  1. if combilist[-1] != 0:
  2.     combilist.append(0)
  3.  
and then the rest of the code...
That's the simplest method I could think of around the problem :)
Jul 5 '07 #3
bvdet
2,851 Recognized Expert Moderator Specialist
There is only one problem that may arise from that, and it is that you lose the last list of numbers if combilist does not end with 0. You can just add it if you are not sure if it will be there all the time:

Expand|Select|Wrap|Line Numbers
  1. if combilist[-1] != 0:
  2.     combilist.append(0)
  3.  
and then the rest of the code...
That's the simplest method I could think of around the problem :)
Good point. You can also add an if statement after the for loop. Add another conditional to allow for the possibility of combilist starting with a 0:
Expand|Select|Wrap|Line Numbers
  1. >>> combilist = [0,0,1,0,2,0,3,4,5,0,6,7,8]
  2. >>> lst = []
  3. >>> tem = []
  4. >>> for i in combilist:
  5. ...     if i != 0:
  6. ...         tem.append(i)
  7. ...     else:
  8. ...         if len(tem):
  9. ...             lst.append(tem)
  10. ...             tem = []
  11. ...             
  12. >>> if i != 0:
  13. ...     lst.append(tem)
  14. ...     
  15. >>> print lst
  16. [[1], [2], [3, 4, 5], [6, 7, 8]]
  17. >>> 
Jul 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i...
1
by: svilen | last post by:
hi. this was named but it is misleading. i want to have the order of setting items in intrinsic dicts (keyword args, or class attributes, etc). which is, a prdered dict trhat matches the...
9
by: Ivan Voras | last post by:
I need to transform this: into this: (a left-rotation. Actually, any rotation will do).
3
by: Matjaz | last post by:
Dear all, I have run into a problem using python lists and sequence protocol. The first code snippet uses explicit list operations and works fine. PyObject *argseq, *ov; int i, v, len; ...
8
by: Charlotte Henkle | last post by:
Hello; I'm pondering how to count the number of times an item appears in total in a nested list. For example: myList=,,] I'd like to know that a appeared three times, and b appeared twice,...
4
by: Porthos | last post by:
Hi All, I've been working on mining data from a schema file (all attribute data so far) and have come to the point where I need to get information that is contained in tags. For instance,...
4
by: Jesse Noller | last post by:
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = list2 = list3 = main_list =
36
by: Digital Puer | last post by:
Hi, suppose I have an unsigned long long. I would like to extract the front 'n' bits of this value and convert them into an integer. For example, if I extract the first 3 bits, I would get an int...
14
by: Adnan Siddiqi | last post by:
Hi Suppose I have following URLs comming from an HTML document <a href="http://mydomain1.com">Domain1</a> <a...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
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
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...
1
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...
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...
0
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 project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
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...

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.