纯数字判断:isnumeric()
>>> str.isnumeric()
False
>>> str = '12312311111111111111'
>>> str.isnumeric()
True
纯字母判断: isalpha();前提是unicode字符
>>> str = 'aaaasd'
>>> str.isalpha()
True
>>> str = 'aaaasd1111'
>>> str.isalpha()
False
>>> str = 'aaaasd安全'
>>> str.isalpha()
True
>>> str = u'aaaasd安全'
>>> str.isalpha()
True
>>> str.encode('utf-8').isalpha()
False
>>> str = '安全'
>>> str.encode('utf-8').isalpha()
False
>>> str = 'aaaasd'
>>> str.encode('utf-8').isalpha()
True
python元素增加
>>> str = ['a','b']
>>> str.append('11')
>>> str
['a', 'b', '11']
列表合并:
>>> str1 = ['a','b','c']
>>> str1 = ['1','2','3']
>>> str1 = ['a','b','c']
>>> str2 = ['1','2','3']
>>> str1.extend(str2)
>>> str1
['a', 'b', 'c', '1', '2', '3']
python元素删除
remove:
>>> str1 = ['a','b','c']
>>> str1.remove('b')
>>>
>>>
>>> str1
['a', 'c']
del:
>>> str1 = ['a','b','c']
>>>
>>>
>>> del str1[1]
>>>
>>> str1
['a', 'c']
pop():
>>> str1 = ['a','b','c']
>>>
>>>
>>>
>>> str1.pop(2)
'c'
>>>
>>>
>>> str1
['a', 'b']
>>>
列表删除注意:
需要倒序删除,否则会下标越界:
def test(data):
for i in data[::-1]:
data.remove(i)
return data
data = [1, 2, 3]
print(test(data))
综合下: