将列表中的项目重复到所需的长度

时间:2019-02-25 10:31:57

标签: python

我有一个可用项目列表,可用于创建总长度为4的新列表。可用项目列表的长度从不超过4个项目。如果列表中的元素少于4个,我想用从开始元素开始的可用元素填充它。

示例1:

available_items = [4, 2]
Result -> [4, 2, 4, 2]

示例2:

available_items = [9, 3, 12]
Result -> [9, 3, 12, 9]

示例3:

available_items = [3]
Result -> [3, 3, 3, 3]

我觉得我的解决方案不是最优的,但是到目前为止,我发现没有什么比这更好的了:

available_items = [3, 5]
required_items = 4

if len(available_items) == 1:
  new_items = [available_items[0]] * required_items
else:
  new_items = available_items + []
  for i in range(required_items - len(available_items)):
    new_items.append(available_items[i])

print(new_items)

11 个答案:

答案 0 :(得分:7)

您可以使用itertools.cycle

例如:

from itertools import cycle

available_items_1 = cycle([4, 2])
available_items_2 = cycle([9, 3, 12])
available_items_3 = cycle([3])

n = 4

print([next(available_items_1)for i in range(n)])
print([next(available_items_2)for i in range(n)])
print([next(available_items_3)for i in range(n)])

输出:

[4, 2, 4, 2]
[9, 3, 12, 9]
[3, 3, 3, 3]

答案 1 :(得分:5)

使用一些整数数学而不是导入的替代解决方案:

def repeat_items(l, c):
    return l * (c // len(l)) + l[:(c % len(l))]

>>> repeat_items([1, 2, 3], 4)
[1, 2, 3, 1]

避免重复多余的元素(如果len(l)大而c小则特别有益)。

警告:不检查空列表

答案 2 :(得分:3)

一个不错的单行代码(无需任何进口)将是:

[available_items * required_items][0][:required_items]

在示例列表中对其进行测试,我们将获得您想要的结果

required_items = 4

available_items = [4, 2]
[available_items * required_items][0][:required_items]
# Result -> [4, 2, 4, 2]

available_items = [9, 3, 12]
[available_items * required_items][0][:required_items]
# Result -> [9, 3, 12, 9]

available_items = [3, 3, 3, 3]
[available_items * required_items][0][:required_items]
# Result -> [3, 3, 3, 3]

答案 3 :(得分:3)

尝试一下:

lst1=[4,2]
lst2=[9, 3, 12]
lst3=[3]
no_item=4
print([lst1[i%len(lst1)] for i in range(no_item)])
print([lst2[i%len(lst2)] for i in range(no_item)])
print([lst3[i%len(lst3)] for i in range(no_item)])

答案 4 :(得分:2)

总是将您的输入乘以并限制结果会更简单。 范例1:

>> available_items = [4, 2]
>> Result = available_items * 4
>> Result = Result[0:4]
>> Result
[4, 2, 4, 2]

示例2:

>> available_items = [9, 3, 12]
>> Result = available_items * 4
>> Result = Result[0:4]
>> Result
[9, 3, 12, 9]

示例3:

>> available_items = [3]
>> Result = available_items * 4
>> Result = Result[0:4]
>> Result
[3, 3, 3, 3]

答案 5 :(得分:2)

def solve(arr):
    res = []
    req = 4-len(arr)

    while req>0:
      for i in range(len(arr)):
        res.append(arr[i])
        req = req-1
        if req == 0:
          break

    return arr+res

num = [4, 2]
res_num = solve(num)
print(res_num)

num = [9, 3, 12]
res_num = solve(num)
print(res_num)

num = [3]
res_num = solve(num)
print(res_num)

输出:

[4, 2, 4, 2]
[9, 3, 12, 9]
[3, 3, 3, 3]

答案 6 :(得分:2)

这是另一个班轮:

result = (available_items * (int(required_items /len(available_items ))+1))[:required_items]

无论required_itemsavailable_items,这都应该起作用。

答案 7 :(得分:2)

使用itertools.cycle,您可以这样做:

from itertools import cycle

available_items = [3, 5]
required_items = 4

[item for item, idx in zip(cycle(available_items), range(required_items))]

# [3, 5, 3, 5]

答案 8 :(得分:2)

只需将列表重复4次并将其缩减为4个元素:

>>> i = [4, 2]
>>> (i * 4)[:4]
[4, 2, 4, 2]

答案 9 :(得分:2)

您可以使用itertools.cycleitertools.islice创建所需的列表。 list(islice(cycle(available_items), 4))

>>> lst = [1]; list(islice(cycle(lst), 4)) == [1,1,1,1]
>>> lst = [1,3]; list(islice(cycle(lst), 4)) == [1,3,1,3]
>>> lst = [1,3,2]; list(islice(cycle(lst), 4)) == [1,3,2,1]
>>> lst = [1,3,2,4]; list(islice(cycle(lst), 4)) == [1,3,2,4]

答案 10 :(得分:1)

这是我的尝试

available_items = [3, 5, 9]
required_items = 4

times = 4 // len(available_items)
remain = 4 % len(available_items)

new_items = (available_items * times ) + available_items[:remain]
print(available_items)
print(new_items)