#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 学习网站:www.liaoxuefeng.com
#****************************************************
# Python3 高级特性-迭代器 *
#****************************************************
print ("--------------------分割线------------------")
#============
# 迭代器
#============
'''
可迭代对象(Iterable):可以直接作用于for循环的对象
一类是集合数据类型,如list, tuple, dict, set, str等
一类是generator,包括生成器和带yield的generator function
可以使用isinstance()判断一个对象是否是Iterable对象
'''
from collections import Iterable
print( isinstance([], Iterable) ) # True
print( isinstance({}, Iterable) ) # True
print( isinstance((), Iterable) ) # True
print( isinstance((x for x in range(10)), Iterable) ) # True
print( isinstance(100, Iterable) ) # False
print ("--------------------分割线------------------")
'''
迭代器(Iterator):可以被next()函数调用并不断返回下一个值的对象
可以使用isinstance()判断一个对象是否是Iterator对象
'''
from collections import Iterator
print( isinstance([], Iterator) ) # False
print( isinstance((), Iterator) ) # False
print( isinstance({}, Iterator) ) # False
print( isinstance('str', Iterator) ) # False
print( isinstance(100, Iterator) ) # False
# 列表生成式
print( isinstance([x for x in range(5)], Iterator) ) # False
# 生成器
print( isinstance((x for x in range(5)), Iterator) ) # True
print ("--------------------分割线------------------")
'''
生成器都是Iterator对象,但list,dict,str虽然是Iterable,却不是Iterator
可使用iter()函数把list,dict,str等Iterable变成Iterator
'''
print( isinstance( iter([]), Iterator ) ) # True
print( isinstance( iter(()), Iterator ) ) # True
print( isinstance( iter({}), Iterator ) ) # True
print( isinstance( iter('str'), Iterator ) )# True
'''
为什么list,dict,str等数据类型不是Iterator???
因为Python的Iterator对象表示的是一个数据流,Iterator对象可以被next()
函数调用并不断返回下一个数据,知道没有数据时抛出StopIteration错误。
可以把这个数据流看做是一个有序序列,但我们却不能提前知道序列的长度
只能不断通过next()函数实现按需计算下一个数据,所以Iterator的计算是
惰性的,只有在需要返回下一个数据时它才会计算。
Iterator甚至可以表示一个无限大的数据流,例如全体自然数。而使用list是
永远不可能存储全体自然数的。
'''
相关推荐
C51单片机驱动ILI9341彩屏实战:从Proteus仿真到绘制自定义图案(附完整代码)
本文详细介绍了使用C51单片机驱动ILI9341彩屏的完整实战流程。内容涵盖硬件连接、8080并行接口时序模拟、寄存器初始化、屏幕方向设置以及基本图形绘制原理,并重点讲解了如何在Proteus仿真环境中搭建电路、调试代码,最终实现自定义图案的显示。文章提供了完整的代码示例,为嵌入式开发学习者和复古硬件爱好者提供了一套可行的解决方案。
Python学习笔记【高级篇】
由于Python相关内容太多了,所以分成三个版块发布,分别为:基础篇、进阶篇、高级篇,大家也别纠结某些模块分到了进阶还是高级里,我也是参考书中的分类,综合下来区分的,这个没必要纠结。Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。**.Python-多任务(线程、进程、协程)编程模块**
【RDMA】13. RDMA之用户态与内核态交互
原文:https://zhuanlan.zhihu.com/p/346708569 在“RDMA之Verbs”一文中我们说道,Verbs API分为用户态和内核态,分别以ibv_和ib_作为前缀。RDMA技术的最大的优势就在于用户态可以绕过内核,直接控制硬件收发数据,减少了系统调用和内存拷贝次数,所以大部分的RDMA应用都是用户态应用,即使用以ibv_为前缀的用户态Verbs API。 但是并非所有的用户态Verbs API都可以完全绕开内核,本文中我来讲解一下哪些API依赖于内核RDMA子系统(包括驱
python 3-1 如何实现可迭代对象iterable和迭代器对象iterator,__iter__,__getitem__
3-1 如何实现可迭代对象和迭代器对象先看看如何遍历list的la = [1,2,3] for x in la: ==>其实是先取得iterator,然后调用iterator.next() print x如下是for循环的迭代的过程>>> la = [1,2,3] >>> iterator1 = iter(la) >>> iterator1.next() 1 >>> iterator1.ne
使用gsplat进行3D高斯泼溅的方案
gsplat 不是一个软件,而是一个深度优化CUDA的开源Python库。它的作者是 Nerfstudio 团队,这个团队最出名的项目是 Nerfstudio,是对 NeRFs 方法支持最好的一套框架。据其所述,出于对新一代技术的好奇,他们构建了 gsplat 这个库。它支持 Windows/Linux 两个系统,使用前必须准备好 Nvidia 显卡。跑这个项目真是特别费劲,官方的文档支持不够完善,以至于遇到问题,开发者们都只能在issue区里抱团取暖。
解读文献(五)------基于阻抗控制
文章目录背景主动柔顺控制阻抗控制基于力的阻抗控制基于位置的阻抗控制力位混合控制 背景 主动柔顺控制 阻抗控制 基于力的阻抗控制 基于位置的阻抗控制 力位混合控制 ...
python的迭代器iterator
可以被next函数调用不断返回下一个值的对象称为迭代器(iterator),可以直接用于for循环的对象称为可迭代对象(iterable),所有的可迭代对象均可以通过iter函数转变为迭代器。事实上,for循环内部先调用iter()把iterable变成iterator然后再进行循环迭代 iterator可以用来访问集合元素,迭代器对象从集合的首元素开始访问,直到所有元素被
quartus 13.1常见报错解决方法
在使用quartus中会遇到很多由于安装或设置不规范造成的错误。我就目前我遇到的一些错误以及我的解决方法分享给大家,这个文章将会持续更新,如果读者有遇到软件中错误无法解决的问题,也欢迎在评论区留言。 一、编译出现lience错误 在对设计的电路编译时,系统在程序运行3秒内即结束并报错,大概意思为现有的证书不支持编译。这个常见问题源自破解不成功,这里读者很有可能犯的错误是在使用破解器的时候没有区分x86(即32-bit)和x64软件破解器对应的文件夹。 如图所示有两种破解器,分别对应32位和64位的软件,大部
Python学习笔记:高级特性
前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖子,廖雪峰的课程连接在这里:廖雪峰 Python的相关介绍,以及它的历史故事和运行机制,可以参见这篇:python介绍 Python的安装可以参见这篇:Python安装 Python的运行模式...
黑暗中的爆破(船讯网Ais爬虫暨爬虫实战js逆向学习经验分享)
船讯网 爬虫 js逆向 python
基于TKVA与深度学习的量子点微型光谱仪光谱重建改进算法研究
本文提出了一种融合深度学习的改进TKVA算法(TKVA-DL),用于提升量子点微型光谱仪的光谱重建精度。该方法创新性地结合深度先验网络与注意力机制,通过自适应学习光谱隐式先验替代传统固定正则化项,并动态加权探测器响应通道。实验表明,所提方法在仿真和实测数据上均显著优于传统TKVA及主流深度学习算法,RMSE降低35%,PSNR提升4.2 dB,同时保持物理可解释性。完整MATLAB实现验证了方法的有效性与可复现性,为微型光谱仪的高精度重建提供了新思路。 关键词:量子点微型光谱仪;光谱重建;Tikhonov正
Python学习笔记——数据分析之Matplotlib绘图
目录 figure subplot 直方图:hist 散点图:scatter 柱状图:bar 矩阵绘图:plt.imshow() plt.subplots() 颜色、标记、线型 刻度、标签、图例 matplotlib基本要点 设置图片大小 调整X或者Y轴上的刻度 设置中文显示 给图像添加描述信息 自定义绘制图形的风格 为每条线添加图例 Matplotlib 是...
PCL点云算法与项目实战案例汇总(长期更新)
本专栏将深入探讨PCL(点云库)的基础算法,旨在帮助读者掌握点云处理的核心技术。内容涵盖点云的采样、过滤、配准、分割等基础操作,并结合实际案例,展示这些算法在工程中的应用。无论你是初学者还是有一定经验的开发者,都能通过本专栏加深对PCL的理解,提升在三维数据处理领域的技能。
python学习笔记の高级特性:切片、迭代、列表生成式、生成器、迭代器
切片在很多编程语言中,针对字符串提供了很多各种截取函数(例如,substring),其实目的就是对字符串切片。Python没有针对字符串的截取函数,只需要切片一个操作就可以完成,非常简单。我们先创建一个0-99的数列:>>> L = list(range(100))>>> L[0, 1, 2, 3, ..., 99]可以通过切片轻松取出某一段数列。比如前10个数...
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
假人有限元模型
假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用
Python学习笔记整理(python 3)
一、tuple(元组) tuple和list非常类似,但是tuple一旦初始化就不能修改,如: classmates = ('Michael', 'Bob', 'Tracy') classmates这个tuple不能变了,它也没有append(),insert()这样的方法。其他获取元素的方法和list是一样的,你可以正常地使用classmates[0],classmates[-1],但...
文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况
主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。
Python3 学习笔记6_字符串_20180224
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.runoob.com #**************************************************** # Python3 字符串 * #*************************...
IMSL CNL 7.0.0 (x86-64)Crack
IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件
Python3 学习笔记20_函数式编程-高阶函数-map/reduce_20180312
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.liaoxuefeng.com #**************************************************** # Python3 函数式编程-高阶函数-map/reduce * #************************...
Python3 学习笔记5_数字(Number)_20180223
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.runoob.com #**************************************************** # Python3 数字(Number) * #***********************...
Python3 学习笔记8_元组_20180227
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.runoob.com #**************************************************** # Python3 元组 * #*************************...
Python3 学习笔记14_递归函数_20180305
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.liaoxuefeng.com #**************************************************** # Python3 函数 * #************************...
Python3 学习笔记4_运算符_20180223
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.runoob.com #**************************************************** # Python3 运算符 * #*************************...
Python3 学习笔记21_函数式编程-高阶函数-filter/sorted_20180312
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.liaoxuefeng.com #**************************************************** # Python3 函数式编程-高阶函数-filter/sorted * #*************************...
Python3 学习笔记13_函数的参数_20180305
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.liaoxuefeng.com #**************************************************** # Python3 函数 * #************************...
Python3 学习笔记23_函数式编程-匿名函数_20180314
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.liaoxuefeng.com #**************************************************** # Python3 函数式编程-匿名函数 * #************************...
Python3 学习笔记11_条件语句_20180301
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.runoob.com #**************************************************** # Python3 循环语句 * #*************************...
Python3 学习笔记9_字典_20180228
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 学习网站:www.runoob.com #**************************************************** # Python3 字典 * #*************************...
511



被折叠的 条评论
为什么被折叠?



