python 將列表中的字符串轉(zhuǎn)為數(shù)字
轉(zhuǎn)自:https://www.jb51.net/article/86561.htm
本文實(shí)例講述了Python中列表元素轉(zhuǎn)為數(shù)字的方法。分享給大家供大家參考,具體如下:
有一個(gè)數(shù)字字符的列表:
1 |
|
想要把每個(gè)元素轉(zhuǎn)換為數(shù)字:
1 |
|
用一個(gè)循環(huán)來解決:
1 2 3 4 |
|
有沒有更簡(jiǎn)單的語句可以做到呢?
1.
1 |
|
2. Python2.x,可以使用map函數(shù)
1 |
|
如果是3.x,map返回的是map對(duì)象,當(dāng)然也可以轉(zhuǎn)換為List:
1 |
|
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ì)您有幫助就好】元
