0%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
#-*- coding:utf8 -*-
from bs4 import BeautifulSoup as bs
import urllib2
import re
import pymysql
import os

#autor:Carlos
#功能:利用beautifulsoup,urllib2爬取ygdy8.net中电影下载链接


def html_downloader(url):
#读取网页内容
html_content = urllib2.urlopen(url).read()
return html_content

def parser(html_content):
#把网页内容传递给BeautifulSoup解析
soup = bs(html_content, 'html.parser', from_encoding='gb18030')
<!--more-->
#新建set,用以存储下一步爬取的链接。
new_urls = set()
#downloadlink存储下载链接
downloadlink=''
#获取网页中所有的内容页链接
urllist = soup.findAll('a',href=re.compile(r'/html/gndy/\w{4}/.*/'))
#将内容页链接拼接完整后存储至set new_urls中
for url in urllist :
new_urls.add('http://www.ygdy8.net'+url['href'])
#获取下载链接
if soup.find('a',href=re.compile(r'ftp\:\/\/(.*)')):
downloadlink=soup.find('a',href=re.compile(r'ftp\:\/\/(.*)'))['href']
return new_urls,downloadlink

def save(content):
#将爬取的内容保存到txt文档中
with open('downloadlist.txt','a') as f:
f.write(content)
f.close()

rooturl='http://www.ygdy8.net'
'''
html = html_downloader(url)
urls,download = parser(html)
print len(urls)
for url in urls:
print url
'''
#uncrawled_list为未爬取链接库,开始为空,格式为set,可以自动过滤重复链接。
uncrawled_list=set()
#crawled_list为已爬取链接库
crawled_list =set()
#将最开始爬取的链接放入未爬取链接库中
uncrawled_list.add(rooturl)
#download_list = set()
#count和num均为计数器,count记录爬取链接数量,num记录成功爬取下载链接数量。
count=0
num = 1
#未爬取链接库不为空时处于循环状态
while len(uncrawled_list)>0:
try:
count = count + 1
#从未爬取链接库中取出一个新的url
newurl = uncrawled_list.pop()
#判断新的url是否在已爬取链接库中
if newurl not in crawled_list:
html_content = html_downloader(newurl)
#new_urls,downloadlink = parser(html_content)
#url_manager(new_urls)
#将已爬过的链接放入crawled_list
crawled_list.add(newurl)
#返回新的内容页链接和下载链接
new_urls,downloadlink = parser(html_content)
#打印爬取状态
print "crawled %s:%s" % (count, newurl)
#判断new_urls是否为空
if new_urls is not None or len(new_urls)!=0:
for url in new_urls:
#将new_urls加入uncrawled_list
uncrawled_list.add(url)
#判断downloadlink是否为空
if downloadlink!='':
#将downloadlink格式
downloadurl = downloadlink.encode("utf8")
save(str(num)+'.'+downloadurl+'\r\n')
#download_list.add(downloadurl)
num=num+1
except:
print "crawl failed"

The count-and-say sequence is the sequence of integers with the first five terms as following:

  1. 1
  2. 11
  3. 21
  4. 1211
  5. 111221
    1 is read off as “one 1” or 11.
    11 is read off as “two 1s” or 21.
    21 is read off as “one 2, then one 1” or 1211. Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def countAndSay(n):
"""
:type n: int
:rtype: str
"""
oldstr='1'
for i in range(n-1):
tmp=''
count=1
for j in range(1,len(oldstr)+1):
if j< len(oldstr) and oldstr[j]==oldstr[j-1]:
count+=1
else:
tmp += str(count)+oldstr[j-1]
count = 1
oldstr = tmp
return oldstr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/urs/bin/python
#-*- coding:utf8 -*-
from bs4 import BeautifulSoup as bs
import urllib
import re
import json
import os

def get_musicid(url):
#url='http://music.baidu.com/top/dayhot'
html = urllib.urlopen(url).read()
soup = bs(html,'lxml',from_encoding='utf8')
urls = soup.findAll('a',href=re.compile(r'/song/(\d+)'))
musicidlist=set()
for url in urls:
musicidlist.add(url['href'].split('/')[-1])
return musicidlist


def parser(api):
#api="http://musicapi.qianqian.com/v1/restserver/ting?method=baidu.ting.song.play&format=jsonp&callback=jQuery17208098337996053833_1513859108469&songid=%s&_=1513859109906" % musicid
html=urllib.urlopen(api).read()
data = re.findall(r'\((.*)\)',html)[0]
jsondata = json.loads(data)
songtitle=jsondata['songinfo']['title']
songdownloadlink=jsondata['bitrate']['file_link']
songformat=jsondata['bitrate']['file_extension']
#print(jsondata)
return songtitle,songformat,songdownloadlink

def music_download(filename,downloadlink):
path= '/Users/apple/documents/music/' + filename
if(os.path.exists('/Users/apple/documents/music')==False):
os.makedirs('/Users/apple/documents/music/')
elif(os.path.isfile(path)==False):
urllib.urlretrieve(downloadlink, '/Users/apple/documents/music/' + filename)
else:
return

url='http://music.baidu.com/top/dayhot'
musicidlist = get_musicid(url)
for songid in musicidlist:
try:
api = "http://musicapi.qianqian.com/v1/restserver/ting?method=baidu.ting.song.play&format=jsonp&callback=jQuery17208098337996053833_1513859108469&songid=%s&_=1513859109906"%songid
songtitle,songformat,songdownloadlink=parser(api)
filename=songtitle+'.'+songformat
music_download(filename,songdownloadlink)
print(songtitle+' downloaded successfully!')
except:
print('download fail')
#parser(api)
#print(musicidlist)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
#-*- coding:utf8 -*-
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import re
import urllib
import os
#autor:carlos
#介绍:
# 利用selenium获取网易云音乐toplist页面中frame框架的源代码
# 利用BeautifulSoup获取音乐id、标题
# 拼接网易云音乐真实下载链接('http://music.163.com/song/media/outer/url?%s.mp3' % id)
# 利用urllib.urlretrieve()下载音乐文件
#利用selenium打开浏览器
driver=webdriver.Chrome()
#打开链接
driver.get('http://music.163.com/#/discover/toplist')
#跳转到contentFrame框架
driver.switch_to.frame('contentFrame')
#获取contentFrame框架源代码
html = driver.page_source
#driver.find_element_by_xpath("//span[@class='txt']")
#print driver.page_source
#browser.find_element_by_xpath(u"//img[@alt='强化学习 (Reinforcement Learning)']").click()
#browser.find_element_by_link_text("About").click()
#driver.get("http://music.163.com/#/discover/toplist")
# driver.find_element_by_xpath("//div[@id='g-topbar']/div/div/ul/li[2]/span/a/em").click()
# driver.find_element_by_xpath("//div[@id='g-topbar']/div/div/ul/li[3]/span/a/em").click()
# driver.find_element_by_css_selector("em").click()
# html=driver.page_source
# print html
# driver.close()
#退出浏览器
driver.quit()
soup = bs(html,'lxml')
#将音乐内容信息保存到contents中
contents = soup.findAll('div',{'class':'ttc'})
data=[]
for content in contents:
dataunit={}
#获取链接中的id部分 /song?id=525238891
id = content.find('a')['href'].split('?')[-1]
title = content.find('b')['title']
dataunit['id']=id
dataunit['title']=title
#将音乐id和标题保存在字典dataunit中,然后存放到list。
data.append(dataunit)

#利用urllib.urlretrieve()下载图片
def music_download(filename,downloadlink):
path= '/Users/apple/documents/music/' + filename
if(os.path.exists('/Users/apple/documents/music')==False):
os.makedirs('/Users/apple/documents/music/')
elif(os.path.isfile(path)==False):
urllib.urlretrieve(downloadlink, '/Users/apple/documents/music/' + filename)
else:
return
#循环输出list中的内容[{'title':'','id':''}]
for content in data:
#
title=content['title']
id=content['id']
#利用id拼接真实下载链接
downloadlink = 'http://music.163.com/song/media/outer/url?%s.mp3' % id
filename=title+'.'+'.mp3'

try:
music_download(filename,downloadlink)
print(title+' downloaded successfully!!')
except:
print('download failed!')

   十一回到了老家,虽然自从疫情解封到再次拥抱它的怀抱只有六个多月。在老家没有山珍海味,好看的、好玩的景物也早已吃完、玩完。但真正让我怀念的是,呆在故乡的那份安逸和从容,独在异乡为异客,每逢佳节倍思亲。自从到异乡上高中,而后到武汉读大学,再到结婚生子,养家糊口而为生活所累,前后算来离开家早已近20年。再也无法像小时候那样熟悉家里的一草一木,各种时节的变化、节气,张家长李家短,身边熟悉的人一个个变成熟悉的回忆,生活中缺乏了那种叫做“从容”的感觉,剩下的只是压迫的自己无法呼吸的“窒息”,但是一次久违的捡蘑菇让重新找到了家的感觉。

   蘑菇好吃吗?对于在外习惯了各种风味的饮食的我们,蘑菇给我们的印象或许仅仅停留在圆锥形的头和长长的杆,根据外形我们将他们命名为香菇、金针菇、平菇等等,但其实还有一种难得的美味,它隐藏在我们的骨子里,而并不随着时间的流逝而冲淡,这一次老家十一之旅,终于找到了这种感觉。

   在密林深处,茅草之间,腐叶之下,惊鸿一瞥,那种心惊肉跳,就跟初次见到心动女生一样激动,内心欢快而无法不能自已,恨不得立马把它捧在手心而据为己有。捧在手心里,它那被换润的粘液包裹的外壳,褐色的外表绽放着一种诱人的光芒,仿佛在告诉你,摘走我吧,我就是传说中好吃而且无法人工驯养的枞树菌,配上老家的酸糊汤,这可是难得的美味,多少次梦中思念的就是这种味道。

   月是故乡明,在心里保存着这份感觉,不在乎山有多高,水有多深,不在乎在哪里买房,在哪里工作,不在乎城市的建筑多么宏伟,心里始终惦记着那个出生并长大的地方,离家越远,思念越深。呆在它的怀里守候着那份安逸的时候,却发现自己早已不是那个小时候的自己,没了那份从容、对未来的无知和茫然中的不知所措,剩下的只是对生活的各种无奈,各种委屈中求全,求得片刻的安宁而开心无比。

   其实,生活就是这样,在你获得的同时让你散失。有得就有失,生活的至理名言早已告诉我们这个道理。而我们早已长大,早已不必纠结于各种小时候的记忆,还有更多的事情等着我们,就像草丛里的蘑菇一样,等待着我们去发现和探索,好像不是它们在等待我们去发现,而是命中注定,上天注定让我们相遇,并赐予我们心动而激情澎湃的瞬间。

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.

Example 1:
Input: [1,3,5,6], 5
Output: 2

Example 2:
Input: [1,3,5,6], 2
Output: 1

Example 3:
Input: [1,3,5,6], 7
Output: 4

Example 4:
Input: [1,3,5,6], 0
Output: 0

1
2
3
4
5
6
7
8
9
10
def searchInsert(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
for i in range(len(nums)):
if nums[i]>=target:
return i
return len(nums)
1
2
3
nums = [1,3,5,6]
target = 5
searchInsert(nums, target)
1
2
1
2
3
nums = [1,3,5,6]
target = 2
searchInsert(nums, target)
1
1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy
#从文件中读取数据到numpy
data = numpy.genfromtxt('/Users/apple/PycharmProjects/Carlos_python/data_science/downloadlist.txt',delimiter='',dtype='str')
print(type(data))
#print(help(numpy.genfromtxt))
#一维
vector = numpy.array([3,6,7,89,90])
#多维
matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])
print(vector)
print(matrix)
#矩阵的结构
print(vector.shape)
print(matrix.shape)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<class 'numpy.ndarray'>
[ 3 6 7 89 90]
[[12 3 67 9]
[34 2 7 9]
[ 3 8 9 6]]
(5,)
(3, 4)
#数据结构
numbers = numpy.array([1,2,3,4,5,6,7,8])
print(numbers)
print(numbers.dtype)
[1 2 3 4 5 6 7 8]
int64
#数据索引
matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])
#第一行第一个数
print(matrix[0,0])
12
matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])
#第三行最后一个数
print(matrix[2,-1])
6
#切片
vector = numpy.array([3,6,7,89,90])
print(vector[0:3])
[3 6 7]
matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])
#所有行的第二个数
print(matrix[:,1])
[3 2 8]
# matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])
#所有行的第一到二个数
print(matrix[:,0:2])

1
2
3
4
import numpy
#数据运算
vector = numpy.array([3,6,7,89,90])
vector == 6
1
array([False,  True, False, False, False])
1
matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])matrix == 3
1
2
3
array([[False,  True, False, False],
[False, False, False, False],
[ True, False, False, False]])
1
2
3
4
vector = numpy.array([3,6,7,89,90])
equal_to_six = (vector == 6)
print(equal_to_six)
print(vector[equal_to_six])
1
2
[False  True False False False]
[6]
1
2
3
4
matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])
second_column_three = (matrix[:,1] == 3)
print(second_column_three)
print(matrix[second_column_three,:])
1
2
[ True False False]
[[12 3 67 9]]
1
2
3
4
#逻辑运算
vector = numpy.array([3,6,7,89,90])
equal_to_three_and_six = (vector == 3)&(vector == 6)
print(equal_to_three_and_six)
1
[False False False False False]
1
2
3
vector = numpy.array([3,6,7,89,90])
equal_to_three_or_six = (vector == 3)|(vector == 6)
print(equal_to_three_or_six)
1
[ True  True False False False]
1
#类型转换vector = numpy.array(['3','6','7','89','90'])print(vector.dtype)vector = vector.astype(float)print(vector.dtype)print(vector)
1
2
3
<U2
float64
[ 3. 6. 7. 89. 90.]
1
2
3
#最小值
vector = numpy.array([3,6,7,89,90])
print(vector.min())
1
2
3
#最大值
vector = numpy.array([3,6,7,89,90])
print(vector.max())
1
2
3
#列求和
matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])
print(matrix.sum(axis=0))
1
[49 13 83 24]
1
2
3
#行求和
matrix = numpy.array([[12,3,67,9],[34,2,7,9],[3,8,9,6]])
print(matrix.sum(axis=1))
1
[91 52 26]

1
2
3
4
import numpy as np
print(np.arange(15))
a = np.arange(15).reshape(3,5)
print(a)
1
2
3
4
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
1
#结构a.shape
1
(3, 5)
1
2
#维度
a.ndim
1
2
1
2
3
#数据类型
a.dtype
dtype('int64')
1
2
3
#元素数量
a.size
15
1
2
3
4
5
#初始化元素为0的矩阵
np.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
1
2
3
4
5
6
7
8
9
#初始化元素为1的矩阵,数据类型为int32,结构为(2,3,4)
np.ones((2,3,4),dtype=np.int32)
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],

[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]], dtype=int32)
1
2
3
#生成序列,从10到30,间隔为5
np.arange(10,30,5)
array([10, 15, 20, 25])
1
2
3
#生成序列,从0到2,间隔为0.3
np.arange(0,2,0.3)
array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
1
2
3
4
5
#生成序列,并对序列进行结构操作,当arange()没有制定起始值时,默认从0开始
np.arange(12).reshape(3,4)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
1
2
3
4
5
#生成随机数
np.random.random((3,4))
array([[0.29086774, 0.66853514, 0.80893891, 0.4672383 ],
[0.65005622, 0.433645 , 0.6389985 , 0.00900272],
[0.49442838, 0.66853125, 0.57854652, 0.85672554]])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from numpy import pi
#linspace(),从0到2*pi均匀取100个数
np.linspace(0,2*pi,100)
array([ 0. , 1.01010101, 2.02020202, 3.03030303,
4.04040404, 5.05050505, 6.06060606, 7.07070707,
8.08080808, 9.09090909, 10.1010101 , 11.11111111,
12.12121212, 13.13131313, 14.14141414, 15.15151515,
16.16161616, 17.17171717, 18.18181818, 19.19191919,
20.2020202 , 21.21212121, 22.22222222, 23.23232323,
24.24242424, 25.25252525, 26.26262626, 27.27272727,
28.28282828, 29.29292929, 30.3030303 , 31.31313131,
32.32323232, 33.33333333, 34.34343434, 35.35353535,
36.36363636, 37.37373737, 38.38383838, 39.39393939,
40.4040404 , 41.41414141, 42.42424242, 43.43434343,
44.44444444, 45.45454545, 46.46464646, 47.47474747,
48.48484848, 49.49494949, 50.50505051, 51.51515152,
52.52525253, 53.53535354, 54.54545455, 55.55555556,
56.56565657, 57.57575758, 58.58585859, 59.5959596 ,
60.60606061, 61.61616162, 62.62626263, 63.63636364,
64.64646465, 65.65656566, 66.66666667, 67.67676768,
68.68686869, 69.6969697 , 70.70707071, 71.71717172,
72.72727273, 73.73737374, 74.74747475, 75.75757576,
76.76767677, 77.77777778, 78.78787879, 79.7979798 ,
80.80808081, 81.81818182, 82.82828283, 83.83838384,
84.84848485, 85.85858586, 86.86868687, 87.87878788,
88.88888889, 89.8989899 , 90.90909091, 91.91919192,
92.92929293, 93.93939394, 94.94949495, 95.95959596,
96.96969697, 97.97979798, 98.98989899, 100. ])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#矩阵数学运算
a = np.array([20,30,40,50])
b = np.arange(4)
print(a)
print(b)

[20 30 40 50]
[0 1 2 3]

c = a+b
print(c)
[20 31 42 53]
c = c-1
print(c)
[19 30 41 52]
print(b**2)
[0 1 4 9]
print(a<35)
[ True True False False]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#矩阵乘法
a = np.array((
[1,5],
[0,1]))
b = np.array((
[2,0],
[3,4]))
print(a)
print(b)
print("-------")
#对应位置相乘
print(a*b)
[[1 5]
[0 1]]
[[2 0]
[3 4]]
-------
[[2 0]
[0 4]]
1
2
3
4
5
6
print(a.dot(b))
print(np.dot(a,b))
[[17 20]
[ 3 4]]
[[17 20]
[ 3 4]]

1
2
3
4
5
6
7
import numpy as np
a = np.arange(4)
print(a)
#exp():返回e的n次方,e是一个常数为2.71828
print(np.exp(a))
#sqrt():开方
print(np.sqrt(a))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
[0 1 2 3]
[ 1. 2.71828183 7.3890561 20.08553692]
[0. 1. 1.41421356 1.73205081]
#floor():向下取整,例如3.5 取整后为3
b = np.floor(10*np.random.random((3,4)))
print(b)
print("-----")
#将矩阵拉为向量
print(b.ravel())
print("-----")
#调整结构
b.shape = (6,2)
print(b)
#行和列转换
print("-----")
print(b.T)
[[7. 1. 5. 6.]
[7. 1. 2. 4.]
[1. 5. 7. 4.]]
-----
[7. 1. 5. 6. 7. 1. 2. 4. 1. 5. 7. 4.]
-----
[[7. 1.]
[5. 6.]
[7. 1.]
[2. 4.]
[1. 5.]
[7. 4.]]
-----
[[7. 5. 7. 2. 1. 7.]
[1. 6. 1. 4. 5. 4.]]
#reshape()自动计算列,只要将最后一个数改为-1即可。
b.reshape(3,-1)
array([[7., 1., 5., 6.],
[7., 1., 2., 4.],
[1., 5., 7., 4.]])
#矩阵拼接
import numpy as np
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print("-----")
print(b)
print("-----")
#横向拼接
print(np.hstack((a,b)))
print("-----")
#纵向拼接
print(np.vstack((a,b)))
[[1. 8.]
[7. 6.]]
-----
[[3. 9.]
[7. 5.]]
-----
[[1. 8. 3. 9.]
[7. 6. 7. 5.]]
-----
[[1. 8.]
[7. 6.]
[3. 9.]
[7. 5.]]
#矩阵切分
a = np.floor(10*np.random.random((2,12)))
print(a)
[[4. 3. 0. 7. 6. 7. 7. 8. 9. 1. 2. 7.]
[5. 5. 9. 1. 1. 4. 5. 5. 4. 2. 8. 8.]]
#横向切分
#切成3份
np.hsplit(a,3)
[array([[4., 3., 0., 7.],
[5., 5., 9., 1.]]), array([[6., 7., 7., 8.],
[1., 4., 5., 5.]]), array([[9., 1., 2., 7.],
[4., 2., 8., 8.]])]
#横向指定位置切分
np.hsplit(a,(3,4))
[array([[4., 3., 0.],
[5., 5., 9.]]), array([[7.],
[1.]]), array([[6., 7., 7., 8., 9., 1., 2., 7.],
[1., 4., 5., 5., 4., 2., 8., 8.]])]
#纵向切分
a = np.floor(10*np.random.random((12,2)))
print(a)
print(np.vsplit(a,3))
[[8. 4.]
[6. 1.]
[0. 2.]
[7. 5.]
[7. 6.]
[7. 6.]
[3. 3.]
[8. 0.]
[5. 6.]
[1. 3.]
[1. 2.]
[1. 5.]]
[array([[8., 4.],
[6., 1.],
[0., 2.],
[7., 5.]]), array([[7., 6.],
[7., 6.],
[3., 3.],
[8., 0.]]), array([[5., 6.],
[1., 3.],
[1., 2.],
[1., 5.]])]
#复制的不同操作
#赋值,a、b变量指向了相同的位置,当a和b二者任意出现改变时,数据均会发生变化
a = np.arange(12)
print(a)
b = a
print(a is b)
b.shape = (3,4)
print(a.shape)
print(id(a))
print(id(b))
[ 0 1 2 3 4 5 6 7 8 9 10 11]
True
(3, 4)
4562436864
4562436864
#浅复制
c = a.view()
print(c is a)
c.shape = (2,6)
print(a.shape)
c[1,0] = 123
print(a)
print(id(a))
print(id(b))
False
(3, 4)
[[ 0 1234 2 3]
[1234 5 123 1234]
[ 8 9 10 11]]
4562436864
4562436864
#深复制
a = np.arange(12)
b = a.copy()
a.shape = (3,4)
b.shape = (2,6)
a[0,0] = 123
b[1,0] = 234
print(a)
print(b)
print(id(a))
print(id(b))
[[123 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3 4 5]
[234 7 8 9 10 11]]
4562468656
4562467776