视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Python易忽视知识点小结
2020-11-27 14:41:13 责编:小采
文档

这里记录Python中容易被忽视的小问题

一、input(...)和raw_input(...)

#简单的差看帮助文档input(...)和raw_input(...)有如下区别 
>>> help(input) 
Help on built-in function input in module __builtin__: 
input(...) 
 input([prompt]) -> value 
 Equivalent to eval(raw_input(prompt)). 
>>> help(raw_input) 
Help on built-in function raw_input in module __builtin__: 
raw_input(...) 
 raw_input([prompt]) -> string 
 
 Read a string from standard input. The trailing newline is stripped. 
 If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. 
 On Unix, GNU readline is used if enabled. The prompt string, if given, 
 is printed without a trailing newline before reading. 
 
#可见 input会根据输入的内容eval结果来返回值,即输入纯数字,则得到的就是纯数字 
# raw_input返回的才是字符串 
#test: 
>>> a = input("输入数字") 
输入数字1 
>>> type(a) 
 
>>> b=raw_input("输入数字") 
输入数字1 
>>> type(b) 
 

ps:在python3.0以后的版本中,raw_input和input合体了,取消raw_input,并用input代替,所以现在的版本input接收的是字符串

二、python三目运算符

虽然Python没有C++的三目运算符(?:),但也有类似的替代方案,

那就是
1、 true_part if condition else false_part

>>> 1 if True else 0 
1 
>>> 1 if False else 0 
0 
>>> "True" if True else "False" 
'True' 
>>> "True" if True else "False" 
'Falser' 

2、 (condition and [true_part] or [false_part] )[0]

>>> (True and ["True"] or ["False"])[0] 
'True' 
>>> (False and ["True"] or ["False"])[0] 
'False' 
>>> 

三、获得指定字符串在整个字符串中出现第N次的索引

# -*- coding: cp936 -*- 
def findStr(string, subStr, findCnt): 
 listStr = a.split(subStr,findCnt) 
 if len(listStr) <= findCnt: 
 return -1 
 return len(string)-len(listStr[-1])-len(subStr) 
#test 
a = "12345(1)254354(1)3534(1)14" 
sub = "(1)" 
N = 2 #查找第2次出现的位置 
print findStr(a,sub,N) 
N = 10 #查找第10次出现的位置 
print findStr(a,sub,N) 
#结果 
#>>> 
#14 
#-1 

四、enumerate用法:

遍历序列的时候,可能同时需要用到序列的索引和对应的值,这时候可以采用enumerate方法进行遍历

enumerate的说明如下:

>>> help(enumerate) 
Help on class enumerate in module __builtin__: 
 
class enumerate(object) 
 | enumerate(iterable[, start]) -> iterator for index, value of iterable 
 | 
 | Return an enumerate object. iterable must be another object that supports 
 | iteration. The enumerate object yields pairs containing a count (from 
 | start, which defaults to zero) and a value yielded by the iterable argument. 
 | enumerate is useful for obtaining an indexed list: 
 | (0, seq[0]), (1, seq[1]), (2, seq[2]), ... 
 | 
 | Methods defined here: 
 | 
 | __getattribute__(...) 
 | x.__getattribute__('name') <==> x.name 
 | 
 | __iter__(...) 
 | x.__iter__() <==> iter(x) 
 | 
 | next(...) 
 | x.next() -> the next value, or raise StopIteration 
 | 
 | ----------------------------------------------------------------------
 | Data and other attributes defined here: 
 | 
 | __new__ =  
 | T.__new__(S, ...) -> a new object with type S, a subtype of T 

五、遍历序列的方法

>>> List = ['a','b','c'] 
>>> for index, value in enumerate(List): 
 print index, value 
0 a 
1 b 
2 c 
>>> 

六、使用python random模块的sample函数从列表中随机选择一组元素

import 
List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
slice = random.sample(List, 5)
#从List中随机获取5个元素,作为一个片断返回 
print slice 
print List #原有序列并没有改变。

七、用json打印包含中文的列表字典等

# -*- coding:utf-8 -*- 
import json 
#你的列表 
listA = [{'path': ['[AWS] xe7xbex8exe5xb0x91xe5xa5xb3xe6x88x98xe5xa3xab Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 1080p x2 AAC][6E5CFE86].mp4'], 'length': 131248608L}, {'path': ['[AWS] xe7xbex8exe5xb0x91xe5xa5xb3xe6x88x98xe5xa3xab Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 720p x2 AAC][639D304A].mp4'], 'length': 103166306L}, {'path': ['[AWS] xe7xbex8exe5xb0x91xe5xa5xb3xe6x88x98xe5xa3xab Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 480p x2 AAC][5A81BACA].mp4'], 'length': 75198408L}]
#打印列表
print json.dumps(listA, encoding='UTF-8', ensure_ascii=False) 

输出结果:

>>> 
[{"path": ["[AWS] 美少女战士 Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 1080p x2 AAC][6E5CFE86].mp4"], "length": 131248608}, {"path": ["[AWS] 美少女战士 Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 720p x2 AAC][639D304A].mp4"], "length": 103166306}, {"path": ["[AWS] 美少女战士 Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 480p x2 AAC][5A81BACA].mp4"], "length": 75198408}] 

希望本文所述对大家的Python程序设计有所帮助。

下载本文
显示全文
专题