Python 如何使用数组中文件名的前两个字母标记图像?
我在上神经网络入门课,所以请注意我的无知 我有一个文件夹,包含大约12000个纹理图像,每个图像分为10个不同的子集。示例文件名为bubbly_0012.png。有些文件名以相同的第一个字母开头,例如bubbly_0012.png和blotchy_0012.png 我正在尝试创建一个.csv文件,其中包含每个图像的数组。我想根据每个图像的子集(以及它们的名称)来标记每个图像,这样bubbly被赋予标签0,blotched被赋予标签1,依此类推 我发现我可以使用这一行对每个文件名的第一个字母执行此操作:Python 如何使用数组中文件名的前两个字母标记图像?,python,neural-network,Python,Neural Network,我在上神经网络入门课,所以请注意我的无知 我有一个文件夹,包含大约12000个纹理图像,每个图像分为10个不同的子集。示例文件名为bubbly_0012.png。有些文件名以相同的第一个字母开头,例如bubbly_0012.png和blotchy_0012.png 我正在尝试创建一个.csv文件,其中包含每个图像的数组。我想根据每个图像的子集(以及它们的名称)来标记每个图像,这样bubbly被赋予标签0,blotched被赋予标签1,依此类推 我发现我可以使用这一行对每个文件名的第一个字母执行此
if (file[0]) == "b":
name_array = [[0]]
但是,当我尝试为斑点子集标记以相同字母开头的子集时,这就成为了一个问题,我尝试了以下方法:
if (file[0:1]) == "bl":
name_array = [[0]]
但这不起作用
如果您有任何建议,我们将不胜感激。在切片/范围等方面做得很好。。。第一个索引是包含的,而第二个索引不是。因此
[0:1]
的一个片段将返回第一个索引。要切片包括第二个数字,我们需要使用[0:2]
,它将返回第0和第1个索引。但是,如果我们已经知道关键字
,最好使用其他方法
my_dict_of_files_and_indexes = {"blotchy":1, "bubly":0}
#if we manually fill out the fields then we can just use a list and index it
my_list_of_file_names = ["bubly", "blotchy"]
#for our list where its already in the correct order
if file in my_list_of_file_names:
index_of_file = my_list_of_file_names.index(file)
#do stuff with the file and index
#for our dict where the order of indexes don't matter
if my_dict_of_files_and_indexes.contains(file):
index = my_dict_of_files_and_indexes[file]
#do stuff with the file and index
切片/范围内的井等。。。第一个索引是包含的,而第二个索引不是。因此
[0:1]
的一个片段将返回第一个索引。要切片包括第二个数字,我们需要使用[0:2]
,它将返回第0和第1个索引。但是,如果我们已经知道关键字
,最好使用其他方法
my_dict_of_files_and_indexes = {"blotchy":1, "bubly":0}
#if we manually fill out the fields then we can just use a list and index it
my_list_of_file_names = ["bubly", "blotchy"]
#for our list where its already in the correct order
if file in my_list_of_file_names:
index_of_file = my_list_of_file_names.index(file)
#do stuff with the file and index
#for our dict where the order of indexes don't matter
if my_dict_of_files_and_indexes.contains(file):
index = my_dict_of_files_and_indexes[file]
#do stuff with the file and index
如果您事先知道所有标签,那么使用字符串标签字典来分类将是一个简单的解决方案您想对图像文件名进行分组吗?您想按文件名的第一个字母还是按文件的第一个单词分组?(即bubbly_0001、bubbly_0002和bubbly_0003组合在一起,blotchy_0001、blotchy_0002、blotchy_0003组合在一起)有关切片字符串/列表/等的更多信息,请参阅。如果您事先知道所有标签,则使用字符串标签字典分类将是一个简单的解决方案您想对图像文件名进行分组吗?您想按文件名的第一个字母还是按文件的第一个单词分组?(即bubbly_0001、bubbly_0002和bubbly_0003组合在一起,blotchy_0001、blotchy_0002、blotchy_0003组合在一起)有关切片字符串/列表/等的更多信息,请参阅。