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 不关闭控制台的实现方法
Oct 23 Python
用Python计算三角函数之atan()方法的使用
May 15 Python
如何用python整理附件
May 13 Python
用Python将一个列表分割成小列表的实例讲解
Jul 02 Python
Django中日期处理注意事项与自定义时间格式转换详解
Aug 06 Python
Python3实现对列表按元组指定列进行排序的方法分析
Dec 22 Python
基于Python打造账号共享浏览器功能
May 30 Python
Pycharm使用之设置代码字体大小和颜色主题的教程
Jul 12 Python
Django rstful登陆认证并检查session是否过期代码实例
Aug 13 Python
获取Pytorch中间某一层权重或者特征的例子
Aug 17 Python
Python 实现一个手机号码获取妹子名字的功能
Sep 25 Python
教你使用pyinstaller打包Python教程
May 27 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执行速率优化技巧小结
2008/03/15 PHP
sql注入与转义的php函数代码
2013/06/17 PHP
PHP递归删除目录几个代码实例
2014/04/21 PHP
php检测图片主要颜色的方法
2015/07/01 PHP
php获取linux命令结果的实例
2017/03/13 PHP
javascript小数计算出现近似值的解决办法
2010/02/06 Javascript
jquery的键盘事件修改代码
2011/02/24 Javascript
web开发人员学习jQuery的6大理由及jQuery的优势介绍
2013/01/03 Javascript
关闭ie窗口清除Session的解决方法
2014/01/10 Javascript
Nodejs+express+html5 实现拖拽上传
2014/08/08 NodeJs
JS中如何判断传过来的JSON数据中是否存在某字段
2014/08/18 Javascript
JS实现超炫网页烟花动画效果的方法
2015/03/02 Javascript
js实现简洁大方的二级下拉菜单效果代码
2015/09/01 Javascript
jQuery zclip插件实现跨浏览器复制功能
2015/11/02 Javascript
基于JavaScript实现数码时钟效果
2020/03/30 Javascript
vue的基本用法与常见指令
2017/08/15 Javascript
nodejs连接mysql数据库及基本知识点详解
2018/03/20 NodeJs
JS实现table表格内针对某列内容进行即时搜索筛选功能
2018/05/11 Javascript
Vue中点击active并第一个默认选中功能的实现
2020/02/24 Javascript
Node Mongoose用法详解【Mongoose使用、Schema、对象、model文档等】
2020/05/13 Javascript
Python3 正在毁灭 Python的原因分析
2014/11/28 Python
python实现字符串和日期相互转换的方法
2015/05/13 Python
Python中encode()方法的使用简介
2015/05/18 Python
python 线程的暂停, 恢复, 退出详解及实例
2016/12/06 Python
Python数据报表之Excel操作模块用法分析
2019/03/11 Python
Django实现发送邮件功能
2019/07/18 Python
python标记语句块使用方法总结
2019/08/05 Python
Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解
2020/02/18 Python
keras读取h5文件load_weights、load代码操作
2020/06/12 Python
Python实现Canny及Hough算法代码实例解析
2020/08/06 Python
保加利亚手表、香水、化妆品和珠宝购物网站:Brasty.bg
2020/04/22 全球购物
自荐书模板
2013/12/15 职场文书
买卖车协议书
2014/04/21 职场文书
本科生求职信
2014/06/17 职场文书
不同意离婚代理词
2015/05/23 职场文书
Python语言中的数据类型-序列
2022/02/24 Python