python易忽视知识点小结

这里记录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) >>
#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] \xe7\xbe\x8e\xe5\xb0\x91\xe5\xa5\xb3\xe6\x88\x98\xe5\xa3\xab sailor moon crystal – moon pride mv[big5][bdrip 1080p x264 aac][6e5cfe86].mp4’], ‘length’: 131248608l}, {‘path’: [‘[aws] \xe7\xbe\x8e\xe5\xb0\x91\xe5\xa5\xb3\xe6\x88\x98\xe5\xa3\xab sailor moon crystal – moon pride mv[big5][bdrip 720p x264 aac][639d304a].mp4’], ‘length’: 103166306l}, {‘path’: [‘[aws] \xe7\xbe\x8e\xe5\xb0\x91\xe5\xa5\xb3\xe6\x88\x98\xe5\xa3\xab sailor moon crystal – moon pride mv[big5][bdrip 480p x264 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 x264 aac][6e5cfe86].mp4”], “length”: 131248608}, {“path”: [“[aws] 美少女战士 sailor moon crystal – moon pride mv[big5][bdrip 720p x264 aac][639d304a].mp4”], “length”: 103166306}, {“path”: [“[aws] 美少女战士 sailor moon crystal – moon pride mv[big5][bdrip 480p x264 aac][5a81baca].mp4”], “length”: 75198408}]

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

Posted in 未分类

发表评论