[英]How to change occurrence of the same element in a list next to each other without deleting the element in python?
我有两个列表 list1=['A','B','C'] list2=['1','2','3'] 我需要一个长度为 'n 的长列表(由这两个列表组合而成) ' 没有
我想要任何顺序的组合列表的元素,但没有彼此相邻的重复项。 换句话说,就像这个例子一样,['A1','B2','C3','A2','B3','C1','A3','B1','C2','A1 ','B2','C3'.....]
有人可以帮帮我吗。 我从 2 天起一直在寻找答案。
编辑:
我尝试了 itertools 方法产品。
newlist = [ n[0]+n[1] for n in list(itertools.product(list1,list2))]
#这给出了我需要的元素的精确排列,但不是按照我希望的顺序排列。
newlist = ['a1','a2','a3','b1','b2','b3','c1','c2','c3']
#然后,我使用了嵌套循环,
#将newlist中的连续对元素交换为ind,n in enumerate(newlist): while n == newlist[ind-1]: for ind,n in enumerate(newlist): while n == newlist[ind-1] : newlist[ind-1],newlist[ind-2] = newlist[ind-2],newlist[ind-1]
#将newlist中连续的list1元素交换为ind,n in enumerate(newlist): while n[0] == newlist[ind-1][0]: for ind,n in enumerate(newlist): while n[0 ] == newlist[ind-1][0]: newlist[ind-1],newlist[ind-2] = newlist[ind-2],newlist[ind-1]
#将newlist中连续的list2元素交换为ind,n in enumerate(newlist): while n[1] == newlist[ind-1][1]: for ind,n in enumerate(newlist): while n[1 ] == newlist[ind-1][1]: newlist[ind-1],newlist[ind-2] = newlist[ind-2],newlist[ind-1]
显然,它适用于超过 3 个元素的列表。 但不适用于长度分别为 3 和 2 的列表。
我找到了你想要的解决方案。 jrook 指出的评论显示了与此类似的讨论,但在他们的情况下,它没有检查以确保它遵循您想要的所有参数。 所以我继续为它写了一些代码。
import itertools
list1 = ["A","B","C"]
list2 = ["1","2","3", "4"]
if len(list1) < len(list2):
x = list1[:]
list1 = list2
list2 = x
list3 = [zip(x,list2) for x in itertools.permutations(list1,len(list2))]
new = []
for lis in list3:
for i in lis:
new.append(i[1] + i[0])
else:
list3 = [zip(x,list2) for x in itertools.permutations(list1,len(list2))]
new = []
for lis in list3:
for i in lis:
new.append(i[0] + i[1])
final = []
final.append(new[0])
new = new[1:]
def add_to(new, final):
if final[-1][0] != new[0][0] and final[-1][1] != new[0][1] and final[-1] != new[0]:
final.append(new[0])
new = new[1:]
else:
b = new[0]
new = new[1:]
new.append(b)
return new, final
while new != []:
new, final = add_to(new, final)
print(final)
变量 final 是一个列表,它将确保遵循您想要的所有规则,即:没有重复,没有相同的连续字母或数字。 希望这就是你要找的。 我编辑了这个,现在你可以让第二个或第一个列表的长度更长,它会工作得很好。
#Join the given list
list(map("".join,(zip(["A", "B", "C"], ["1","2","3"]))))
# Given your list
l3=["A1","B1","C1","A2","B2"]
l2=["A1","A2","A3","B1","B2"]
l=["A1","A1","C3","B2","B2"]
# set() data structure ensure no duplicate
list(set(l))+list(set(l2))+list(set(l3))
# output should be:
# ['B2', 'C3', 'A1', 'B2', 'A3', 'A2', 'B1', 'A1', 'C1', 'B2', 'A2', 'B1', 'A1']
希望这有帮助。
#declaring 2 lists
x=['a','b','c']
y=[str(n) for n in range(1,5)]
li=[]
#Creating the pairs by shuffling two lists
if len(x)>len(y):
for m in range(len(x)):
for n in range(len(y)):
li.append(x[m]+y[n-1])
m-=1
else:
for m in range(len(y)):
for n in range(len(x)):
li.append(y[m]+x[n-1])
m-=1
def pair_check(li):
for ind,val in enumerate(li):
if li[ind]==li[ind-1] or li[ind][0]==li[ind-1][0] or li[ind][1]==li[ind-1][1]:
return True
def pair_swap(li):
for ind,val in enumerate(li):
while li[ind]==li[ind-1] or li[ind][0]==li[ind-1][0] or li[ind][1]==li[ind-1][1]:
li[ind-len(x)],li[ind]=li[ind],li[ind-(len(x))]
#functions that verifies and swaps the pairs in combined list
while pair_check(li):
pair_swap(li)
print (li)
#Now the list li contains the list satisfies all the 3 conditions.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.