Python易忽視知識點(diǎn)小結(jié)
這里記錄Python中容易被忽視的小問題
一、input(...)和raw_input(...)
#簡單的差看幫助文檔input(...)和raw_input(...)有如下區(qū)別 >>> 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會根據(jù)輸入的內(nèi)容eval結(jié)果來返回值,即輸入純數(shù)字,則得到的就是純數(shù)字 # raw_input返回的才是字符串 #test: >>> a = input("輸入數(shù)字") 輸入數(shù)字1 >>> type(a) <type 'int'> >>> b=raw_input("輸入數(shù)字") 輸入數(shù)字1 >>> type(b) <type 'str'>
ps:在python3.0以后的版本中,raw_input和input合體了,取消raw_input,并用input代替,所以現(xiàn)在的版本input接收的是字符串
二、python三目運(yùn)算符
雖然Python沒有C++的三目運(yùn)算符(?:),但也有類似的替代方案,
那就是
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' >>>
三、獲得指定字符串在整個字符串中出現(xiàn)第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次出現(xiàn)的位置 print findStr(a,sub,N) N = 10 #查找第10次出現(xiàn)的位置 print findStr(a,sub,N) #結(jié)果 #>>> #14 #-1
四、enumerate用法:
遍歷序列的時候,可能同時需要用到序列的索引和對應(yīng)的值,這時候可以采用enumerate方法進(jìn)行遍歷
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__ = <built-in method __new__ of type object> | 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函數(shù)從列表中隨機(jī)選擇一組元素
import List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] slice = random.sample(List, 5) #從List中隨機(jī)獲取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)
輸出結(jié)果:
>>> [{"path": ["[AWS] 美少女戰(zhàn)士 Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 1080p x264 AAC][6E5CFE86].mp4"], "length": 131248608}, {"path": ["[AWS] 美少女戰(zhàn)士 Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 720p x264 AAC][639D304A].mp4"], "length": 103166306}, {"path": ["[AWS] 美少女戰(zhàn)士 Sailor Moon Crystal - Moon Pride MV[BIG5][BDrip 480p x264 AAC][5A81BACA].mp4"], "length": 75198408}]
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python 正則表達(dá)式中re.group()使用小結(jié)
正則表達(dá)式是在處理字符串時非常有用的工具,而re.group()是在匹配到的文本中提取特定分組內(nèi)容的方法之一,這篇文章主要介紹了Python 正則表達(dá)式之re.group()用法,需要的朋友可以參考下2024-01-01python正則表達(dá)式去除兩個特殊字符間的內(nèi)容方法
今天小編就為大家分享一篇python正則表達(dá)式去除兩個特殊字符間的內(nèi)容方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python Pandas Dataframe.describe()使用及代碼實(shí)例
這篇文章主要介紹了Python Pandas Dataframe.describe()使用及代碼實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09python基礎(chǔ)教程之lambda表達(dá)式使用方法
lambda表達(dá)式相當(dāng)于函數(shù)體為單個return語句的普通函數(shù)的匿名函數(shù),本文主要介紹lambda表達(dá)式使用方法2014-02-02python生成隨機(jī)數(shù)、隨機(jī)字符、隨機(jī)字符串的方法示例
這篇文章主要介紹了python生成隨機(jī)數(shù)、隨機(jī)字符、隨機(jī)字符串的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04使用 Python 實(shí)現(xiàn)文件遞歸遍歷的三種方式
這篇文章主要介紹了使用 Python 實(shí)現(xiàn)文件遞歸遍歷的三種方式,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07