Python。按另一个列表中指定的随机集大小对列表进行索引
Python. Index through a list by random set size specified in another list
我想遍历不同集合大小的单词列表,集合大小的顺序在另一个列表中指定。我想在每次试听后跳转到词表中的生词。
例如,我有
stimuli_words = ["tree","bird","cake","ocean","dance","statistics","headphone", "red","duck"]
我有set_size = [2, 4, 3]
我使用 chosen_size = +1
遍历 set_size 列表。我希望第一次显示“树”、“鸟”,第二次显示“蛋糕”、“海洋”、“舞蹈”、“统计”,第三次显示“耳机”、“红色”、“鸭子” ",我该怎么做?
我认为辅助函数比较合适:
def chunks_with_sizes(li, sizes):
i = 0
for sz in sizes:
yield li[i:i+sz]
i += sz
然后:
>>> list(chunks_with_sizes(stimuli_words, set_size))
[['tree', 'bird'], ['cake', 'ocean', 'dance', 'statistics'], ['headphone', 'red', 'duck']]
我想遍历不同集合大小的单词列表,集合大小的顺序在另一个列表中指定。我想在每次试听后跳转到词表中的生词。
例如,我有
stimuli_words = ["tree","bird","cake","ocean","dance","statistics","headphone", "red","duck"]
我有set_size = [2, 4, 3]
我使用 chosen_size = +1
遍历 set_size 列表。我希望第一次显示“树”、“鸟”,第二次显示“蛋糕”、“海洋”、“舞蹈”、“统计”,第三次显示“耳机”、“红色”、“鸭子” ",我该怎么做?
我认为辅助函数比较合适:
def chunks_with_sizes(li, sizes):
i = 0
for sz in sizes:
yield li[i:i+sz]
i += sz
然后:
>>> list(chunks_with_sizes(stimuli_words, set_size))
[['tree', 'bird'], ['cake', 'ocean', 'dance', 'statistics'], ['headphone', 'red', 'duck']]