數(shù)據(jù)類型轉(zhuǎn)換
基本數(shù)據(jù)類型轉(zhuǎn)換
| 轉(zhuǎn)換方向 | 轉(zhuǎn)換函數(shù) | 說明 |
|---|---|---|
string->int |
int(str_value) |
將字符串str_value轉(zhuǎn)成一個整數(shù),str_value也可為long/float
|
num->string |
str(num_value) |
將一個數(shù)(int/long/float)num_value轉(zhuǎn)成字符串 |
x->float |
float(x) |
將x轉(zhuǎn)換為一個浮點數(shù) |
x->long |
long(x) |
將x轉(zhuǎn)換為一個長整數(shù) |
s->tuple |
tuple(s) |
將序列s轉(zhuǎn)化成元組tuple
|
s->list |
list(s) |
將序列s轉(zhuǎn)化成列表list
|
s->set |
set(s) |
將序列s轉(zhuǎn)化成可變集合 set
|
char->num |
ord(c) |
將一個字符轉(zhuǎn)化成其整數(shù)值(ASCII碼值) |
num->char |
chr(c) |
將一個整數(shù)(ASCII碼值)轉(zhuǎn)化成其對應(yīng)的字符 |
num->hex |
hex(x) |
將一個整數(shù)值轉(zhuǎn)化為其對應(yīng)的十六進(jìn)制字符串 |
num->oct |
oct(x) |
將一個整數(shù)值轉(zhuǎn)化成其對應(yīng)的八進(jìn)制字符串 |
特殊轉(zhuǎn)換方法
- 將
list中的字符串全部轉(zhuǎn)化成數(shù)值(list中僅包含可以轉(zhuǎn)化成數(shù)值的字符串)
list(map(int, a))
a = ['123', '432', '31']
b = ['12.43', '54.6754', '12.453']
c = list(map(int, a))
d = list(map(float, a))
print(c)
print(d)
# 運行結(jié)果
[123, 432, 31]
[12.43, 54.6754, 12.453]
- 將
list中的數(shù)值全部轉(zhuǎn)化成字符串(list中可以包含一切能轉(zhuǎn)成字符串的對象)
list(map(str, a))
e = [12.43, 54.6754, 12.453, '123']
f = list(map(str, e))
print(f)
# 運行結(jié)果
['12.43', '54.6754', '12.453', '123']
數(shù)值 nan 的處理
判斷一個值是否是 nan
- 使用nunpy進(jìn)行判斷
import numpy as np
#若要判斷變量Number是不是nan
np.isnan(number)
- 使用math
import math
math.isnan(number)
- 自己定義判別函數(shù)
# 自己定義一個判斷nan的函數(shù)
def isNan(num):
return num != num
示例
import math
import numpy as np
a = float('nan')
b = 10
# 自己定義一個判斷nan的函數(shù)
def isNan(num):
return num != num
print("a is {}".format(a))
print("b is {}".format(b))
print("{} --> {} is nan: {}".format('isNan', a, isNan(a)))
print("{} --> {} is nan: {}".format('isNan', b, isNan(b)))
print("{} --> {} is nan: {}".format('math', a, math.isnan(a)))
print("{} --> {} is nan: {}".format('math', b, math.isnan(b)))
print("{} --> {} is nan: {}".format('numpy', a, np.isnan(a)))
print("{} --> {} is nan: {}".format('numpy', b, np.isnan(b)))
# 輸出
a is nan
b is 10
isNan --> nan is nan: True
isNan --> 10 is nan: False
math --> nan is nan: True
math --> 10 is nan: False
numpy --> nan is nan: True
numpy --> 10 is nan: False
nan 轉(zhuǎn)化成數(shù)值
- 利用numpy的
nan_to_num函數(shù)實現(xiàn):
a = float('nan')
b = 10
print(np.nan_to_num(a, nan=100))
print(np.nan_to_num(a))
print(np.nan_to_num(b))
# 輸出
100.0
0.0
10
- 根據(jù)nan判斷函數(shù),自己寫轉(zhuǎn)換函數(shù)
日期處理
使用datetime模塊處理日期:
>>> import datetime
>>> d1 = datetime.datetime(2018,10,31) # 第一個日期
>>> d2 = datetime.datetime(2019,2,2) # 第二個日期
>>> interval = d2 - d1 # 兩日期差距
>>> interval.days # 具體的天數(shù)
94
>>> interval.seconds # 具體的秒數(shù)
0
datetime.datetime()常用模塊有:
>>> filter(lambda x: not x.startswith("_"), dir(interval))
['days', 'max', 'microseconds', 'min', 'resolution', 'seconds', 'total_seconds']
如果是兩個日期還是帶時,分,秒的話,同樣可以算出兩者的相距天數(shù)及秒數(shù).
>>> import datetime
>>> d1 = datetime.datetime(2018,10,31,10,30,00)
>>> d2 = datetime.datetime(2018,11,1,10,40,30)
>>> interval = d2 - d1
>>> interval # 第一項是天數(shù),相距1天
datetime.timedelta(1, 630)
>>> interval.days # 具體天數(shù)
1
>>> interval.seconds # 額外秒數(shù)
630
>>> interval.total_seconds() # 相差總秒數(shù)
87030.0