Python 实现flatten功能
from collections import Iterable def flatten(items):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
yield from flatten(x)
else:
yield x
"""
>>> items = [1, 2, [3, 4, [5, 6], 7], 8]
>>> flatten(items)
<generator object flatten at 0x73bd9c>
>>> list(flatten(items))
[1, 2, 3, 4, 5, 6, 7, 8]
>>> mixed_bag = [1, 'spam', 2, [3, 'eggs', 4], {'x': 1, 'y': 2}]
>>> list(flatten(mixed_bag))
[1, 'spam', 2, 3, 'eggs', 4, 'y', 'x']
"""
Python 实现flatten功能的更多相关文章
- Python中flatten用法
Python中flatten用法 原创 2014年04月16日 10:20:02 标签: Python / flatten 22667 一.用在数组 >>> a = [[1,3],[ ...
- Python实现截图功能你肯定不会吧?【面试必学】
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:CyborgLin python实现截图功能. windows环境下.需 ...
- 【python库模块】Python subprocess模块功能与常见用法实例详解
前言 这篇文章主要介绍了Python subprocess模块功能与常见用法,结合实例形式详细分析了subprocess模块功能.常用函数相关使用技巧. 参考 1. Python subprocess ...
- Python实现截图功能
Python实现截图功能 Windows环境下需要用到PIL库,使用pip安装PIL库: pip install Pillow 安装完成,截图方法代码: from PIL import ImageGr ...
- python实现curl功能
之前写过一篇文章关于python CURL模块的,在这里我们从urllib来实现同样的功能.具体代码如下: import urllib import urllib2 import json #发起请求 ...
- 利用PYTHON设计计算器功能
通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3 ...
- python专题-爬虫功能
在我们日常上网浏览网页的时候,经常会看到一些好看的图片,我们就希望把这些图片保存下载,或者用户用来做桌面壁纸,或者用来做设计的素材. 我们最常规的做法就是通过鼠标右键,选择另存为.但有些图片鼠标右键的 ...
- python爬虫高级功能
上一篇文章中我们介绍了爬虫的实现,及爬虫爬取数据的功能,这里会遇到几个问题,比方站点中robots.txt文件,里面有禁止爬取的URL.还有爬虫是否支持代理功能.及有些站点对爬虫的风控措施.设计的爬虫 ...
- 【Python】断言功能Assertion
转自 https://www.cnblogs.com/cicaday/p/python-assert.html Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在 ...
随机推荐
- O_DIRECT方式读取文件示例
#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h& ...
- 基于RFID恢复信号场
学习RFID的核心是什么?难处在哪?:https://www.zhihu.com/question/26799106
- 编写高质量代码改善C#程序的157个建议——建议97:优先考虑将基类型或接口作为参数传递
建议97:优先考虑将基类型或接口作为参数传递 除了公开及类型或接口外,方法的参数也应该考虑基类型或接口. 以Enumerable类型为例,它的成员方法中只要涉及需要操作集合对象的地方,都要使用IEnu ...
- [转载]Java线程的两种实现方式
转载:http://baijiahao.baidu.com/s?id=1602265641578157555&wfr=spider&for=pc 前言 线程是程序的一条执行线索,执行路 ...
- [leetcode] 4. Path Sum
终于到了二叉树.题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ...
- sql 计算地球2个坐标之间的距离
show variables like '%func%'; set global log_bin_trust_function_creators=1; use scm_wuliudelimiter $ ...
- Linux Guard Service - 守护进程分裂
分裂守护进程 由于fork()后第一行仍然在循环中,使用fork()返回值鉴别当前进程的性质 int i = 0; for (i = 0; i < 10; i++) { // sleep(1); ...
- 折腾了两天的跨站脚本提交问题,与IIS7有关
根据这里提供的方法,本地测试通过没有问题,但是部署到服务器上之后,只有GET请求可以跨站提交,POST请求继续报错,折腾了两天之后觉得,是不是IIS7的问题?果然,找到了这篇文章,照做之后解决.
- Replication--使用MSlogreader_history查看日志读起的延迟和事务命令
--======================================================================== MSlogreader_history 表存放本地 ...
- elasticsearch(0.90.10)安装配置+超多插件!!
一)安装elasticsearch 1)下载elasticsearch-0.90.10,解压,运行\bin\elasticsearch.bat (windwos) 2)进入http://localho ...