列表中的项目通过索引设置

时间:2013-04-24 20:09:12

标签: python list python-3.x

给出一个清单,

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)

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

3 个答案:

答案 0 :(得分:2)

您正在寻找sliding window generator代替:

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 sets,您需要小心您的术语。

具体来说,您没有在第二个索引到达列表末尾时进行测试:

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']]

答案 1 :(得分:1)

也许这会有所帮助

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

在此处运行代码http://codebunk.com/bunk#-IsxjI9Up1AgOfi0nuCd

答案 2 :(得分: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')}