python:零散记录
1、rstrip()删除末尾指定字符串
例如:A = '1,2,3,4,5,'
B = A.rstrip(',')
B = '1,2,3,4,5'
2、isdigit()方法
Python isdigit() 方法检测字符串是否只由数字组成,返回 True 或者False。
例如:str = '123456'
str.isdigit()为True
3、enumerate() 函数
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
例如:seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print i, element
输出:0 one
1 two
2 three
4、字符串 startswith、find、join
例如:name = 'Swaroop'
if name.startswith('Swa'): #startswith方法用于查找字符串是否以给定的字符串内容开头
print('the string start with "Swa"')
if 'a' in name:
print('it contains the string "a"')
if name.find('war') !=-1: #find方法用于定位字符串中给定字符串的位置,如果找不到对应的字符串,会返回-1
print('it contains the string "war"')
delimiter = '_*_'
mylist = ['Brazil','Russia','India','China']
print(delimiter.join(mylist)) #字符串delimiter将会作为每个项目之间的分隔,并以此返回一串更大的字符串
输出:the string start with "Swa"
it contains the string "a"
it contains the string "war"
Brazil_*_Russia_*_India_*_China
5、函数传递元组,返回多个变量
例如:def run():
return ('hello','world')
first, second = run()
print(first)
print(second)
输出:hello
world
6、在函数中接收元组与字典
分别使用 * 或 ** 作为元组或字典的前缀,来使它们作为一个参数为函数所接收
例如:def powersum(power, *args):
total = 0
for i in args:total += pow(i, power)
return total
print(powersum(2, 3, 4))
print(powersum(2, 10))
输出:25
100
python:零散记录的更多相关文章
- python 零散记录(七)(下) 新式类 旧式类 多继承 mro 类属性 对象属性
python新式类 旧式类: python2.2之前的类称为旧式类,之后的为新式类.在各自版本中默认声明的类就是各自的新式类或旧式类,但在2.2中声明新式类要手动标明: 这是旧式类为了声明为新式类的方 ...
- python 零散记录(七)(上) 面向对象 类 类的私有化
python面向对象的三大特性: 多态,封装,继承 多态: 在不知道对象到底是什么类型.又想对其做一些操作时,就会用到多态 如 'abc'.count('a') #对字符串使用count函数返回a的数 ...
- python 零散记录(五) import的几种方式 序列解包 条件和循环 强调getattr内建函数
用import关键字导入模块的几种方式: #python是自解释的,不必多说,代码本身就是人可读的 import xxx from xxx import xxx from xxx import xx1 ...
- python 零散记录(二) 序列的相关操作 相加 相乘 改变 复制
序列相加: [1,2] + [3,4] == [1,2,3,4] #字符串也是序列的一种 'hello' + 'world' == 'hello world' #但是序列相加只限于相同类型的序列间相加 ...
- python 零散记录(一) input与raw_input 数学相关函数 转换字符串的方法
input()与raw_input(): 两者都是接受命令行输入,但区别在于,raw_input()接受原始数据(raw data). #使用input()来提示输入名字 input("en ...
- python 零散记录(六) callable 函数参数 作用域 递归
callable()函数: 检查对象是否可调用,所谓可调用是指那些具有doc string的东西是可以调用的. 函数的参数变化,可变与不可变对象: 首先,数字 字符串 元组是不可变的,只能替换. 对以 ...
- python 零散记录(四) 强调字典中的键值唯一性 字典的一些常用方法
dict中键只有在值和类型完全相同的时候才视为一个键: mydict = {1:1,':1} #此时mydict[1] 与 mydict['1']是两个不同的键值 dict的一些常用方法: clear ...
- python 零散记录(三) 格式化字符串 字符串相关方法
使用 % 符号格式化字符串: """常用转换说明符:""" #%s: 按照str()方式转换 #%r: 按照repr()方式转换 #%d: ...
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- Python学习记录day5
title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
随机推荐
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
- first-child和last-child选择器 nth-child(n)第几个元素 nth-last-child(n)倒数第几个元素
:first-child 和 :last-child 分别表示父元素中第一个 或者 最后一个 子元素设置样式,如上图
- Python递归实现遍历目录
import os filePath = "/Users/busensei/wzy/filePath/" def read(filePath, n): it = os.listdi ...
- Codeforces 1107G(dp)
1.答案要取连续的区间疯狂暗示线段树. 2.外层枚举r,内层枚举l显然过于暴力. 3.考虑内层的优化.dp[i]:以第i位为结尾的答案(长度大于1的).dp[i] = max(第一种情况,第二种情况) ...
- port 22: Connection refused
issue: os>ssh 196.168.27.90ssh: connect to host 196.168.27.90 port 22: Connection refused solutio ...
- Unity Mesh 初体验
什么是Mesh Mesh是Unity中的一个组件,称为网格组件.通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而一个复杂的多边形,实际上是由多个三角面拼接而成.所以一个3D模型的表面是 ...
- 分布式数据存储 之 Redis(二) —— spring中的缓存抽象
分布式数据存储 之 Redis(二) -- spring中的缓存抽象 一.spring boot 中的 StringRedisTemplate 1.StringRedisTemplate Demo 第 ...
- android studio MQTT测试成功
package myself.mqtt.wenzheng.studio.mqtt; import android.app.Notification; import android.app.Notifi ...
- Hibernate核心接口和工作原理
Hibernate核心接口和工作原理 Hibernate有五大核心接口,分别是:Session .Transaction .Query .SessionFactory .Configuration . ...
- Android手机app耗电量测试工具 - Gsam Battery Monitor
这段时间需要测试一个Android手机app的耗电量,在网上找了一个工具,Gsam Battery Monitor,觉得挺好用,和大家分享一下. 安装app后打开,可以看到主界面是这样的 点击一下上图 ...