944,062 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5766
  • Python RSS
May 25th, 2006
0

how to generate subsets from a list?

Expand Post »
If i have a list (or list of lists) with elements of size k and i want to generate all higher level elements (of size k + 1) from it,what should i do??Any pointers would be of a great help
I tried the following for lists...for list of lists i do not know what to do (
Python Syntax (Toggle Plain Text)
  1. #pruned_k is a list or list of lists
  2. for element in pruned_k:
  3. if len(element)=1: #generate all 2 size sets
  4. for i in range(0,len(pruned_k)-1,2)
  5. pruned_new.append(pruned_k[i:i+2])
  6. else: #if pruned_k is list of lists e.g [['a','b'],['b','c']]
  7. pruned_new.append(

Thanks a lot,
girish
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
girish_sahani is offline Offline
23 posts
since May 2006
May 25th, 2006
0

Re: how to generate subsets from a list?

You need to give more details of what you want to accomplish.

At first look, there are quite a few errors in your code. Your indentations are a mess (they are not cosmetic, they actually form statement blocks in Python!), if needs == as comparison operator, for needs colon at the end of line, and pruned_new has to be initiated as empty list.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
May 26th, 2006
0

Re: how to generate subsets from a list?

sorry for the unindented code
what i want to do is generate 2 size sets from a list of single elements,generate 3 size sets from a list of double elements(each element is a list of 2) and so on...
e.g [['a','b'],['a','c']] should give [['a','b','c']]
for list of single elements i can easily generate sets of 2 elements bu iterating over the list...but for lists of lists i am not able to do that...
one thing i thought of is to 'flatten' the list of lists (convert it into a list) after getting the length of its elements (suppose k) ,remove duplicates,and then generate the k+1 size sets from the flattened list.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
girish_sahani is offline Offline
23 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Simple file reading code
Next Thread in Python Forum Timeline: simulation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC