這期內容當中小編將會給大家帶來有關利用Python怎么在list中查找出最長的單詞鏈,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
例如:
words = ['giraffe', 'elephant', 'ant', 'tiger', 'racoon', 'cat', 'hedgehog', 'mouse']
最長的單詞鏈列表:
['hedgehog', 'giraffe', 'elephant', 'tiger', 'racoon']
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']
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。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯