Python-os.path.join()函数和numpy.concatenate()函数

Python-os.path.join()函数

说明

拼接文件路径,可以有多个参数。

语法为:

os.path.join(path1, path2, *)

path1 初始路径;path2 需要拼接在其后的路径。初始路径文件夹下的文件或文件夹。可以有多个需要拼接的参数,依次拼接。

如果拼接在后的参数中含有'\'开头的参数,将从'\'开头的参数开始,前面的参数均将失效。并且路径将从对应磁盘的根目录开始。

实例1

(py3.7) C:\Users\HASEE>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> path='D:\dataset'
>>> path
'D:\\dataset'
>>> os.path.join(path,'trainingset')
'D:\\dataset\\trainingset'

 实例2

(py3.7) C:\Users\HASEE>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> DATASET_DIR = '../datasets/cifar-10-batches-py'
>>> DATASET_DIR
'../datasets/cifar-10-batches-py'
>>> os.path.join(DATASET_DIR, 'train.tfrecords')
'../datasets/cifar-10-batches-py\\train.tfrecords'

内容参考自:https://www.py.cn/jishu/jichu/30628.html

numpy.concatenate()函数

numpy提供了numpy.concatenate((a1,a2,...), axis=0)函数。

能够一次完成多个数组的拼接。其中a1,a2,...是数组类型的参数。(与numpy.append()方法相比,效率更高,节约时间)

实例

(py3.7) C:\Users\HASEE>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a=np.array([1, 2, 3])
>>> b=np.array([11, 22, 33])
>>> c=np.array([44, 55, 66])
>>> np.concatenate((a, b, c), axis=0) # 默认情况下,axis=0可以不写
array([ 1,  2,  3, 11, 22, 33, 44, 55, 66]) # 对于一维数组拼接,axis的值不影响最后的结果
>>>
>>> d=np.array([[1, 2, 3],[4, 5, 6]])
>>> e=np.array([[11, 21, 31],[7, 8, 9]])
>>> np.concatenate((d, e), axis=0)
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [11, 21, 31],
       [ 7,  8,  9]])
>>> np.concatenate((d, e), axis=1) # axis=1表示对应行的数组进行拼接
array([[ 1,  2,  3, 11, 21, 31],
       [ 4,  5,  6,  7,  8,  9]])

内容参考自:https://blog.csdn.net/kekeshu_k/article/details/109110916

猜你喜欢

转载自blog.csdn.net/aaaccc444/article/details/129774356
今日推荐