加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

【Python】收集的高级函数、功能

发布时间:2020-12-17 01:21:58 所属栏目:Python 来源:网络整理
导读:说明:本文更新顺序是从下到上,最新的函数更新在开头。 Python日志实时写入 pre class="has" open 函数中有一个bufferin的参数,默认是-1,如果设置为0是,就是无缓冲模式。 但是用二进制模式打开这个文件,并且把要写入的信息转换byte -like如下。 with op
<tr><td style="border-color:#cccccc;vertical-align:baseline;">

1

        <p>2</p>

        <p>3</p>

        <p>4</p>

        <p>5</p>
        </td&gt;
        <td style="border-color:#cccccc;vertical-align:baseline;width:611px;"&gt;
        <p><code>In [</code><code>2</code><code>]: </code><code>eval</code><code>(</code><code>'a = 1'</code><code>)</code></p>

        <p><code>?</code><code>File</code> <code>"<string>"</code><code>,line </code><code>1</code></p>

        <p><code>?</code><code>a </code><code>=</code> <code>1</code></p>

        <p><code>??</code><code>^</code></p>

        <p><code>SyntaxError: invalid syntax</code></p>
        </td&gt;
    </tr&gt;</tbody></table&gt;<p>  百度没百度到结果,最后在stackoverflow上找到了好的答案.</p>

  作者的意思是,eval函数只负责对表达式进行处理,并没有赋值的功能,也就是说,eval函数只负责对你的输入进行输出,True还是False又或者是什么东西。但它本身是没有影响当前代码环境的能力的。如果我们想用来进行赋值,那么应该使用exec()函数。看代码:

<table border="0" cellpadding="0" cellspacing="0" style="margin-left:0px;width:643px;">

<tr><td style="border-color:#cccccc;vertical-align:baseline;">

1

        <p>2</p>

        <p>3</p>
        </td&gt;
        <td style="border-color:#cccccc;vertical-align:baseline;width:611px;"&gt;
        <p><code>In [</code><code>3</code><code>]: </code><code>exec</code><code>(</code><code>'a = 1'</code><code>)</code></p>

        <p><code>In [</code><code>4</code><code>]: a</code></p>

        <p><code>Out[</code><code>4</code><code>]: </code><code>1</code></p>
        </td&gt;
    </tr&gt;</tbody></table&gt;<p>  问题的解决方案已经供出了,那么我们现在再看看官方文档对这两个函数怎么说。</p>

<pre class="has">
eval(expression,global=None,local=None)

????? 参数是字符串和可选的global和local。global应当为一个字典文件,local应为一个映射对象。

  expression参数将被处理为一个python的表达式(严格来说,是一串条件语句),global和local参数将被用来当做全局和局部的命名空间。

<pre class="has">
exec(object[,global,[locals])

  这个函数能够为python提供动态的代码执行功能。

多层创建目录函数os.makedirs(path)。这两个函数之间最大的区别是当父目录不存在的时候os.mkdir(path)不会创建,os.makedirs(path)则会创建父目录。

在 python 中,一共有两个 join 方法,一个是 str.join(),另一个是 os.path.join()?

str.join(iterable):将可迭代对象中的元素以 str 为分隔符拼接返回,但是这些元素必须为 String 类型,不然会报错。

注:当要使用大量的字符串拼接时,尽量避免 + 操作,这样会产生大量的临时变量,占据内存,可以先将其拼接到 list 中,然后使用 join 方法

>> a
array([[-4,-4,-5,[-1,-2,-1,3],[ 0,-3,-4],-4]])
# 
>>> np.maximum(a,0)
array([[0,0]])

当程序出现错误,python会自动引发异常,也可以通过raise显示地引发异常。

一旦执行了raise语句,raise后面的语句将不能执行。演示raise用法

<pre class="has">
<code class="language-python">try:
s = None
if s is None:
print "s 是空对象"
raise NameError #如果引发NameError异常,后面的代码将不能执行
print len(s) #这句不会执行,但是后面的except还是会走到
except TypeError:
print "空对象没有长度"

s = None
if s is None:
raise NameError

print 'is here?' #如果不使用try......except这种形式,那么直接抛出异常,不会执行到这里

raise语法格式如下:

<pre class="has" style="margin-left:15px;">
<code class="language-prettyprint"><span style="color:#000088;">raise<span style="color:#000000;"> <span style="color:#666600;">[<span style="color:#660066;">Exception<span style="color:#000000;"> <span style="color:#666600;">[,<span style="color:#000000;"> args <span style="color:#666600;">[,<span style="color:#000000;"> traceback<span style="color:#666600;">]]]<span style="color:#000000;"> <span style="color:#666600;">[<span style="color:#660066;">Exception<span style="color:#000000;"> <span style="color:#666600;">[,<span style="color:#000000;"> traceback<span style="color:#666600;">]]]

一个异常可以是一个字符串,类或对象。 Python的内核提供的异常,大多数都是实例化的类,这是一个类的实例的参数。

定义一个异常非常简单,如下所示:

<pre class="has">
def functionName( level ):
if level < 1:
raise Exception("Invalid level!",level)

触发异常后,后面的代码就不会再执行

def functionName( level ):
if level < 1:
raise Exception("Invalid level!",level)

触发异常后,后面的代码就不会再执行

注意:为了能够捕获异常,"except"语句必须有用相同的异常来抛出类对象或者字符串。

例如我们捕获以上异常,"except"语句如下所示:

<pre class="has">
try:
? ? 正常逻辑
except "Invalid level!":
? ? 触发自定义异常 ? ?
else:
? ? 其余代码
:
? ? 正常逻辑
except "Invalid level!":
? ? 触发自定义异常 ? ?
else:
? ? 其余代码

set集合说复杂不复杂,说简单也不简单。简单见下例:

<pre class="has">
<code class="language-python">>>> x = set('spam')

y = set(['h','a','m'])
x,y
(set(['a','p','s','m']),set(['a','h','m']))

x & y # 交集
set(['a','m'])

x | y # 并集
set(['a','m'])

x - y # 差集
set(['p','s'])

不简单是因为,当二维list或者numpy数组想要转set时,就会报错。而有些操作如:并集、交集、差集,必须要用set对象操作。

PS:楼主一晚上憋了三行代码:

<pre class="has">
<code class="language-python"> [_a,_b,_c] = map(lambda x: np.array(np.where(x != 0)).transpose(),[pos-elem_to_reduce,lbest-pos,gbest-pos])
[a,b,c] = map(lambda y: set([tuple(it) for it in y]),[_a,_c])
indices = (b | c) & a

numpy.where函数是三元表达式x if condition else y的矢量化版本。定义一个布尔数组和两个值数组:

<pre class="has">
<code class="language-python">>>> xarr=np.array([1.1,1.2,1.3,1.4,1.5])

yarr=np.array([2.1,2.2,2.3,2.4,2.5])
cond=np.array([True,False,True,False])
result=[(x for c else y) for x,y,c in zip(xarr,yarr,cond)]
File "",line 1
result=[(x for c else y) for x,cond)]
^
SyntaxError: invalid syntax
result=[(x if c else y) for x,cond)]
result
[1.1000000000000001,2.2000000000000002,1.3999999999999999,2.5]

以上做法有几个问题:第一,速度不够快;第二:无法用于多维数组。若用np.where,则可以将该功能写得非常简洁。

<pre class="has">
<code class="language-python">>>> reslut=np.where(cond,xarr,yarr)

result
[1.1000000000000001,2.5]

再看一个例子,既能找出位置,又能方便找出元素:

<pre class="has">
<code class="language-python">>>> x = np.arange(9.).reshape(3,3)

np.where( x > 5 )
(array([2,2]),array([0,2]))
x[np.where( x > 3.0 )] # Note: result is 1D.
array([ 4.,5.,6.,7.,8.])
np.where(x < 5,x,-1) # Note: broadcasting.
array([[ 0.,1.,2.],[ 3.,4.,-1.],[-1.,-1.,-1.]])

b.flatten()
Out[42]: array([1,6])

b
Out[43]:
array([[1,6]])

注意,当数组列长度不一样时flatten失效。

<pre class="has">
<code class="language-python">b
Out[126]: array([[0,13],[32,33,30],29,23]],dtype=object)

b.flatten()
Out[127]: array([[0,dtype=object)

这时应该改为np.hstack()函数

<pre class="has">
<code class="language-python">b
Out[128]: array([[0,dtype=object)

np.hstack(b)
Out[129]: array([ 0,...,23])

使用数组的reshape方法,可以创建一个改变了尺寸的新数组,原数组的shape保持不变:

<pre class="has">
<code class="language-python">>>> d = a.reshape((2,2))

d
array([[1,4]])
a
array([1,4])

数组a和d其实共享数据存储内存区域,因此修改其中任意一个数组的元素都会同时修改另外一个数组的内容!

<pre class="has">
<code class="language-python">>>> a[1] = 100 # 将数组a的第一个元素改为100

d # 注意数组d中的2也被改变了
array([[ 1,100],[ 3,4]])

Note: 需要注意的是,这里与MATLAB不一样,MATLAB变换是按列向量来的,而NUMPY是基于行向量。

问题:python filter 如何应用在numpy array中,比如我有一个二维numpy数组,想将数组中小于0的置零大于零的保留原值,但是不想用for循环,有没有比较高效简洁的方法。

<pre class="has">
<code class="language-python">>>> import numpy as np

a = np.random.randint(-5,(5,5))
a
array([[-4,-4]])

方式一

np.maximum(a,0]])

方式二

(a + abs(a)) / 2
array([[0,0]])

方式三

b = a.copy()
b[b < 0] = 0
b
array([[0,0]])

方式四

np.where(a > 0,a,0]])

链接:https://www.zhihu.com/question/46988087/answer/115228501

Views(视图)的作用,类似于数据库中视图的一个作用,数据库表变化后,视图查询的结果是变化后的值。python中通过Views查询的值也是这样的。

dict.keys(),dict.items(),dict.values()返回的是Views。

<pre class="has">
<code class="language-python">>>> dishes = {'eggs': 2,'sausage': 1,'bacon': 1,'spam': 500}

keys = dishes.keys()
values = dishes.values()

iteration

n = 0
for val in values:
... n += val
print(n)
504

keys and values are iterated over in the same order

list(keys)
['eggs','bacon','sausage','spam']
list(values)
[2,500]

view objects are dynamic and reflect dict changes

del dishes['eggs']
del dishes['sausage']
list(keys)
['spam','bacon']

set operations

keys & {'eggs','salad'}
{'bacon'}

如果引入文件是在子目录,如:

folder

------tobeinvodedA.py

------tobeinvodedB.py

------tobeinvodedC.py

toinvoke.py

这种情况,在folder 下新建一个__init__.py 的空文件,此时的folder不再是一个普通的文件夹,而是一个包 package。现在像这样

folder #文件夹 现在的性质为一个python包package?

------__init__.py

------tobeinvoded.py

------tobeinvodedA.py

------tobeinvodedB.py

------tobeinvodedC.py

toinvoke.py

这样在toinvoke.py 中引入

import folder.toveinvoked 或 from folder.tobeinvoked import *

即可

如果是外部目录,如folderB中的模块要调用folderA中的模块,方法同上

folderA?

------tobeinvoded.py

------tobeinvodedA.py

------tobeinvodedB.py

------tobeinvodedC.py

folderB?

--------toinvoke.py

这样在toinvoke.py 中引入

import folder.toveinvoked 或 from folder.tobeinvoked import *

即可

Python的包搜索路径

Python会在以下路径中搜索它想要寻找的模块:

  • 程序所在的文件夹
  • 标准库的安装路径
  • 操作系统环境变量PYTHONPATH所包含的路径

将自定义库的路径添加到Python的库路径中去,可以在程序运行过程中修改sys.path的值,动态地添加自己的库路径:

<pre class="has">
<code class="language-python">import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(file)))
sys.path.insert(0,parentdir)

sys模块是使用c语言编写的,因此字符串支持 'n','r','t'等来表示特殊字符。所以最好写成:?sys.path.append('c:xxxb.py') 避免转义

或者,在Python安装目录下的Libsite-packages文件夹中建立一个.pth文件,内容为自己写的库路径。示例如下:

E:workPythonhttp

  • shuffle()随机排序

shuffle() 方法将序列的所有元素随机排序。

random.shuffle 并不返回一个list,而是原地改变。

  • 字典关键字获取的特殊方式

偶然从一个良心bug中发现的

<pre class="has">
<code class="language-python">nets = {
"RG": 1,}
for net in nets:
print(net)

RG

用法:zeros(shape,dtype=float,order='C')

返回:返回来一个给定形状和类型的用0填充的数组;

参数:shape:形状

? ? ? ? ? ? dtype:数据类型,可选参数,默认numpy.float64

? ? ? ? ? ? dtype类型:t,位域,如t4代表4位

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?b,布尔值,true or false

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?i,整数,如i8(64位)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? u,无符号整数,u8(64位)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? f,浮点数,f8(64位)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?c,浮点负数,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? o,对象,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s,a,字符串,s24

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?u,unicode,u24

? ? ? ? ? ? order:可选参数,c代表与C语言类似,行优先;F代表列优先

  • numpy数组切片和部分赋值
>>a=np.array([0,0])
>>> b="123"
>>> a[len(a)-len(b):]=list(b)
>>> a
array([0,3])
  • Python中字符串与数组的转换
 'xyz'
b = ''.join(['x','z'])

<pre class="has">
<code class="language-python">'xyz' => ['x','z']
a = list('xyz')

  • python字符串str和字节数组相互转化
str object

s = "example"

str to bytes

bytes(s,encoding = "utf8")

bytes to str

str(b,encoding = "utf-8")

an alternative method

str to bytes

str.encode(s)

bytes to str

bytes.decode(b)

参考:http://blog.csdn.net/gong_xucheng/article/details/45216721

  • bin —— 十进制转化为二进制

将十进制数转化为二进制,并去除前面标记符:bin(number).replace('0b','')

二进制转化为十进制:int(number,2)

  • L[::-1] ?—— numpy数组倒序

一般形式是seq[start: end: step],由于seq的slice操作带有步长的操作,因此,在设计中可以使用负数作为步长,当step为-1时,指定需要逆序的元素区间,则可以逆序

  • xrange —— 迭代器

range返回的是一个包含所有元素的列表,xrange返回的是一个生成器,生成器是一个可迭代对象,在对生成器进行迭代时,元素是逐个被创建的。一般来看,在对大序列进行迭代的时候,因为xrange的特性,所以它会比较节约内存。

>> a=np.array([1,3])
>>> b=np.array([11,22,33])
>>> c=np.array([44,55,66])
>>> np.concatenate((a,c),axis=0)  # 默认情况下,axis=0可以不写
array([ 1,44,66]) #对于一维数组拼接,axis的值不影响最后的结果

a=np.array([[1,[4,6]])
b=np.array([[11,21,31],[7,9]])
np.concatenate((a,b),axis=0)
array([[ 1,[ 4,6],[11,[ 7,9]])

np.concatenate((a,axis=1) #axis=1表示对应行的数组进行拼接
array([[ 1,9]])

对numpy.append()和numpy.concatenate()两个函数的运行时间,concatenate()效率更高,适合大规模的数据拼接。

  • 比较[it if else for...] 与 [it for ... if]
a=[1,1]

b=[index for (index,value) in enumerate(a) if value != 0]

b
Out[396]: [0,5]

b=[index if value != 0 else -1 for (index,value) in enumerate(a)]

b
Out[398]: [0,5]

  • print打印所有列表元素

在开头加入:

<pre class="has">
<code class="language-python">import numpy as np
np.set_printoptions(threshold=np.inf)

  • print (s,file = f)—— print日志到文件
  • np.diag() —— 创建对角矩阵

X=diag(v,k)

以向量v为矩阵X的第k条对角线,当k=0时,向量v为X的主对角线,k>0时,v为主对角线上方的第k条对角线,k<0时,v为主对角线下方的第k条对角线。

格式变体:

X=diag(v):以向量v为矩阵X的主对角线,即默认k=0。

v=diag(X,k)

从矩阵中抽取一条对角线返回给向量v。当k=0时,抽取主对角线,k>0时,抽取主对角线上方的第k条对角线,k<0时,抽取主对角线下方的第k条对角线。格式变体:

V=diag(X):抽取矩阵X的主对角线元素,即默认k=0。

  • Python time clock()方法

该函数有两个功能,

在第一次调用的时候,返回的是程序运行的实际时间;

以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔

在win32系统下,这个函数返回的是真实时间(wall time),而在Unix/Linux下返回的是CPU时间。

所以程序中计时一般用time.time()方法,而不是容易出错的time.clock()

  • print-seq —— 打印分隔符

python print()使用其他分隔符或行终止符打印

可以在 print() 函数中使用 sep 和 end 关键字参数,以你想要的方式输出。比如:

<pre class="has">
<code class="language-python">>>print('abc','def','123',sep=',',end='???n')
abc,def,123
???

  • numpy.squeeze(a) —— 删除1维度

从数组中删除所有为一的维度;

<pre class="has">
<code class="language-python">>>> x = np.array([[[0],[1],[2]]])

x.shape
(1,1)
np.squeeze(x).shape
(3,)

在python中,全局变量一般有两种使用方式:

第一种:是在一个单独的模块中定义好,然后在需要使用的全局模块中将定义的全局变量模块导入。

第二种:直接在当前的模块中定义好,然后直接在本模块中通过global声明,然后使用.

random.sample(list,5)

choice() 方法返回一个列表,元组或字符串的随机项。

round() 是python内置函数,round()不是简单的四舍五入的处理方式。

<pre class="has">
<code class="language-python">>>> round(2.5)
2

round(1.5)
2
round(2.675)
3
round(2.675,2)
2.67

?round()如果只有一个数作为参数,不指定位数的时候,返回的是一个整数,而且是最靠近的整数(这点上类似四舍五入)。

但是当出现.5的时候,两边的距离都一样,round()取靠近的偶数,这就是为什么round(2.5) = 2。

当指定取舍的小数点位数的时候,一般情况也是使用四舍五入的规则,但是碰到.5的这样情况,如果要取舍的位数前的小树是奇数,则直接舍弃,如果偶数这向上取舍。看下面的示例:

<pre class="has">
<code class="language-python">>>> round(2.635,2)
2.63

round(2.645,2)
2.65
round(2.655,2)
2.65
round(2.665,2)
2.67
round(2.675,2)
2.67

  • np.linalg.eigvals() —— 求矩阵特征值

# 只求特征值

求特征值加特征向量:

<pre class="has">
<code class="language-python">import numpy as np
x= np.mat([[4,-1],3]])
a,b=np.linalg.eig(x)

  • map(op,b) —— 用map写点积

时间最快()

<pre class="has">
<code class="language-python">import operator
from itertools import izip,starmap
sum(starmap(operator.mul,izip(r,r)))

  • 一句话写乘法口诀表
  • adjacency_spectrum —— 图的邻接矩阵特征值
  • nx.all_neighbors —— 图节点的邻居
If the graph is directed returns predecessors as well as successors.</code></pre>
  • nx.adjacency_matrix —— 图的邻接矩阵

# 图的邻接矩阵

<pre class="has">
<code class="language-python">>>> import scipy as sp

G = nx.Graph([(1,1)])
A = nx.adjacency_matrix(G)
print(A.todense())
[[1]]
A.setdiag(A.diagonal()*2)
print(A.todense())
[[2]]

<pre class="has">
<code class="language-python">nx.from_numpy_matrix

从矩阵画出图

import networkx as nx
import numpy as np

A = [[0.000000,0.0000000,0.05119703,1.3431599],[0.000000,-0.6088082,0.4016954,0.00000000,0.6132168],-0.63295415,0.0000000],-0.29831267,[0.051197,-0.6329541,-0.2983127,0.1562458],[1.343159,0.6132168,0.15624584,0.0000000]]

G = nx.from_numpy_matrix(np.array(A))
nx.draw(G,with_labels=True)

  • pattern —— 实现列表元素替换

映射替换,根据一个字典的映射关系替换,下例里把 ‘3’ 和 ‘4’ 都替换成英文:

<pre class="has">
<code class="language-python">>>> lst = ['1','2','3','4','5']

pattern = {'3':'three','4':'four'}
rep = [pattern[x] if x in pattern else x for x in lst]
rep
['1','three','four','5']

  • enumerate —— 取元素列表索引

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

说明:本文更新顺序是从下到上,最新的函数更新在开头。

Python日志实时写入

<pre class="has">
open 函数中有一个bufferin的参数,默认是-1,如果设置为0是,就是无缓冲模式。
但是用二进制模式打开这个文件,并且把要写入的信息转换byte -like如下。

with open("test.txt",'wb',buffering=0) as f:

wb是写模式加二进制模式

f.write(b"hello!")在字符串前加b,转换成二进制

如果没用二进制打开文件会提示ValueEorror:

没把字符串转成二进制会提示:TypeError: a bytes-like object is required,not ‘str’

测试:

<pre class="has">
class Logger(object):
def init(self,log_path="default.log"):
self.terminal = sys.stdout

self.log = open(log_path,"w+")

    self.log = open(log_path,"wb",buffering=0)

def print(self,message):
    self.terminal.write(message + "n")
    self.log.write(message.encode('utf-8') + b"n")

def print_time(self,text=""):
    message = time.strftime('[ %Y-%m-%d %H:%M ]',time.localtime(time.time())) + text
    self.print(message)

def flush(self):
    self.terminal.flush()
    self.log.flush()

def close(self):
    self.log.close()

报错:TypeError: can't concat str to bytes

这是因为:

(1)log.write需要写入bytes对象,encode返回的是bytes型的数据,不可以和str相加,将‘n’前加b

(2)terminal.write函数参数需要为str类型,转化为str

<pre class="has">
a=np.arange(10)
a
array([0,1,2,3,4,5,6,7,8,9])
a[0]=11
a
array([11,9])
np.argsort(a)
array([1,9,0],dtype=int64)

首先这里p_arr为一个numpy的array,p_为一个元素

<pre class="has">
p_arr = np.concatenate((parr,[p])) # 先将p_变成list形式进行拼接,注意输入为一个tuple
p_arr = np.append(parr,p) #直接向parr里添加p

注意一定不要忘记用赋值覆盖原p_arr不然不会变

但是,仍然建议用list子代append。因为numpy的append默认添加的值全转化为float64了。

shutil.copy(sourceDir,targetDir)

os.path.join():路径拼接

<pre class="has">
import networkx as nx

抽取txt中的数据

def read_txt(data):
g = nx.read_edgelist("data",create_using=nx.DiGraph())
print(g.edges())

抽取gml中的数据

networkx可以直接通过函数从gml文件中读出数据

def read_gml(data):
H=nx.read_gml(data)
print(H.edges())

read_txt('D:Artifical平均度4SF2-4.txt')
print('---------------gml------------------')
read_gml('D:文档论文代码社区发现数据dataadjnounadjnoun.gml')


原文:https://blog.csdn.net/qq_38266635/article/details/81743336

<pre class="has">
<code class="language-html">while len(list(nx.dfs_edges(G)))!=G.number_of_nodes()-1:
G = RG(100,e)
print("not connected!")

def get_adj_matrix(G):
? ? return nx.adjacency_matrix(G).todense()

todense()与toarray()

todense()返回一个矩阵,toarray()返回一个numpy数组。

np.random.choice

1、以p的概率,在a中取size个数,p值越大越可能被取到;2、当replace=True时,取值可重复

<pre class="has">
import numpy as np
a1=np.array([3,2.6,41.2,5.7])
a2 = np.random.choice(a=a1,size=3,replace=False,p=[0.2,0.1,0.3,0.4,0.0])
print(a2)
输出: array([41.2,?5.,?2.6])

注意sample是属于random模块,而不是nump.random模块,二者不一样。

注意n必须是整数,返回一个列表。

??

???a = [[1,2],[3,4],[5,6]]

???1.sum(map(sum,a)) #first,map(func,a) 函数是对a中的每一个元素进行sum操作

??解释一下map函数, map(fund,a) ??equals ??[func(i) for i in a] ?and return a list

???2.sum(sum(i) for i in a) #second one

???3.sum(sum(a[i]) for i in range(len(a))) #third one

???4.reduce(lambda x,y:x+y,reduce(lambda x,a))
---------------------?
作者:jinmingz?
来源:CSDN?
原文:https://blog.csdn.net/zjm750617105/article/details/51173032?
版权声明:本文为博主原创文章,转载请附上博文链接!

  • reshape:有返回值,所谓有返回值,即不对原始多维数组进行修改;
  • resize:无返回值,所谓有返回值,即会对原始多维数组进行修改;
  • 但是,numpy.resize(a,size)不会对原数组进行修改。
>> X = np.random.randn(2,3)
>> X
array([[ 1.23077478,-0.70550605,-0.37017735],[-0.61543319,1.1188644,-1.05797142]])

X.reshape((3,2))
array([[ 1.23077478,-0.70550605],[-0.37017735,-0.61543319],[ 1.1188644,-1.05797142]])

X
array([[ 1.23077478,-1.05797142]])

X.resize((3,2))
X
array([[ 1.23077478,-1.05797142]])

来自:https://blog.csdn.net/lanchunhui/article/details/51258503?utm_source=copy

(1)通过reshape生成的新数组和原始数组公用一个内存,也就是说,假如更改一个数组的元素,另一个数组也将发生改变。

(2)reshape只能作用于能规整划分的数组。

<pre class="has">
<code class="language-python">>>> z = np.array([[1,8],[9,10,11,12],[13,14,15,16]])

print(z)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
print(z.shape)
(4,4)
print(z.reshape(-1))
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
print(z.reshape(-1,1)) #我们不知道z的shape属性是多少,

但是想让z变成只有一列,行数不知道多少,

通过z.reshape(-1,1),Numpy自动计算出有16行,

新的数组shape属性为(16,1),与原来的(4,4)配套。

[[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]
[11]
[12]
[13]
[14]
[15]
[16]]
print(z.reshape(2,-1))
[[ 1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16]]


<pre class="has">
>>> arr = np.arange(10)

np.random.shuffle(arr)
arr
[1 7 5 2 9 4 3 6 0 8]
This function only shuffles the array along the first index of a multi-dimensional array:
arr = np.arange(9).reshape((3,3))
np.random.shuffle(arr)
arr
array([[3,5],? ? ? ?[6,? ? ? ?[0,2]])

返回一个随机排列

<pre class="has">
<code class="language-python">a=np.arange(9)
np.random.permutation(a)
Out[39]: array([0,6])
a
Out[40]: array([0,8])
b=np.arange(9).reshape((3,3))
b
Out[42]:
array([[0,[6,8]])
np.random.permutation(b)
Out[43]:
array([[6,[0,5]])
b
Out[44]:
array([[0,8]])


? ?a = [[1,6]]
? ?1.sum(map(sum,a) 函数是对a中的每一个元素进行sum操作
? 解释一下map函数, map(fund,a)? ?equals?
? [func(i) for i in a]? and return a list
? ?2.sum(sum(i) for i in a) #second one
? ?3.sum(sum(a[i]) for i in range(len(a))) #third one
? ?4.reduce(lambda x,a))
? ? ?解释一下reduce(fun,a),只不说reduce返回的是一个结果值而不是一个list,第一步的时候是([1,2]+[3,4]) + [5,6]
? ? ?得到一个[1,2,3,4,5,6], 然后进行的运算是(((((1+2)+3)+4)+5)+6) = 21

功能:从一维数组a中以概率p抽取抽取元素,形成size形状新的数组,replace表示是否可以重用元素,默认为True。举例:

<pre class="has">
<code class="language-python">np.random.choice([0,1],(2,3),p=[0.1,0.9])
Out[14]:
array([[1,[1,1]])
np.random.choice([0,p=[0.5,0.5])
Out[15]:
array([[0,0.5])
Out[16]:
array([[0,0]])

本来是想打算使用eval函数对变量进行赋值的,没想到出现了invalid syntax错误。源代码如下

<table border="0" cellpadding="0" cellspacing="0" style="margin-left:0px;width:643px;">

    推荐文章
      热点阅读