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 创建子进程模块subprocess详解
Apr 08 Python
python解决方案:WindowsError: [Error 2]
Aug 28 Python
Python基于回溯法子集树模板解决取物搭配问题实例
Sep 02 Python
基于python实现在excel中读取与生成随机数写入excel中
Jan 04 Python
python链接oracle数据库以及数据库的增删改查实例
Jan 30 Python
python将文本中的空格替换为换行的方法
Mar 19 Python
解决win64 Python下安装PIL出错问题(图解)
Sep 03 Python
python将txt文件读取为字典的示例
Dec 22 Python
Python实现账号密码输错三次即锁定功能简单示例
Mar 29 Python
使用Python做定时任务及时了解互联网动态
May 15 Python
python实现udp聊天窗口
Mar 31 Python
浅析Python的命名空间与作用域
Nov 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的特殊设置
2006/10/09 PHP
php中Smarty模板初体验
2011/08/08 PHP
div li的多行多列 无刷新分页示例代码
2013/10/16 PHP
PHP实现恶意DDOS攻击避免带宽占用问题方法
2015/05/27 PHP
phpcms配置列表页以及获得文章发布时间
2017/07/04 PHP
用Laravel Sms实现laravel短信验证码的发送的实现
2018/11/29 PHP
tp5(thinkPHP5框架)时间查询操作实例分析
2019/05/29 PHP
一段实时更新的时间代码
2006/07/07 Javascript
常用参考资料(手册)下载或者链接
2006/07/22 Javascript
javascript之大字符串的连接的StringBuffer 类
2007/05/08 Javascript
跨域表单提交状态的变相判断代码
2009/11/12 Javascript
jquery 表格分页等操作实现代码(pagedown,pageup)
2010/04/11 Javascript
jQuery 1.9.1源码分析系列(十四)之常用jQuery工具
2015/12/02 Javascript
JavaScript数据结构链表知识详解
2016/11/21 Javascript
jquery判断页面网址是否有效的两种方法
2016/12/11 Javascript
详解前端自动化工具gulp自动添加版本号
2016/12/20 Javascript
JS多文件上传的实例代码
2017/01/11 Javascript
js中如何完美的解析数据
2018/03/18 Javascript
原生js实现瀑布流效果
2020/03/09 Javascript
Javascript模块化机制实现原理详解
2020/04/02 Javascript
JavaScript实现点击切换验证码及校验
2021/01/10 Javascript
nodejs处理tcp连接的核心流程
2021/02/26 NodeJs
用smtplib和email封装python发送邮件模块类分享
2014/02/17 Python
Python 功能和特点(新手必学)
2015/12/30 Python
Selenium元素的常用操作方法分析
2018/08/10 Python
python中协程实现TCP连接的实例分析
2018/10/14 Python
Python自动发送邮件的方法实例总结
2018/12/08 Python
python3 http提交json参数并获取返回值的方法
2018/12/19 Python
python操作链表的示例代码
2020/09/27 Python
HTML5 File API改善网页上传功能
2009/08/19 HTML / CSS
淘宝店铺营销方案
2014/02/13 职场文书
幼儿教师演讲稿
2014/05/06 职场文书
机关职员工作检讨书
2014/10/23 职场文书
2014年扶贫帮困工作总结
2014/12/09 职场文书
2016年“世界气象日”广播稿
2015/12/17 职场文书
python使用openpyxl库读写Excel表格的方法(增删改查操作)
2021/05/02 Python