我不知道为什么我遇到这个问题,我是python的新手,但这个想法应该很简单。
我有一个元组列表,如:
(A,B),(B,C),(B,d),(B,A),(C,L),(d,Y),(Y,L)
给出一个起点,让我们说一个和一个终点l。
我想制作所有路径。 (A,B,C,L) (A,B,d,Y,L)
我做了很多尝试,他们都在打破无益的错误消息,如:
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
File "<stdin>", line 20, in findAllPaths
我认为每种方法都过于复杂。有一种简单的python方式吗?
编辑以增加一些清晰度:
我的目标是旅行元组以找到最终目标的所有不同(非圆形)路径。 所以在我上面的例子中,我想从一个 - &gt;旅行升。
因此,该计划将检查所有&#39; a&#39;连接并创建上面的新元组。
因此,程序将采用它可以的所有路径,直到找到l,如果路径用完,并且找不到它应该返回空列表的位置。
最后,a-&gt; b和b-&gt; a,但这将创建一个我们可以忽略的循环。
答案 0 :(得分:1)
您想将8个列表合并为2个列表吗?或者你想在这8个列表中查看重复的内容?我不明白(a,b,c,l) (a,b,d,y,l)
你是怎么得到的?
编辑好吧,写得非常快,你可能会发现它里面有一些奇怪的名字,而且它可以更优化,但它确实起作用,也是开始的好地方。
def check_connection(Connection_DNA_List, Preview=True):
DNA_Path = []
for DNA_tuples in Connection_DNA_List:
DNA_Path = check_DNA_List(newValue=DNA_tuples, Path_list=DNA_Path, Preview=False)
if Preview:
print DNA_Path
def check_DNA_List(newValue,Path_list, Preview=False):
FinalList = []
if Path_list:
for eachpath in Path_list:
newList, newfile = check_DNA(newValue=newValue, DNA_List=eachpath, Preview=False)
if newfile:
FinalList.append(eachpath)
if "A" in newList:
FinalList.append(newList)
else:
newList, newfile = check_DNA(newValue=newValue, DNA_List=Path_list, Preview=False)
if "A" in newList:
FinalList.append(newList)
FinalList.append(newList)
if Preview:
print "Current Path List: ", Path_list
print "New Value: %s %s" %newValue
print "New Path List: %s" %FinalList
print "------------------------------------"
return FinalList
def check_DNA(newValue, DNA_List, Preview=False):
newDNA = []
newList = False
if DNA_List:
if newValue[0] in DNA_List and newValue[1] in DNA_List:
newDNA = DNA_List
newList = False
elif newValue[0] in DNA_List[-1] and newValue[1] not in DNA_List:
newDNA = DNA_List
newDNA.append(newValue[1])
newList = False
else:
newList = True
newDNA.append(newValue[0])
newDNA.append(newValue[1])
else:
newList = True
newDNA.append(newValue[0])
newDNA.append(newValue[1])
if Preview:
print "Current List: %s" %DNA_List
print "New Value: %s %s" %newValue
print "Created List: %s" %newDNA
print "Is it a newList? %s" %newList
print "------------------------------------"
return newDNA ,newList
a = "A"
b = "B"
c = "C"
d = "D"
y = "Y"
l = "L"
DNAList = [(a,b),(b,c),(b,d),(b,a),(c,l),(d,y),(y,l)]
check_connection(Connection_DNA_List=DNAList)
注意:你发现很多预览,我经常把它们写进去调试,知道它在做什么是很好的方法,你可以把它们关掉,如果你不想看到它们的话
答案 1 :(得分:1)
这是一个相当简洁的方法:
CREATE TRIGGER STOCKGROUP
ON [dbo].[STOCK_ITEMS]
FOR UPDATE
AS
IF UPDATE (X_STOCK_GROUP3)
set STOCKGROUP = STOCK_GROUPS.GROUPNO
PRINT 'AFTER UPDATE Trigger fired.'
GO
...
def find_paths(segments, start, end):
full_paths = []
part_paths = [(start,)]
while len(part_paths) > 0:
new_part_paths = []
for p in part_paths:
for s in segments:
if s[0] == p[-1] and s[-1] not in p:
# segment s should be added to path p
new_path = p + s[1:]
if new_path[-1] == end:
full_paths += [new_path]
else:
new_part_paths += [new_path]
part_paths = new_part_paths
return full_paths
a = 'a'; b = 'b'; c = 'c'; d = 'd'; l = 'l'; y = 'y'
print find_paths([(a,b),(b,c),(b,d),(b,a),(c,l),(d,y),(y,l)], a, l)