我在下面的Python 3.2中有一个代码,我想在Python 2.7中运行它。我确实进行了转换(已经missing_elements在两个版本中都放了代码),但是我不确定这是否是最有效的方法。基本上,如果函数的yield from上半部和下半部有两个类似下面的调用,会发生什么情况missing_element?是否将两个半部分(上半部分和下半部分)中的条目彼此追加到一个列表中,以便父递归函数与yield from调用一起使用并将这两个半部分一起使用?

def missing_elements(L, start, end): # Python 3.2

if end - start <= 1:

if L[end] - L[start] > 1:

yield from range(L[start] + 1, L[end])

return

index = start + (end - start) // 2

# is the lower half consecutive?

consecutive_low = L[index] == L[start] + (index - start)

if not consecutive_low:

yield from missing_elements(L, start, index)

# is the upper part consecutive?

consecutive_high = L[index] == L[end] - (end - index)

if not consecutive_high:

yield from missing_elements(L, index, end)

def main():

L = [10, 11, 13, 14, 15, 16, 17, 18, 20]

print(list(missing_elements(L, 0, len(L)-1)))

L = range(10, 21)

print(list(missing_elements(L, 0, len(L)-1)))

def missing_elements(L, start, end): # Python 2.7

return_list = []

if end - start <= 1:

if L[end] - L[start] > 1:

return range(L[start] + 1, L[end])

index = start + (end - start) // 2

# is the lower half consecutive?

consecutive_low = L[index] == L[start] + (index - start)

if not consecutive_low:

return_list.append(missing_elements(L, start, index))

# is the upper part consecutive?

consecutive_high = L[index] == L[end] - (end - index)

if not consecutive_high:

return_list.append(missing_elements(L, index, end))

return return_list

python编码转换语句_将“ yield from”语句转换为Python 2.7代码相关推荐

  1. python编码转换在线_在线UTF-8编码汉字互转 | utf8编码转换器

    一.UTF-8是什么? UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,又称万国码.由Ken Thompson于1992 ...

  2. python编码转换语句_好程序员Python教程之字符串编码知识小结

    好程序员Python教程之字符串编码知识小结,提及Python字符串,你会想到什么?是ASCII,还是Unicode?他们之间是如何转换的?字符串编码和字符串有什么区别?接下来好程序员Python教程 ...

  3. unicode 编码转换漏洞_好程序员Python教程之字符串编码知识小结

    好程序员Python教程之字符串编码知识小结,提及Python字符串,你会想到什么?是ASCII,还是Unicode?他们之间是如何转换的?字符串编码和字符串有什么区别?接下来好程序员Python教程 ...

  4. python编码转换在线_Python 编码转换与中文处理

    Python 编码转换与中文处理 python 中的 unicode是让人很困惑.比较难以理解的问题. utf-8是unicode的一种实现方式,unicode.gbk.gb2312是编码字符集. d ...

  5. Python 编码转换与中文处理

    Python 编码转换与中文处理 python 中的 unicode是让人很困惑.比较难以理解的问题. utf-8是unicode的一种实现方式,unicode.gbk.gb2312是编码字符集. d ...

  6. python编码规范总结、python编码转换

    python编码规范总结.python编码转换 所有的 Python 脚本文件都应在文件头标上 #-*- coding:utf8 -*- .设置编辑器,默认保存为 utf8 格式. 编码 Python ...

  7. python 编码转换 专题

    主要介绍了python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动识别 字符串编 ...

  8. python编码转换在线_Python字符编码转换Unicode和str

    ## str 我们平时写的用引号括起来的字符串都是str类型的. >>> x = '哈哈' >>> x '\xb9\xfe\xb9\xfe' ### 根据上面的打印 ...

  9. python 编码转换

    一.了解字符编码的知识储备 1. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编写的内容也都是存放与内存中的, ...

  10. python整数转换字符串_使用Python中的str()函数将整数值转换为字符串

    python整数转换字符串 Given an integer value and we have to convert the value to the string using str() func ...

最新文章

  1. AIC-赤池信息准则、BIC-贝叶斯信息准则
  2. 判断JavaScript对象为null或者属性为空
  3. LeetCode 424. Longest Repeating Character Replacement
  4. 纯做技术是自娱自乐 抛开技术做技术才是出路
  5. Hibernaate 详解
  6. Nginx+Tomcat搭建高性能负载均衡集群
  7. jdbc 连接 mysql 时的中文乱码问题
  8. 轻松调整,提升写作效率,让 Word 更好地为你所用
  9. extThree20XML extThree20JSON 引入到工程中的方式
  10. PPT转pdf保存动画效果
  11. 三次hermite插值多项式例题_hermite插值例题
  12. 长字符串的算术编码matlab,算术编码及MATLAB实现
  13. 程序员的算法趣题 python3 - (4)
  14. 【网上商城】--图片保存位置分析
  15. (转)春节抢票难,github标星1.5万的2款开源项目你一定没试过
  16. ntp服务器安装和配置文件,NTP服务的安装、配置和使用
  17. 如何去除图片上的文字(PS使用教程)
  18. 树莓派(4B)DHT11实现温湿度获取+mysql数据库存储
  19. 【环境配置】ceres solver安装
  20. 程序员每天每周每月每年应该做的事

热门文章

  1. 安装grid时找不到ASM共享磁盘
  2. [CF1137E]Train Car Selection[维护凸壳]
  3. python读、写、修改、追写excel文件(xlrd / xlwt / xlutils / openpyxl)
  4. C#几种截取字符串的方法(split 、Substring、Replace、remove)
  5. Centos干净卸载apache-php-mysql
  6. linux下关于程序性能和系统性能的工具、方法
  7. Item 22. 模板方法与曲线救国(Template Method)
  8. 漫谈 Clustering (番外篇): Vector Quantization
  9. 游标需要手动关闭吗MySQL,【MySQL必知必会】使用游标
  10. mysql表空间预估_MYSQL实战优化——数据页、表空间