美文网首页
python一些知识碎片4

python一些知识碎片4

作者: 淡若s清茶 | 来源:发表于2018-08-06 21:11 被阅读0次

一、hash 返回数值

二、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))
结果

四、slice

五、numpy的运用

1.条件赋值

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

2.切片改变数值

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

3.where使用

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

4.数组重构 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))

相关文章

网友评论

      本文标题:python一些知识碎片4

      本文链接:https://www.haomeiwen.com/subject/flcnvftx.html