我有一个元素列表,我需要循环重新排列,以便我保持他们的顺序。问题看起来很简单,但我无法找到一种智能的编码方式。假设你有元素
1 2 3 4 o o o 5 6 7
o在数组中总是连续的,但我需要更改此数组,以便o(不一定是不同类型)以循环方式持续:
5 6 7 1 2 3 4 o o o
问题在于o可能也以循环方式连续。例如,
o o 1 2 3 4 5 6 7 o
有一种聪明的方法吗?我一直在查看来自itertools的cycle
,但截至目前我还没有工作实现,因为我所做的是无法处理最后一个案例。
更新
我有第一个工作实施:
def arrange2(nodes, contiguous):
arranged = []
size = len(nodes)
if nodes[0] in contiguous:
# obtain the id of the last interface node in nodes
id = None
for i in range(1, len(nodes)):
if nodes[i] not in contiguous:
id = i
break
# copy nodes to new list starting from the first node past id
for i in range(id, id + size):
arranged += [nodes[i % size]]
else:
# obtain the id of the last interface node in nodes
id = None
for i in range(size - 1, -1, -1):
if nodes[i] in contiguous:
id = i
break
# copy nodes to new list starting from the first node past id
for i in range(id+1, id + size+1):
arranged += [nodes[i % size]]
return arranged
print(arrange2([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [5, 6]))
这打印 [7,8,9,10,1,2,3,4,5,6]
答案 0 :(得分:1)
好的,基于你的实现,我有这个:
def arrange(nodes: list, contigious: list) -> list:
first_contigious = nodes.index(contigious[0])
last_contigious = nodes.index(contigious[-1])
if first_contigious < last_contigious:
# Normal situation
return nodes[last_contigious+1:] + nodes[:first_contigious] + contigious
else:
# The contigious part is cycling
return nodes[last_contigious+1:first_contigious] + contigious
编辑在评论中澄清后,这个有名的收藏品不必订购我有这个:
def arrange(nodes: list, contigious: set) -> list:
# Make sure that contigious is a set
contigious = set(contigious)
# Return if all nodes are in contigious or nodes are empty
if len(contigious.intersection(nodes)) == len(nodes) or not len(nodes):
return nodes
if nodes[0] in contigious and nodes[-1] in contigious:
# The contigious part is split and present on the beggining and the
# end of the nodes list
cut = next(i for i, x in enumerate(nodes) if x not in contigious)
# I move the nodes from the beggining to the end
return nodes[cut:] + nodes[:cut]
else:
# The contigious part is somewhere in the middle of the nodes list
# I need to find the end of contigious sequence
cut = next(i for i, x in enumerate(reversed(nodes)) if x in contigious)
cut = len(nodes) - cut
return nodes[cut:] + nodes[:cut]
注意:确保重要元素确实彼此相邻而不分散在3个或更多组中是你的工作。
答案 1 :(得分:1)
使用所有&#34; o&#34;是连续的,我实施了:
def rearrange(l):
i=0
found=False
while i<len(l):
if l[i]=="o":
found=True
l=l[i+1:]+l[:i+1]
i=0
else:
if found:
break
i+=1
return l
您遍历列表。在&#34; o&#34;的第一次出现i
时,您拆分列表并反转它。
l=["o","o",1,2,3,"o"]
是&#34;分裂&#34;在["o"] (l[:i+1])
和["o",1,2,3,"o"] (l[i+1:])
中。将两部分反转并将它们重新组合在一起,即可获得一个新列表["o",1,2,3,"o","o"]
这样,&#34; o&#34;被推到最后。
然后再次使用新列表重新开始。如果新列表没有以&#39; o&#39; (found
为True且l[i]!="o"
),表示已完成。
>>>l=[1,2,3,4,"o","o","o",5,6,7]
>>>print(rearrange(l))
[5, 6, 7, 1, 2, 3, 4, 'o', 'o', 'o']
>>>l=["o","o",1,2,3,"o"]
>>>print(rearrange(l))
[1, 2, 3, 'o', 'o', 'o']
要提供一个连续元素列表而不是&#34; o&#34;,这个小改动应该有效:
def rearrange(l,contiguous):
i=0
found=False
while i<len(l):
if l[i] in contiguous:
...