0

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Bob
  • 1,344
  • 3
  • 29
  • 63
  • 1
    I don't see any sets. I only see lists in your examples. Are you certain you are not confused about the [Python `set` type](http://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset)? You set `a_set` to a *`list`*. – Martijn Pieters Apr 24 '13 at 20:12

3 Answers3

2

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 sets 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']]
Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

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

spicavigo
  • 4,116
  • 22
  • 28
0

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')}
mata
  • 67,110
  • 10
  • 163
  • 162