2016.4.13Python中的filter

【廖雪峰Python教程练习答案】filter 回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()筛选出回数:import math def is_palindrome(n): numstr = str(n) size = math.floor(len(numstr)/2) for i in range(size): if numstr[i]!=numstr[len... 阅读详情

与map()类似,Python中的filter()函数也接收一个函数和一个序列,但它把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。所以也可以被认为是用于过滤序列的一种函数。

例如,在一个list中,删掉偶数,只保留奇数,可以这么写:

def is_odd(n):
    return n % 2 == 1

list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]))
# 结果: [1, 5, 9, 15]

把一个序列中的空字符串删掉,可以这么写:

def not_empty(s):
    return s and s.strip()

list(filter(not_empty, ['A', '', 'B', None, 'C', '  ']))
# 结果: ['A', 'B', 'C']

可见用filter()这个高阶函数,关键在于正确实现一个“筛选”函数。

注意到filter()函数返回的是一个Iterator,也就是一个惰性序列,所以要强迫filter()完成计算结果,需要用list()函数获得所有结果并返回list。

用filter求素数

计算素数的一个方法是埃氏筛法,它的算法理解起来非常简单:

首先,列出从2开始的所有自然数,构造一个序列:

2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...

取序列的第一个数2,它一定是素数,然后用2把序列的2的倍数筛掉:

3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...

取新序列的第一个数3,它一定是素数,然后用3把序列的3的倍数筛掉:

5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...

取新序列的第一个数5,然后用5把序列的5的倍数筛掉:

7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...

不断筛下去,就可以得到所有的素数。

用Python来实现这个算法,可以先构造一个从3开始的奇数序列:

def _odd_iter():
    n = 1
    while True:
        n = n + 2
        yield n

注意这是一个生成器,并且是一个无限序列。

然后定义一个筛选函数:

def _not_divisible(n):
    return lambda x: x % n > 0

最后,定义一个生成器,不断返回下一个素数:

def primes():
    yield 2
    it = _odd_iter() # 初始序列
    while True:
        n = next(it) # 返回序列的第一个数
        yield n
        it = filter(_not_divisible(n), it) # 构造新序列

这个生成器先返回第一个素数2,然后,利用filter()不断产生筛选后的新的序列。

由于primes()也是一个无限序列,所以调用时需要设置一个退出循环的条件:

# 打印1000以内的素数:
for n in primes():
    if n < 1000:
        print(n)
    else:
        break

注意到Iterator是惰性计算的序列,所以我们可以用Python表示“全体自然数”,“全体素数”这样的序列,而代码非常简洁。

练习

回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()滤掉非回数:

我写的是这样:




string后面跟的[]里面是index,表示的是对string中字符的指向,比如[0:n]表示string的中第一个到第n-1个字符,是用0来表示第一个字符的位置。
如果不写开头或结尾,就默认从头开始,或直到最后。例如[:3]其实就是[0:3],就是[0],[1],[2]这三个;[3:]则代表第四个到最后一个。
然后如果index中时负数,则表示从后往前算,倒着数。上面这个写法就是将 s 字符串中倒数第一个字符删除, -2 就是将s 字符串中倒数后两个字符删除。此处是将删除后的值再赋给s 。

我写这个的时候参考了:https://www.zhihu.com/question/22686450



另附:
部分函数:

abs(number),返回数字的绝对值

cmath.sqrt(number),返回平方根,也可以应用于负数

float(object),把字符串和数字转换为浮点数

help(),提供交互式帮助

input(prompt),获取用户输入

int(object),把字符串和数字转换为整数

math.ceil(number),返回数的上入整数,返回值的类型为浮点数

math.floor(number),返回数的下舍整数,返回值的类型为浮点数

math.sqrt(number),返回平方根不适用于负数

pow(x,y[.z]),返回X的y次幂(有z则对z取模)

repr(object),返回值的字符串标示形式

round(number[.ndigits]),根据给定的精度对数字进行四舍五入

str(object),把值转换为字符串




Mel-Filter banks/MFCC特征提取(基于python) 最近开始上手语音相关的课题,第一步当然是了解并提取语音相关的特征及其提取,纵览paper,使用最多的莫过于Filter banks和MFCC了,因此就开始上手自己编写代码提取。(当然不是白手起家,参考在http://haythamfayek.com/2016/04/21/speech-processing-for-machine-learning.html) 1.概述 Filter b 阅读详情

相关推荐

Mel-Filter banksMFCC特征提取(基于python)

最近开始上手语音相关的课题,第一步当然是了解并提取语音相关的特征及其提取,纵览paper,使用最多的莫过于Filter banks和MFCC了,因此就开始上手自己编写代码提取。(当然不是白手起家,参考在http://haythamfayek.com/2016/04/21/speech-processing-for-machine-learning.html) 1.概述 Filter banks和M...

weixin_43106248的博客 728

[廖雪峰python教程map/reduce练习题]利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456

https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317852443934a86aa5bb5ea47fbbd5f35282b331335000#0正确实践 def fn(x,y): return x*10+y #n = s.index['.']...

yantuguiguziPGJ的博客 1506

PCIe信号眼图总是不开?手把手教你调CTLE和DFE参数(附实测波形对比)

本文详细解析了PCIe信号眼图调试中CTLE和DFE参数的优化方法,帮助硬件工程师解决眼图无法张开的问题。通过实际案例和步骤指导,介绍了如何调整CTLE直流增益和DFE抽头系数,提升接收端均衡器性能,确保信号完整性。

weixin_26805597的博客 214

Python笔记——贰

1,高阶函数:一个函数接收另一个函数作为参数 def add(x, y, f): return f(x) + f(y) add(-5, 6, abs) 11 2,map() 函数接收两个参数,一个是函数,一个是Iterable,传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。 def f(x): return x * x #把f(x)作用在list的每一个元素并把结果生成一个新的list r = map(f, [1, 2, 3, 4, 5, 6, 7, 8,

weixin_44042027的博客 279

解决鸿蒙/荣耀设备GMS应用调试中的“Play保护机制认证”报错

本文详细解析了在鸿蒙或荣耀设备上调试GMS应用时,遇到“Play保护机制认证”报错的根本原因与解决方案。核心在于获取设备的GMS Device ID,并通过Google官方渠道完成临时注册,从而绕过认证限制。文章提供了无需Root和ADB查询两种具体方法,并分享了授权后的故障排查与实战经验,帮助开发者高效解决问题。

weixin_29288313的博客 299

StarWind V2V Converter v9 虚拟磁盘转换工具使用教程 (转)

StarWind V2V Converter v9.0.1.268 是一款免费的虚拟磁盘转换工具,支持不同管理程序间的虚拟机镜像格式转换,包括VMDK、VHD/VHDX、IMG和QCOW2等格式。该工具具有窗口修复模式、零拷贝转换和管理程序切换等特色功能,能够轻松实现物理机到虚拟机的转换,并兼容主流管理程序如Hyper-V、ESXi等。使用教程详细介绍了从本地文件选择到格式转换的完整操作流程,转换过程简单高效且不影响原虚拟磁盘数据。

hsh的专栏 1481

编写一个函数,用来删除字符串中的空格

程序代码#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; void fun(char *str) { char *p=str; int i=0; while(*p) { if(*p!=' ') str[i++]=*p; p++; } str[i]='\0'; } int main() { char str[81]; ch...

xiaochenxiaoren的博客 2万+

SAP中错记虚拟利润中心的调整,冲销处理实例

我非CO专业顾问。工作中遇到的问题,也会找资料,分析,测试。有时这个过程会拿到结果,解决问题。遂将过程记录下来备查。读者请谨慎参考!EC-PCA: 利润中心记账流程_9ke0-优快云博客。

专注SAP应用技术文章写作,致力于拉近用户与系统间的距离; 1795

利用filter()滤掉非回数

代码可以优化的地方太多了,学习真是永无止境。每个人都有自己的见解,但是也会有一个最优解。 回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()滤掉非回数: def is_palindrome(n): a = repr(n) if (a[0] == a[-1]) and (n % int(a[-1]) == 0): i

cnniu的专栏 883

UIPath 获取网页元素做遍历点击的实现

UIPath 获取网页元素做遍历点击的实现

geekqian的博客 1万+

ESP32-CAM 赋能 AI 视觉巡检小车:自主避障与目标追踪实现全解析

本文基于 ESP32-CAM 实现了低成本 AI 视觉巡检小车的核心功能,硬件成本<100 元,兼顾自主避障与目标追踪。算法升级:移植 YOLOv5nano 量化模型(INT8),提升目标检测精度(如识别巡检场景中的 “异常物体”“人员”);无线拓展:通过 MQTT 协议将巡检图像、距离数据上传到阿里云 / 腾讯云,实现远程监控;多传感器融合:增加温湿度(DHT11)、红外测温(MLX90614)模块,扩展巡检维度;路径规划:结合 SLAM 算法,实现自主建图与路径巡检。

weixin_42300449的博客 368

python 08 map、filter、reduce

map 返回一个可迭代对象,这个可迭代对象通过function计算每一个元素产生 # 对列表中的数字取平方,方法单一 num1 = [1, 2, 3, 4, 5] def map_test(array): ret = [] for i in num1: ret.append(i ** 2) return ret print(map_test(num1)) # [1, 4, 9, 16, 25] # 对列表中的元素进行未知操作, 自己实现内部逻辑 num...

qq_30346413的博客 349

【C语言笔记初级篇】第一章:C语言快速入门及注意事项

(1)第一个C语言程序 A:利用vs2010写C语言程序 1:按以下步骤新建工程 2:添加源文件,“.c”文件 B:第一个C语言代码 1:以下是基本的C语言程序 2:由于没有设置连接器,所以打印结果没有显示,按照如下步骤链接控制台 (2)数据类型 A:基本数据类型及占用字节 B:一些概念的梳理 概念一:关于计算机的单位 位(bit):计算机通过电路的开关和闭合处理信息,电路通...

快乐江湖的博客 3737

[从头学数学] 第254节 Python实现数据结构:bloom Filter

剧情提要: 阿伟看到了一本比较有趣的书,是关于《计算几何》的,2008年由北清派出版。很好奇 它里面讲了些什么,就来看看啦。 正剧开始: 星历2016年08月01日 10:17:09, 银河系厄尔斯星球中华帝国江南行省。 [工程师阿伟]正在和[机器小伟]一起研究[计算几何]]。 这次主要是关于bloom Filter这种成员审查结构

微笑的彩虹的博客 507

编程助手:CLAUDE.md 与 AGENTS.md 完全指南

AI编程助手配置文件指南 为解决AI编程助手在陌生项目中面临的运行、规范、架构等问题,本文提出两种配置文件: CLAUDE.md(根目录): 面向通用AI(如ClaudeCode) 包含项目命令、代码风格、架构说明等结构化信息 示例:启动命令:npm run dev,代码规范:TypeScript禁用any AGENTS.md(任意目录): 面向智能编码工具(如Cursor) 支持模块级配置(如微前端子应用) 示例:前端测试命令:npm test:web,API请求规范 与传统README.md的区别: 人

分享AI、区块链技术及应用,未来已来 1250

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

filter,map,reduce,lambda(python3)

1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个list、string、tuple(根据sequence类型决定)返回。 1 #!/usr/bin/env python 2 # encoding: utf-8 3 """ 4 @a...

weixin_30295091的博客 78

深度学习工具包 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');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

python filter过滤器的使用_如何在Python中正确的使用BloomFilter

如何在Python中正确的使用BloomFilterFeb 13, 2016 · 1 minute read · Commentspython0x1 什么是布隆过滤器(BloomFilter)1970年,由布隆提出来的一个用于判断元素是否在集合中的高效的算法,集合中的元素可以增加,但是要删除一个元素比较困难,同时还有少量的误报率。在数据量比较小的时候,我们可以使用Hash来判断元素是否命中,但是当...

weixin_39950812的博客 417

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

python学习手册中的一些易忘的点(第4部分)

1.python函数高聚合低耦合1)对于输入使用参数并且对于输出使用return2)只有在真正必要的情况下使用全局变量3)不要改变可变类型的参数,除非调用者希望这样做4)每个函数都应该有一个单一的、统一的目标5)每一个函数应该相对较小6)避免直接改变在另一个文件中的变量2.递归处理任意结构>>> a=[3,[2,[2,3,4],2],1,[1,5,[1,3,3]]]>&gt...

weixin_30252155的博客 98

python2升级python3

Python2到python3的升级,其中用pipenv生成虚拟环境 第一步:使用Python2到python3的升级工具: C:\Python 37\Tools\scripts>python 2to3.py -w G:\项目路径\ 第二步:django升级 python3.7环境必须对应django2.1版本(我自己装的python3.7.3的环境), 这是python环境对应的dja...

weixin_43794608的博客 792

python相关知识点整理四(自己复习用)

filter, map, reduce几句话就能讲明白filter(function, iterable) 作用的是可迭代对象,list / tuple / dict / set / strlists = ['11班01','11班02','12班02','13班02'] #可迭代对象 f = filter(lambda x: x.startswith('11班'),lists) #遍历li...

gaoyishu91的博客 212

python函数设计与使用_[Python学习笔记][第五章Python函数设计与使用]

2016/1/29学习内容第四章 Python函数设计与使用之前的几页忘记保存了 很伤心变量作用域-一个变量已在函数外定义,如果在函数内需要修改这个变量的值,并将这个赋值结果反映到函数之外,可以在函数内用global声明这个变量为全局变量,明确声明要使用已定义的同名全局变量-在函数内部直接使用global关键词将一个变量声明为全局变量,即使在函数外没有定义该全局变量,在调用这个函数之后,将自动增加...

weixin_29820757的博客 235

python设计函数_[Python学习笔记][第五章Python函数设计与使用]

2016/1/29学习内容第四章 Python函数设计与使用之前的几页忘记保存了 很伤心变量作用域-一个变量已在函数外定义,如果在函数内需要修改这个变量的值,并将这个赋值结果反映到函数之外,可以在函数内用global声明这个变量为全局变量,明确声明要使用已定义的同名全局变量-在函数内部直接使用global关键词将一个变量声明为全局变量,即使在函数外没有定义该全局变量,在调用这个函数之后,将自动增加...

weixin_39789979的博客 153

用python语言介绍自己_python入门第一篇:python语言简介

四、Python发展史1989年,Guido开始写Python语言的编译器。1991年,第一个Python编译器诞生。它是用C语言实现的,并能够调用C语言的库文件。从一出生,Python已经具有了:类,函数,异常处理,包含表和词典在内的核心数据类型,以及模块为基础的拓展系统。Granddaddy of Python web frameworks, Zope 1 was released in 19...

weixin_39927158的博客 147

python:星座与十二生肖_看序列、操作符与简单语句的使用

我们通过存取星座和十二生肖的操作,学习和掌握序列、操作符与简单语句的使用。以下分为三个部分:内容整理和两个实例(十二生肖、星座查询)。 内容整理 序列 字符串:“abcd” 链表:[0,“abcd”] 元组:(“abc”,“def”) ps:python中单双引号不区分 序列的基本操作 成员关系操作符: 对象[not]in序列 连接操作符: 序列+序列 重复操作符: 序...

Rocky_monkey的博客 8017

python高级技能

1、匿名函数:lambda¶ In [1]: f=lambdax,y,z:x+y+z f(2,3,4) Out[1]: 9 In [2]: L=[lambda x:x**2,   lambda x: x**3,   lambda x:x**4] for f in L:     print(f(2)) 4 8 16 In [3]: import sys showal

山谷来客 686

我的Python成长之路---第三天---Python基础(13)---2016年1月16日(雾霾)

五、Python的常用的内置函数   Python为我们准备了大量的内置函数,如下图所示   这里我们只讨论红框内的内置函数   abs(x)   返回一个数的绝对值(模),参数可以是真说或浮点数 >>> abs(-10) 10   all(iterable)   如果可迭代对象(列表、元祖等)中所有的元素都是True,则返回True,否则返回Fals...

weixin_34318326的博客 167
上一篇: 2016.4.12Python迭代器,函数式编程,map/reduce高阶函数
下一篇: 2016.4.14python排序函数
despicablefolk
博客等级 码龄11年 12粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值