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 相关文章推荐
Pyhthon中使用compileall模块编译源文件为pyc文件
Apr 28 Python
Linux系统上Nginx+Python的web.py与Django框架环境
Dec 25 Python
Pycharm学习教程(4) Python解释器的相关配置
May 03 Python
pandas数据处理基础之筛选指定行或者指定列的数据
May 03 Python
Selenium定位元素操作示例
Aug 10 Python
python查找指定文件夹下所有文件并按修改时间倒序排列的方法
Oct 21 Python
python实现二维数组的对角线遍历
Mar 02 Python
Python处理时间日期坐标轴过程详解
Jun 25 Python
python 实现GUI(图形用户界面)编程详解
Jul 17 Python
使用Python实现文字转语音并生成wav文件的例子
Aug 08 Python
Django项目主urls导入应用中views的红线问题解决
Aug 10 Python
python GUI库图形界面开发之PyQt5控件QTableWidget详细使用方法与属性
Feb 25 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遍历文件夹所有文件子文件夹函数代码
2013/11/27 PHP
ThinkPHP文件缓存类代码分享
2015/04/22 PHP
Thinkphp自定义生成缩略图尺寸的方法
2019/08/05 PHP
window.event.keyCode兼容IE和Firefox实现js代码
2013/05/30 Javascript
js window.open弹出新的网页窗口
2014/01/16 Javascript
基于javascript实现窗口抖动效果
2016/01/03 Javascript
angularjs表格分页功能详解
2016/01/21 Javascript
解决微信浏览器Javascript无法使用window.location.reload()刷新页面
2016/06/21 Javascript
jQuery对checkbox 复选框的全选全不选反选的操作
2016/08/09 Javascript
AngularJS使用ng-options指令实现下拉框
2016/08/23 Javascript
纯JS实现简单的日历
2017/06/26 Javascript
如何将 jQuery 从你的 Bootstrap 项目中移除(取而代之使用Vue.js)
2017/07/17 jQuery
javascript流程控制语句集合
2017/09/18 Javascript
Bootstrap一款超好用的前端框架
2017/09/25 Javascript
vue项目中使用axios上传图片等文件操作
2017/11/02 Javascript
angular2路由之routerLinkActive指令【推荐】
2018/05/30 Javascript
Vue作用域插槽slot-scope实例代码
2018/09/05 Javascript
vue实现日历备忘录功能
2020/09/24 Javascript
详解ES6数组方法find()、findIndex()的总结
2020/05/12 Javascript
python每次处理固定个数的字符的方法总结
2013/01/29 Python
老生常谈Python startswith()函数与endswith函数
2017/09/08 Python
Python3 实现随机生成一组不重复数并按行写入文件
2018/04/09 Python
python使用opencv对图像mask处理的方法
2019/07/05 Python
python 图像处理画一个正弦函数代码实例
2019/09/10 Python
css3实现二维码扫描特效的示例
2020/10/29 HTML / CSS
海蓝之谜英国官网:La Mer英国
2020/01/15 全球购物
学习实践科学发展观心得体会
2014/09/10 职场文书
班子群众路线教育实践个人对照检查材料思想汇报
2014/09/30 职场文书
2014年环保工作总结
2014/11/26 职场文书
网络营销计划书
2015/01/17 职场文书
小学六年级班主任工作经验交流材料
2015/11/02 职场文书
html5移动端禁止长按图片保存的实现
2021/04/20 HTML / CSS
Springboot集成阿里云OSS上传文件系统教程
2021/06/28 Java/Android
mysql中varchar类型的日期进行比较、排序等操作的实现
2021/11/17 MySQL
CentOS8.4安装Redis6.2.6的详细过程
2021/11/20 Redis
Android Rxjava3 使用场景详解
2022/04/07 Java/Android