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检查xpath和csspath表达式是否合法
- JS策略模式
- Scrum项目8.0
- Codeforces Round #389 Div.2 A. Santa Claus and a Place in a Class
- Java Web整合开发实战:基于Struts 2+Hibernate+Spring 目录
- GTP (GPRS隧道协议(GPRSTunnellingProtocol))
- C语言 类型
- Oracle 临时表
- 《javascript高级程序设计》第六章 Object Creation VS Inheritance
- C#EasyHook例子C# Hook 指定进程C#注入指定进程 z
- Moses manual 中Basline System 2.3.4节用IRSTLM创建语言模型的命令有误
- ASP.NET网站实现中英文转换(本地化资源)
- ldap命令的使用
- 读headFirst设计模式 - 观察者模式
- 使用Myeclipse2015构建SpringMVC项目
- [BZOJ3011][Usaco2012 Dec]Running Away From the Barn
- 什么是mime类型
- scrapy 是指user_agent
- jq插件封装格式
- [Android Pro] Android中IntentService的原理及使用
热门文章
- bzoj 3745: [Coci2015]Norma【分治】
- spring boot :error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required
- C++伪函数
- 洛谷 P2398 GCD SUM || uva11417,uva11426,uva11424,洛谷P1390,洛谷P2257,洛谷P2568
- Retrofit实现PUT网络请求,并修改Content-Type
- 对js 面对对象编程的一些简单的理解
- LN : leetcode 516 Longest Palindromic Subsequence
- 一份最贴近真实面试的Java基础面试题
- 设置QtreeWidget水平滚动条
- powerDesigner 把name项添加到注释(comment),完美方案!