| | |
how to generate subsets from a list?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 23
Reputation:
Solved Threads: 0
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
(
Thanks a lot,
girish
I tried the following for lists...for list of lists i do not know what to do
( Python Syntax (Toggle Plain Text)
#pruned_k is a list or list of lists for element in pruned_k: if len(element)=1: #generate all 2 size sets for i in range(0,len(pruned_k)-1,2) pruned_new.append(pruned_k[i:i+2]) else: #if pruned_k is list of lists e.g [['a','b'],['b','c']] pruned_new.append(
Thanks a lot,
girish
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.
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.
•
•
Join Date: May 2006
Posts: 23
Reputation:
Solved Threads: 0
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.

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.
![]() |
Similar Threads
- Forum Advertising Revenue (Social Media and Online Communities)
- To find the maximum subset sum in an array. (C)
- Manipulating a list to generate multiple lists (Python)
- Producing list of 2 length strings from list of 1 length strings (Python)
- Download links script (Perl)
- Need Help to Print Doubly Linked List(DLL) (Java)
Other Threads in the Python Forum
- Previous Thread: Simple file reading code
- Next Thread: simulation
Views: 3876 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
address anydbm app backend bash beginner bits calling class code conversion coordinates copy curves dictionary directory dynamic edit examples excel feet file float format ftp function generator gui halp homework http i/o images import info input ip itunes java keycontrol line linux list lists loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects py-mailer py2exe pygame pyqt python queue random rational recursion recursive scrolledtext server smtp split ssh statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update urllib urllib2 variable whileloop windows write wxpython






