一個STR或INT如果我有如下列表:我如何才能找到索引是否是一個STR如何找到如果數據是在列表
samplist= [3,4,5,'abc']
或Int.I想連接一個字符串s ='def'與列表中可用的字符串'abc'。
假設我不知道我對字符串一無所知,既不是名稱也不是它在列表中的索引。我所知道的是列表samplist中有一個字符串,我想循環並找出它,以便它可以與字符串s ='def'/連接。
一個STR或INT如果我有如下列表:我如何才能找到索引是否是一個STR如何找到如果數據是在列表
samplist= [3,4,5,'abc']
或Int.I想連接一個字符串s ='def'與列表中可用的字符串'abc'。
假設我不知道我對字符串一無所知,既不是名稱也不是它在列表中的索引。我所知道的是列表samplist中有一個字符串,我想循環並找出它,以便它可以與字符串s ='def'/連接。
for i in xrange(len(samplist)):
if isinstace(samplist[i], str):
samplist[i] += 'def'
type()
命令也:
>>> list = [1, 2, 'abc']
>>> type(list)
<class 'list'>
>>> type(list[1])
<class 'int'>
>>> type(list[2])
<class 'str'>
>>> type(list[2]) is str
True
>>> type(list[1]) is str
False
'isinstance '是一個更好的方法here.check [this](http://stackoverflow.com/a/1549854/2276527)out – Gogo
@shaktimaan是isinstance也照顧繼承,但我懷疑這是必要的。 –
也許使用類型() –
使用'ISDIGIT()'可能? – Gogo
@shaktimaan,'isdigit()'只能用於'str','unicode','bytes','bytearray',.. – falsetru