[英]How to rearrange list in python
I want to rearrange my list in python but there are some rules about it.我想在 python 中重新排列我的列表,但有一些关于它的规则。 These next few pictures describe how beavers jump into a hole, while others go accross and the ones in the hole go out.
接下来的几张图片描述了海狸如何跳入洞中,而其他人则越过,而洞中的海狸则出去。
Beavers fill in the hole海狸填补了这个洞
Now there's where it gets complicated.现在事情变得复杂了。 What if i have more than 1 hole .
如果我有1 个以上的孔怎么办。 I've managed to figure out everything myself up to this point.
到目前为止,我已经设法自己弄清楚了一切。 Now all i'm asking is how would any of you approach this and if there's any tricks i could use for sorting the list.
现在我要问的是你们中的任何人将如何处理这个问题,如果有任何技巧我可以用来对列表进行排序。 This is my code so far:
到目前为止,这是我的代码:
bobri = "54321"
luknje = [4, 2, 4]
bobri_original = bobri
bobri_original_list = list(bobri_original)
bobri_list = list(bobri)
i = 1
gli = 0
for st in range(len(luknje)):
i = 1
globina = luknje[st]
# Prvega zapisem in zbrisem iz glavnega seznama
last_bober = bobri_list[-1]
bobri_list.remove(str(last_bober))
bobri_luknja = [last_bober]
if globina == 1:
for n in range(len(bobri_list)):
bobri_luknja.append(bobri_list[n])
bobri_list = bobri_luknja
else:
while i <= globina - 1:
last_bober = bobri_list[-1]
bobri_list.remove(str(last_bober))
bobri_luknja.append(last_bober)
if i == globina - 1:
for n in range(len(bobri_list)):
bobri_luknja.append(bobri_list[n])
bobri_list = bobri_luknja
i += 1
gli += 1
Zdravo (:兹德拉沃 (:
def cross_all_holes(initial_list, hole_depths=[]):
# Crossing single hole function.
def cross_the_hole(current_list, hole_depth):
hole_is_filled = False
list_len = len(current_list)
result_list = []
hole = []
# While not result list contain all the items
while len(result_list) < list_len:
# Filling the hole
if not hole_is_filled:
element = current_list.pop()
hole.append(element)
# Checking if it's filled
hole_is_filled = len(hole) == hole_depth
else:
# Crossing the filled hole
if len(current_list):
element = current_list.pop()
# Emptying hole
elif len(hole):
element = hole.pop()
result_list = [element] + result_list
return result_list
# Repeat as much as needed.
current_list = initial_list
for hole_depth in hole_depths:
current_list = cross_the_hole(current_list, hole_depth)
return current_list
Thanks for the task, it was funny (:感谢您的任务,这很有趣(:
In [20]: cross_all_holes([1,2,3,4,5,6,7,8,9,10], [1,3])
Out[20]: [9, 8, 7, 10, 1, 2, 3, 4, 5, 6]
Never thought I would write a function with inputs as 'beavers' and 'holes', but thanks for this moment.从没想过我会写一个输入为“海狸”和“洞”的函数,但感谢这一刻。 The beavers is simply the list of the number of each beaver, and holes is the list with each hole, and the amount of beavers getting into each:
海狸只是每个海狸数量的列表,孔是每个孔的列表,以及进入每个孔的海狸数量:
def rearrange(beavers, holes):
l = len(beavers)
for n in range(len(holes)):
back = beavers[-holes[n]:][::-1]
front = beavers[:l - holes[n]]
beavers = back + front
return(beavers)
rearrange([7,6,5,4,3,2,1], [3])
Out: [1, 2, 3, 7, 6, 5, 4]
rearrange([7,6,5,4,3,2,1], [3,4])
Out: [4, 5, 6, 7, 1, 2, 3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.