Python 找出英文单词列表(list)中最长单词链


Posted in Python onDecember 14, 2020

本文主要介绍Python中单词字符串的列表(list),找出列表中所有单词中前一个单词首字母和后一个单词尾字母相同,组成最长的单词链方法代码,并且每个单词不能多次使用。

例如:

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 列表最长单词链内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
分析Python编程时利用wxPython来支持多线程的方法
Apr 07 Python
在GitHub Pages上使用Pelican搭建博客的教程
Apr 25 Python
python实现二维码扫码自动登录淘宝
Dec 27 Python
python实现二叉树的遍历
Dec 11 Python
python使用sqlite3时游标使用方法
Mar 13 Python
python批量赋值操作实例
Oct 22 Python
详解Python并发编程之从性能角度来初探并发编程
Aug 23 Python
解决python 读取 log日志的编码问题
Dec 24 Python
Pytorch 神经网络—自定义数据集上实现教程
Jan 07 Python
python 解决tqdm模块不能单行显示的问题
Feb 19 Python
pycharm无法安装第三方库的问题及解决方法以scrapy为例(图解)
May 09 Python
详解Python中*args和**kwargs的使用
Apr 07 Python
Python 排序最长英文单词链(列表中前一个单词末字母是下一个单词的首字母)
Dec 14 #Python
Python实现Kerberos用户的增删改查操作
Dec 14 #Python
python-地图可视化组件folium的操作
Dec 14 #Python
python多线程和多进程关系详解
Dec 14 #Python
Python Pandas list列表数据列拆分成多行的方法实现
Dec 14 #Python
pandas将list数据拆分成行或列的实现
Dec 13 #Python
pandas按照列的值排序(某一列或者多列)
Dec 13 #Python
You might like
php中时间轴开发(刚刚、5分钟前、昨天10:23等)
2011/10/03 PHP
PHP基于IMAP收取邮件的方法示例
2017/08/07 PHP
yii2的restful api路由实例详解
2019/05/14 PHP
兼容IE和Firefox的javascript获取iframe文档内容的函数
2011/08/15 Javascript
基于JQuery的模拟苹果桌面Dock效果(稳定版)
2012/10/15 Javascript
JS实现图片预加载无需等待
2012/12/21 Javascript
css3元素简单的闪烁效果实现(html5 jquery)
2013/12/28 Javascript
JSONP获取Twitter和Facebook文章数的具体步骤
2014/02/24 Javascript
js写的方法实现上传图片之后查看大图
2014/03/05 Javascript
jQuery基础知识小结
2014/12/22 Javascript
JavaScript监听文本框回车事件并过滤文本框空格的方法
2015/04/16 Javascript
ES6记录异步函数的执行时间详解
2016/08/31 Javascript
jQuery简单倒计时效果完整示例
2016/09/20 Javascript
微信小程序利用co处理异步流程的方法教程
2017/05/20 Javascript
在 Angular中 使用 Lodash 的方法
2018/02/11 Javascript
使用vue 国际化i18n 实现多实现语言切换功能
2018/10/11 Javascript
微信小程序template模版的使用方法
2019/04/13 Javascript
小程序最新获取用户昵称和头像的方法总结
2019/09/23 Javascript
django开发之settings.py中变量的全局引用详解
2017/03/29 Python
详解Python中where()函数的用法
2018/03/27 Python
PyQt5每天必学之切换按钮
2020/08/20 Python
Django forms组件的使用教程
2018/10/08 Python
Python数据可视化之画图
2019/01/15 Python
django-rest-framework解析请求参数过程详解
2019/07/18 Python
Python列表切片常用操作实例解析
2019/12/16 Python
Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】
2019/12/19 Python
Python celery原理及运行流程解析
2020/06/13 Python
Python 执行矩阵与线性代数运算
2020/08/01 Python
Philosophy美国官网:美国美容品牌
2016/08/15 全球购物
经济学人订阅:The Economist
2018/07/19 全球购物
What is the purpose of Void class? Void类的作用是什么?
2016/10/31 面试题
小学生民族团结演讲稿
2014/08/27 职场文书
介绍信样本
2015/01/31 职场文书
课外活动总结
2015/02/04 职场文书
python中pandas.read_csv()函数的深入讲解
2021/03/29 Python
分析SQL窗口函数之取值窗口函数
2022/04/21 Oracle