0

给定一个清单,

a_list=['chicken','pizza','burger','beer','vodka','potato','fries','mustache']

我正在尝试为每六个单词的短语制作一组新的...

a_set=(['chicken','pizza','burger','beer','vodka','potato'],['pizza','burger','beer','vodka','potato','fries],['burger','beer','vodka','potato','fries','mustache'])

我试图通过索引来做到这一点..

index1=0
index2=6
a_set=[]
while True:
    a_set.append(a_list[index1:index2])
    index1+=1
    index2+=1
print (a_set)

我似乎无法弄清楚我做错了什么。另外,一旦循环完成并创建了所有六个单词短语并到达列表的末尾,我将如何结束循环,以便它不会从头开始并再次执行所有操作?感谢您的任何帮助。

4

3 回答 3

2

您正在寻找一个滑动窗口生成器

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))    

这使:

>>> list(window(a_list, 6)) 
[('chicken', 'pizza', 'burger', 'beer', 'vodka', 'potato'), ('pizza', 'burger', 'beer', 'vodka', 'potato', 'fries'), ('burger', 'beer', 'vodka', 'potato', 'fries', 'mustache')]

你不是在这里创建python set你需要小心你的术语。

具体来说,当第二个索引到达列表末尾时,您不会进行测试:

a_windows = []
index1 = 0
index2 = 6

while index2 <= len(a_list):
    a_windows.append(a_list[index1:index2])
    index1 += 1
    index2 += 1

哪个有效:

>>> a_windows
[['chicken', 'pizza', 'burger', 'beer', 'vodka', 'potato'], ['pizza', 'burger', 'beer', 'vodka', 'potato', 'fries'], ['burger', 'beer', 'vodka', 'potato', 'fries', 'mustache']]
于 2013-04-24T20:15:12.317 回答
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

于 2013-04-24T20:30:44.247 回答
0

如果我理解正确,您需要列表中六项的所有可能组合。itertools.combinations应该使这很容易:

>>> 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')}
于 2013-04-24T20:59:28.880 回答