parts = ['Is', 'Chicago', 'Not', 'Chicago?']
' '.join(parts)
Out[21]: 'Is Chicago Not Chicago?'
','.join(parts)
Out[22]: 'Is,Chicago,Not,Chicago?'
''.join(parts)
Out[23]: 'IsChicagoNotChicago?'
平时shift+f10 跑代码看的是run窗口,打印中间结果和报错等
可以在Python控制台进行和上面编辑区的程序代码完全无关的互动式试验,尝试某个语法是否可以达到预期的效果等,非常方便,就像和Python的命令行shell互动一样,右边的变量只是控制台的,和编辑区一点关系都没有
这里终端就和cmd.exe一样用,但是安装的是conda则实际上是和anaconda prompt一样的,只是进入的路径不一样
Microsoft Windows [版本 10.0.17134.112]
(c) 2018 Microsoft Corporation。保留所有权利。
(base) C:\Users\Administrator\PycharmProjects\xxxx>conda --v
conda 4.7.11
(base) C:\Users\Administrator\PycharmProjects\xxxx>conda --help
usage: conda-script.py [-h] [-V] command ...
conda is a tool for managing and deploying applications, environments and packages.
Options:
positional arguments:
command
clean Remove unused packages and caches.
config Modify configuration values in .condarc. This is modeled
after the git config command. Writes to the user .condarc
file (C:\Users\Administrator\.condarc) by default.
create Create a new conda environment from a list of specified
packages.
help Displays a list of available conda commands and their help
strings.
info Display information about current conda install.
init Initialize conda for shell interaction. [Experimental]
install Installs a list of packages into a specified conda
environment.
list List linked packages in a conda environment.
package Low-level conda package utility. (EXPERIMENTAL)
remove Remove a list of packages from a specified conda environment.
uninstall Alias for conda remove.
run Run an executable in a conda environment. [Experimental]
search Search for packages and display associated information. The
input is a MatchSpec, a query language for conda packages.
See examples below.
update Updates conda packages to the latest compatible version.
upgrade Alias for conda update.
optional arguments:
-h, --help Show this help message and exit.
-V, --version Show the conda version number and exit.
conda commands available from other packages:
build
convert
debug
develop
env
index
inspect
metapackage
render
server
skeleton
verify
在pycharm里可以完成所有的事情,不需要动用别的小工具,很不错
data_ =
2. 在主程序(不是某个函数)里使用了return语句,我从一个函数里复制过来这部分代码忘记删除return语句了
isExists = os.path.exists(labels_file_to_save)
if not isExists:
# 如果不存在则创建
pickle.dump(labels, open(labels_file_to_save, 'wb'))
print( ' labels.dat created successfully')
return true
else:
print(' labels.dat already exists')
return false
a=np.arange(1,10) # 不包括10
print(a)
[1 2 3 4 5 6 7 8 9]
print(a[5:]) # 从第六个数开始输出,包括最后一个数
[6 7 8 9]
print(a[:5]) # 包括第一个数,包括第5个数
[1 2 3 4 5]
print(a[3:8]) # 不包括第3个数,包括第8个数
[4 5 6 7 8]
print(a[:]) # 相当于print(a)
[1 2 3 4 5 6 7 8 9]
b=np.mat(np.arange(20).reshape(4,5))
print(b)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
print(b[1:3,2:5]) # 和上面的规则一样,不包括第一行第二列,包括第三行第五列
[[ 7 8 9]
[12 13 14]]
print(b[:3,2:5])
[[ 2 3 4]
[ 7 8 9]
[12 13 14]]
print(b[:3,4])
[[ 4]
[ 9]
[14]]
print(b[:,4:])
[[ 4]
[ 9]
[14]
[19]]
print(b[:,3:])
[[ 3 4]
[ 8 9]
[13 14]
[18 19]]
c=np.arange(60).reshape(3,4,5) # 3页4行5列 不是3行4列5页!!
print(c)
[[[ 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]]]
print(c.shape[0]) # 页数
3
print(c.shape[1]) # 行数
4
print(c.shape[2]) # 列数
5
print(c[:,:,4]) # 所有页所有行的第4列(列数行数页数从0开始),每列组成新矩阵的一行
[[ 4 9 14 19]
[24 29 34 39]
[44 49 54 59]]
print(c[:2,2:4,1:4]) # 0,1页;3,4两行(由于按0开始则只有0-3行,这里输入的4大于3,所以默认按照1-4行索引(藏得很深!!));2,3,4列(按0开始索引的)
[[[11 12 13]
[16 17 18]]
[[31 32 33]
[36 37 38]]]
即:Python默认从0开始索引任何东西,但是切片式索引的尾部索引值可以是它的索引范围加1,表示最后一个数
print(a)
[1 2 3 4 5 6 7 8 9]
print(a[0])
1
print(a[8])
9
print(a[9]) # 报错索引超出范围
Traceback (most recent call last):
File "D:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3325, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "" , line 1, in <module>
print(a[9])
IndexError: index 9 is out of bounds for axis 0 with size 9
print(a[5:8])
[6 7 8]
print(a[5:9]) # 切片式索引才可以出现9
[6 7 8 9]
切片式索引的尾部索引值是从1开始的,比如下面这行代码,尾部索引值8就表示第八个数(1,2,,,8),不表示第九个数(0,1,2····8)
print(a[:8])
[1 2 3 4 5 6 7 8]
这和MATLAB不一样
>> a=[1 3 5 2;5 6 8 9]
a =
1 3 5 2
5 6 8 9
>> a(:,:,2)=[5 4 3 6;7 9 3 5]
a(:,:,1) =
1 3 5 2
5 6 8 9
a(:,:,2) =
5 4 3 6
7 9 3 5
>> a(:,:,3)=[3 2 5 7;9 0 3 5]
a(:,:,1) =
1 3 5 2
5 6 8 9
a(:,:,2) =
5 4 3 6
7 9 3 5
a(:,:,3) =
3 2 5 7
9 0 3 5
>> size(a)
ans =
2 4 3
>>
MATLAB还可以这样拼接得到三维矩阵
>> a=cat(3,[2 3],[4 7],[5 8])
a(:,:,1) =
2 3
a(:,:,2) =
4 7
a(:,:,3) =
5 8
>> size(a)
ans =
1 2 3
可见MATLAB代码的行列数永远是放在前面的,最外层放在后面
而Python是把行列放在最后面,最外层放在前面
这也并不影响什么,只是我们处理矩阵时要注意矩阵的结构
d=np.arange(240).reshape(2,5,6,4) # 最外层维数是2,先叫张数吧;次之的维度是5,页数;6行4列
print(d)
[[[[ 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 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]
[156 157 158 159]
[160 161 162 163]
[164 165 166 167]]
[[168 169 170 171]
[172 173 174 175]
[176 177 178 179]
[180 181 182 183]
[184 185 186 187]
[188 189 190 191]]
[[192 193 194 195]
[196 197 198 199]
[200 201 202 203]
[204 205 206 207]
[208 209 210 211]
[212 213 214 215]]
[[216 217 218 219]
[220 221 222 223]
[224 225 226 227]
[228 229 230 231]
[232 233 234 235]
[236 237 238 239]]]]
print(d[:2,1:3,2:5,1:3])
[[[[ 33 34]
[ 37 38]
[ 41 42]]
[[ 57 58]
[ 61 62]
[ 65 66]]]
[[[153 154]
[157 158]
[161 162]]
[[177 178]
[181 182]
[185 186]]]]
print(d[:,:,:,3])
[[[ 3 7 11 15 19 23]
[ 27 31 35 39 43 47]
[ 51 55 59 63 67 71]
[ 75 79 83 87 91 95]
[ 99 103 107 111 115 119]]
[[123 127 131 135 139 143]
[147 151 155 159 163 167]
[171 175 179 183 187 191]
[195 199 203 207 211 215]
[219 223 227 231 235 239]]]
print('=' * 80)