国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python易忽視知識點小結

2020-02-23 01:24:41
字體:
來源:轉載
供稿:網友

這里記錄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) <type 'int'> >>> b=raw_input("輸入數字") 輸入數字1 >>> type(b) <type 'str'> 

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__ = <built-in method __new__ of type object>  |   T.__new__(S, ...) -> a new object with type S, a subtype of T             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 谢通门县| 会东县| 溆浦县| 民权县| 清河县| 成都市| 临湘市| 临汾市| 简阳市| 集安市| 晋江市| 衡山县| 伊宁市| 盖州市| 崇信县| 乃东县| 思茅市| 遂溪县| 周宁县| 光山县| 运城市| 绥德县| 玉门市| 白银市| 高邮市| 平谷区| 永善县| 榆社县| 文成县| 伊宁县| 库车县| 瓦房店市| 塔城市| 玉屏| 根河市| 台东市| 南阳市| 拜城县| 怀柔区| 囊谦县| 津南区|