python一些知識碎片4

一、hash 返回數(shù)值

二、delattr刪除屬性,setattr,hasattr,getattr

setattr(People, "name", "hs")
print(People.name)

三、all,any 迭代判斷,any為空則為false

list1 = [1, 3, 5, 7, 9]
print(all(list1) < 10)
print(any(list1))
結(jié)果

四、slice

五、numpy的運(yùn)用

1.條件賦值

import numpy
l = numpy.arange(10)
print(l)
l2 = l[l > 5]
l3 = l[l % 3 == 0]
print(l2)
print(l3)

2.切片改變數(shù)值

l[1::2] = 1
print(l)

3.where使用

import numpy
l = numpy.arange(10)
l2 = numpy.where(l % 2 == 0, "草", l)
print(l2)

4.數(shù)組重構(gòu) reshape,repeat

import numpy
l = numpy.arange(10)
l2 = l.reshape(2, 5)
print(l2)
b = numpy.repeat(1, 12).reshape(3, -1)
print(b)

5.水平,垂直拼接

import numpy
a = numpy.arange(12).reshape(3, 4)
b = numpy.repeat(1, 12).reshape(3, -1)
c = numpy.vstack([a, b])
d = numpy.hstack([a, b])
print(c)
print(d)

6.獲取兩表共有元素

import numpy
a = numpy.arange(12).reshape(3, 4)
b = numpy.repeat(1, 12).reshape(3, -1)
print(numpy.intersect1d(a, b))
print(numpy.setdiff1d(a, b))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容