bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

利用Python怎么在list中查找出最長的單詞鏈-創新互聯

這期內容當中小編將會給大家帶來有關利用Python怎么在list中查找出最長的單詞鏈,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創新互聯從2013年成立,是專業互聯網技術服務公司,擁有項目網站制作、成都做網站網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元安徽做網站,已為上家服務,為安徽各地企業和個人服務,聯系電話:13518219792

例如:


words = ['giraffe', 'elephant', 'ant', 'tiger', 'racoon', 'cat', 'hedgehog', 'mouse']

最長的單詞鏈列表:

['hedgehog', 'giraffe', 'elephant', 'tiger', 'racoon']

1、用遞歸方法查找

words = ['giraffe', 'elephant', 'ant', 'tiger', 'racoon', 'cat', 'hedgehog', 'mouse']
def get_results(_start, _current, _seen):
 if all(c in _seen for c in words if c[0] == _start[-1]):
  yield _current
 else:
   for i in words:
    if i[0] == _start[-1]:
     yield from get_results(i, _current+[i], _seen+[i])

new_d = [list(get_results(i, [i], []))[0] for i in words]
final_d = max([i for i in new_d if len(i) == len(set(i))], key=len)

輸出結果:

['hedgehog', 'giraffe', 'elephant', 'tiger', 'racoon']


2、使用networkx查找

import networkx as nx
import matplotlib.pyplot as plt
words = ['giraffe', 'elephant', 'ant', 'tiger', 'racoon', 'cat',
     'hedgehog', 'mouse']
G = nx.DiGraph()
G.add_nodes_from(words)
for word1 in words:
  for word2 in words:
    if word1 != word2 and word1[-1] == word2[0]:
      G.add_edge(word1, word2)
nx.draw_networkx(G)
plt.show()
print(nx.algorithms.dag.dag_longest_path(G))

上述就是小編為大家分享的利用Python怎么在list中查找出最長的單詞鏈了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創新互聯行業資訊頻道。

當前題目:利用Python怎么在list中查找出最長的單詞鏈-創新互聯
路徑分享:http://vcdvsql.cn/article12/cdepdc.html

成都網站建設公司_創新互聯,為您提供網站導航電子商務關鍵詞優化企業建站網站設計公司域名注冊

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

搜索引擎優化