Reputation: 1396
Given a list,
a_list=['chicken','pizza','burger','beer','vodka','potato','fries','mustache']
I'm attempting to make a new set of each six word phrase...
a_set=(['chicken','pizza','burger','beer','vodka','potato'],['pizza','burger','beer','vodka','potato','fries],['burger','beer','vodka','potato','fries','mustache'])
I'm attempting to do this by indexing..
index1=0
index2=6
a_set=[]
while True:
a_set.append(a_list[index1:index2])
index1+=1
index2+=1
print (a_set)
I can't seem to figure out what I'm doing wrong. Also, how would I go about ending the loop once it has gone through and created all of the six word phrases and gotten to the end of the list so that it doesn't start from the beginning and do it all again? Thanks for any and all help.
Upvotes: 0
Views: 63
Reputation: 69042
If I understand you correctly, you want all the possible combinations of six items of your list. itertools.combinations
should make this very easy:
>>> import itertools
>>> a_list=['chicken','pizza','burger','beer','vodka','potato','fries','mustache']
>>> a_set=set(itertools.combinations(a_list, 6))
>>> pprint(a_set)
{('burger', 'beer', 'vodka', 'potato', 'fries', 'mustache'),
('chicken', 'beer', 'vodka', 'potato', 'fries', 'mustache'),
('chicken', 'burger', 'beer', 'potato', 'fries', 'mustache'),
('chicken', 'burger', 'beer', 'vodka', 'fries', 'mustache'),
('chicken', 'burger', 'beer', 'vodka', 'potato', 'fries'),
('chicken', 'burger', 'beer', 'vodka', 'potato', 'mustache'),
('chicken', 'burger', 'vodka', 'potato', 'fries', 'mustache'),
('chicken', 'pizza', 'beer', 'potato', 'fries', 'mustache'),
('chicken', 'pizza', 'beer', 'vodka', 'fries', 'mustache'),
('chicken', 'pizza', 'beer', 'vodka', 'potato', 'fries'),
('chicken', 'pizza', 'beer', 'vodka', 'potato', 'mustache'),
('chicken', 'pizza', 'burger', 'beer', 'fries', 'mustache'),
('chicken', 'pizza', 'burger', 'beer', 'potato', 'fries'),
('chicken', 'pizza', 'burger', 'beer', 'potato', 'mustache'),
('chicken', 'pizza', 'burger', 'beer', 'vodka', 'fries'),
('chicken', 'pizza', 'burger', 'beer', 'vodka', 'mustache'),
('chicken', 'pizza', 'burger', 'beer', 'vodka', 'potato'),
('chicken', 'pizza', 'burger', 'potato', 'fries', 'mustache'),
('chicken', 'pizza', 'burger', 'vodka', 'fries', 'mustache'),
('chicken', 'pizza', 'burger', 'vodka', 'potato', 'fries'),
('chicken', 'pizza', 'burger', 'vodka', 'potato', 'mustache'),
('chicken', 'pizza', 'vodka', 'potato', 'fries', 'mustache'),
('pizza', 'beer', 'vodka', 'potato', 'fries', 'mustache'),
('pizza', 'burger', 'beer', 'potato', 'fries', 'mustache'),
('pizza', 'burger', 'beer', 'vodka', 'fries', 'mustache'),
('pizza', 'burger', 'beer', 'vodka', 'potato', 'fries'),
('pizza', 'burger', 'beer', 'vodka', 'potato', 'mustache'),
('pizza', 'burger', 'vodka', 'potato', 'fries', 'mustache')}
Upvotes: 0
Reputation: 4224
Perhaps this would help
def get_set(li, phrase_len):
l = len(li)
for i in range(l):
if phrase_len <= l-i:
yield(li[i:i+phrase_len])
a_list=['chicken','pizza','burger','beer','vodka','potato','fries','mustache']
print list(get_set(a_list, 6))
Run the code here http://codebunk.com/bunk#-IsxjI9Up1AgOfi0nuCd
Upvotes: 1
Reputation: 1122032
You are looking for a sliding window generator instead:
from itertools import islice
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
list(window(a_list, 6))
which gives:
>>> list(window(a_list, 6))
[('chicken', 'pizza', 'burger', 'beer', 'vodka', 'potato'), ('pizza', 'burger', 'beer', 'vodka', 'potato', 'fries'), ('burger', 'beer', 'vodka', 'potato', 'fries', 'mustache')]
You are not creating python set
s here, you need to be careful with your terminology.
Specifically, you are not testing when the second index reaches the end of the list:
a_windows = []
index1 = 0
index2 = 6
while index2 <= len(a_list):
a_windows.append(a_list[index1:index2])
index1 += 1
index2 += 1
which works:
>>> a_windows
[['chicken', 'pizza', 'burger', 'beer', 'vodka', 'potato'], ['pizza', 'burger', 'beer', 'vodka', 'potato', 'fries'], ['burger', 'beer', 'vodka', 'potato', 'fries', 'mustache']]
Upvotes: 2