字符串在python中是不可变数据类型,那么我们如何替换字符串中指定位置的字符呢?
字符串转换列表替换并转换解决:
1 def replace_char(string,char,index):
2 string = list(string)
3 string[index] = char
4 return ''.join(string)
- Python slice() 函数——将切片内容固定成变量进行传递
- 写python生成二维数组的时候遇到的问题
TypeError: list indices must be integers or slices, not tuple
我的:
A = [[1,2,3,4,5,6],
[7,8,9,10,11,12],
[13,14,15,16,17,18]]
A = np.mat(A)
print(A[0,:]) # [[1 2 3 4 5 6]]
print(A[0,:1]) #[[1]]
print(A[0,:4]) # [[1 2 3 4]]
print(A[0,:5]) # [[1 2 3 4 5]]
A[0,:4] # [[1 2 3 4]] —— 取列表A的第0行的
前4个元素
- 如何用python生成一个二维数组呢?
# 用列表生成m行n列的矩阵
m,n = map(int,input().split())
matrix = [[0]*(m)]*(n)
print(matrix)
# 2 3
# [[0, 0], [0, 0], [0, 0]]
# 这种方式生成的矩阵存在一定的问题,比如,无法给特定位置的元素赋值,例如:
matrix[1][1] = 9
print(matrix)
# [[0, 9], [0, 9], [0, 9]]
# 可见,第二列的元素全部被赋值为9了
# 采用numpy生成想要维度的矩阵
x,y = map(int,input().split())
a = np.ones((x+1,y+1))
print(a)
# [[1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]]
a[1][1] = 9
print(a)
# [[1. 1. 1. 1. 1.]
# [1. 9. 1. 1. 1.]
# [1. 1. 1. 1. 1.]]
x,y = map(int,input().split())
A = np.ones((x+1,y+1))
for i in range(1,x+1):
for j in range(1,y+1):
A[i][j] = A[i-1][j] + A[i][j-1]
print(A)
# [[ 1. 1. 1. 1. 1.]
# [ 1. 2. 3. 4. 5.]
# [ 1. 3. 6. 10. 15.]]
- numpy取指定数据——Python取numpy数组的某几行某几列方法
直接分析,如原矩阵如下(1):
我们要截取的矩阵(取其一三行,和三四列数据构成矩阵)为如下(2):
错误分析:
取 C 的1 3行,3 4 列,定义
则结果为: [[7,14]]
由结果分析取的是第一行第三列和第三行第四列的数据,并非我们想要的结果。
正确分析:
结果为:
其结果即为我们求解的,需要两步才可完成。
- python一个图像复制到另一个文件夹——shutil.copy(src,dst)
- os.path.join()和‘+’用法一样:
os.path.join()函数:连接两个或更多的路径名组件
1.如果各组件名首字母不包含’/’,则函数会自动加上
2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃
3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾
Demo1
import os
Path1 = 'home'
Path2 = 'develop'
Path3 = 'code'
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)
输出
Path10 = homedevelopcode
Path20 = home\develop\code
-------------------------------------------------------------
Demo2
import os
Path1 = '/home'
Path2 = 'develop'
Path3 = 'code'
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print '(Path20 = ',Path20)
输出
Path10 = /homedevelopcode
Path20 = /home\develop\code
--------------------------------------------------------------
Demo3
import os
Path1 = 'home'
Path2 = '/develop'
Path3 = 'code'
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)
输出
Path10 = home/developcode
Path20 = /develop\code
--------------------------------------------------------------
Demo4
import os
Path1 = 'home'
Path2 = 'develop'
Path3 = '/code'
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print '(Path20 = ',Path20 )
输出
Path10 = homedevelop/code
Path20 = /code
- Python如何批量复制图片到另一个文件夹中
-
python 两个文件夹里的文件名对比(比如需要一个xml对应一个jpg时,有时候会不小心少了其中一个文件,这时可以用以下代码比较缺少的是哪个文件)
- Python3 os.path() 模块
- namedtuple 命名元组
#!/usr/bin/python3
import collections
MyTupleClass = collections.namedtuple('MyTupleClass', ['name', 'age', 'job'])
obj = MyTupleClass("Tomsom", 12, 'Cooker')
print(obj.name) # Tomsom
print(obj.age) # 12
print(obj.job)# Cooker
# python字符串有find,find,index, rindex, 都是只能查找一次字符串出现的位置
import re
[i.start() for i in re.finditer('\\\\', 'C:\\Users\\aaa\\computer\\flicker\\01213.jpg')]