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装饰器入门学习教程(九步学习)
Jan 28 Python
python自定义异常实例详解
Jul 11 Python
python 中split 和 strip的实例详解
Jul 12 Python
python列表生成式与列表生成器的使用
Feb 23 Python
为什么Python中没有"a++"这种写法
Nov 27 Python
python通过ffmgep从视频中抽帧的方法
Dec 05 Python
OpenCV HSV颜色识别及HSV基本颜色分量范围
Mar 22 Python
python破解bilibili滑动验证码登录功能
Sep 11 Python
Python中BeautifuSoup库的用法使用详解
Nov 15 Python
python中有关时间日期格式转换问题
Dec 25 Python
iPython pylab模式启动方式
Apr 24 Python
Python结合百度语音识别实现实时翻译软件的实现
Jan 18 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使用get_class_methods()函数获取分类的方法
2016/07/20 PHP
Ajax+PHP实现的模拟进度条功能示例
2019/02/11 PHP
laravel框架select2多选插件初始化默认选中项操作示例
2020/02/18 PHP
Ext grid 添加右击菜单
2009/11/26 Javascript
jQuery语法总结和注意事项小结
2012/11/11 Javascript
JS/jQuery实现默认显示部分文字点击按钮显示全部内容
2013/05/13 Javascript
js获取select标签的值且兼容IE与firefox
2013/12/30 Javascript
你可能不知道的JavaScript的new Function()方法
2014/04/17 Javascript
如何用javascript计算文本框还能输入多少个字符
2015/07/29 Javascript
Bootstrap每天必学之js插件
2015/11/30 Javascript
jquery实现简单的遮罩层
2016/01/08 Javascript
基于css3新属性transform及原生js实现鼠标拖动3d立方体旋转
2016/06/12 Javascript
JS 拦截全局ajax请求实例解析
2016/11/29 Javascript
详解vue渲染函数render的使用
2017/12/12 Javascript
使用node.js实现微信小程序实时聊天功能
2018/08/13 Javascript
jQuery实现的模仿雨滴下落动画效果
2018/12/11 jQuery
JS Math对象与Math方法实例小结
2019/07/05 Javascript
layui多图上传实现删除功能的例子
2019/09/23 Javascript
layui form表单提交后实现自动刷新
2019/10/25 Javascript
JQuery 实现文件下载的常用方法分析
2019/10/29 jQuery
机器学习的框架偏向于Python的13个原因
2017/12/07 Python
Python随机函数random()使用方法小结
2018/04/29 Python
Python新手如何理解循环加载模块
2020/05/29 Python
如何将anaconda安装配置的mmdetection环境离线拷贝到另一台电脑
2020/10/15 Python
Python通过字典映射函数实现switch
2020/11/06 Python
Lands’ End英国官方网站:高质量男女服装
2017/10/07 全球购物
Hanky Panky官方网站:内衣和睡衣
2019/07/25 全球购物
项目资料员岗位职责
2013/12/10 职场文书
人力资源专员岗位职责
2014/01/30 职场文书
小学体育教学反思
2014/01/31 职场文书
道路建设实施方案
2014/03/18 职场文书
大学校务公开实施方案
2014/03/31 职场文书
校园绿化美化方案
2014/06/08 职场文书
2014业务员年终工作总结
2014/12/09 职场文书
redis 限制内存使用大小的实现
2021/05/08 Redis
Pygame Draw绘图函数的具体使用
2021/11/17 Python