久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

python - 怎么寫才合適才優(yōu)雅

瀏覽:146日期:2022-09-07 14:57:47

問題描述

先上代碼

try:res+='會(huì)話數(shù)<span style=’color: blue;’> '+str(info[1]).strip(’n’)+'</span><br>' except Exception,e:print e try:res+='失效數(shù)<span style=’color: blue;’> '+str(info[2]).strip(’n’)+'</span><br>' except Exception,e:print e try:res+='連接數(shù)<span style=’color: blue;’> '+str(info[3]).strip(’n’).strip(’t’)+'</span><br>' except Exception,e:print e

上面的info[1]、info2[2]、info3[3],可能并不存在,所以我用try包起來,以免程序中途停止。而且各個(gè)的處理方式不一樣。這段代碼要怎么寫才合適才優(yōu)雅?為什么用優(yōu)雅語言寫出來的還是一坨......

問題解答

回答1:

_list = (’會(huì)話數(shù)’, ’失效數(shù)’, ’連接數(shù)’)for index, c in enumerate(_list): try:res+='{}<span style=’color: blue;’> '.format(c) + str(info[index + 1]).strip(’n’)'</span><br>' except Exception,e:print e回答2:

初始化一下info 例如info=[0,0,0] 我感覺這個(gè)干挺優(yōu)雅的!

回答3:

JS實(shí)現(xiàn),其它語言類似吧。

res = ’’;info.forEach(function(inf, i) { i === 1 && (res += ’會(huì)話數(shù)’ + inf); i === 2 && (res += ’失效數(shù)’ + inf); i === 3 && (res += ’連接數(shù)’ + inf);});回答4:

比起拼接字符串使用format函數(shù)是一個(gè)更好的選擇。

res += '{type} {count}'.format(type = ['會(huì)話數(shù)', '失效數(shù)', '連接數(shù)'][i],count = info[i])

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 双牌县| 江津市| 常德市| 香河县| 上犹县| 延长县| 平凉市| 博罗县| 确山县| 固始县| 曲阳县| 理塘县| 徐汇区| 香港| 同仁县| 临泉县| 甘泉县| 积石山| 乌拉特前旗| 渭南市| 黔西| 永嘉县| 望江县| 广丰县| 贵州省| 武鸣县| 奉新县| 开化县| 东丰县| 右玉县| 景宁| 宜兰县| 尼木县| 元阳县| 延津县| 聊城市| 昆山市| 吐鲁番市| 潮安县| 芜湖县| 海晏县|