日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python 將列表中的字符串轉(zhuǎn)為數(shù)字

系統(tǒng) 3037 0

python 將列表中的字符串轉(zhuǎn)為數(shù)字

轉(zhuǎn)自:https://www.jb51.net/article/86561.htm

本文實(shí)例講述了Python中列表元素轉(zhuǎn)為數(shù)字的方法。分享給大家供大家參考,具體如下:

有一個(gè)數(shù)字字符的列表:

1

numbers = ? [ '1' , '5' , '10' , '8' ]

想要把每個(gè)元素轉(zhuǎn)換為數(shù)字:

1

numbers = ? [ 1 , 5 , 10 , 8 ]

用一個(gè)循環(huán)來解決:

1

2

3

4

new_numbers = ? [];

for ? n in ? numbers:

?? new_numbers.append( int (n));

numbers = ? new_numbers;

有沒有更簡(jiǎn)單的語句可以做到呢?

1.

1

numbers = ? [ int (x) for ? x in ? numbers ]

2. Python2.x,可以使用map函數(shù)

1

numbers = ? map ( int , numbers)

如果是3.x,map返回的是map對(duì)象,當(dāng)然也可以轉(zhuǎn)換為List:

1

numbers = ? list ( map ( int , numbers))

3.還有一種比較復(fù)雜點(diǎn):

            
              for i, v in enumerate(numbers): numbers[i] = int(v)
            
          

?python字符數(shù)字之間的轉(zhuǎn)換:轉(zhuǎn)自https://www.cnblogs.com/wuxiangli/p/6046800.html

int(x [,base ]) ? ? ? ? 將x轉(zhuǎn)換為一個(gè)整數(shù) ? ?

long(x [,base ]) ? ? ? ?將x轉(zhuǎn)換為一個(gè)長整數(shù) ? ?

float(x ) ? ? ? ? ? ? ? 將x轉(zhuǎn)換到一個(gè)浮點(diǎn)數(shù) ? ?

complex(real [,imag ]) ?創(chuàng)建一個(gè)復(fù)數(shù) ? ?

str(x ) ? ? ? ? ? ? ? ? 將對(duì)象 x 轉(zhuǎn)換為字符串 ? ?

repr(x ) ? ? ? ? ? ? ? ?將對(duì)象 x 轉(zhuǎn)換為表達(dá)式字符串 ? ?

eval(str ) ? ? ? ? ? ? ?用來計(jì)算在字符串中的有效Python表達(dá)式,并返回一個(gè)對(duì)象 ? ?

tuple(s ) ? ? ? ? ? ? ? 將序列 s 轉(zhuǎn)換為一個(gè)元組 ? ?

list(s ) ? ? ? ? ? ? ? ?將序列 s 轉(zhuǎn)換為一個(gè)列表 ? ?

chr(x ) ? ? ? ? ? ? ? ? 將一個(gè)整數(shù)轉(zhuǎn)換為一個(gè)字符 ? ?

unichr(x ) ? ? ? ? ? ? ?將一個(gè)整數(shù)轉(zhuǎn)換為Unicode字符 ? ?

ord(x ) ? ? ? ? ? ? ? ? 將一個(gè)字符轉(zhuǎn)換為它的整數(shù)值 ? ?

hex(x ) ? ? ? ? ? ? ? ? 將一個(gè)整數(shù)轉(zhuǎn)換為一個(gè)十六進(jìn)制字符串 ? ?

oct(x ) ? ? ? ? ? ? ? ? 將一個(gè)整數(shù)轉(zhuǎn)換為一個(gè)八進(jìn)制字符串 ??

?

?

chr(65)='A'

ord('A')=65

?

int('2')=2;

str(2)='2'

?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 苗栗县| 金乡县| 和平区| 镶黄旗| 自治县| 高密市| 会昌县| 临沧市| 洛南县| 璧山县| 湘潭市| 伊春市| 集贤县| 迁安市| 田阳县| 泰安市| 河津市| 威远县| 应城市| 伊通| 新兴县| 平定县| 松潘县| 抚松县| 青海省| 太和县| 定结县| 荃湾区| 嘉荫县| 永和县| 讷河市| 阿尔山市| 休宁县| 肥乡县| 奎屯市| 灵武市| 朔州市| 高州市| 霞浦县| 手机| 德格县|