我试图首先在一个值列表中识别一些惟一的值,然后从惟一的值重建原始列表。例如,假设在一个目录中我有以下数据集
a_test_1.txt a_test_2.txt a_test_3.txt b_test_1.txt b_test_2.txt b_test_3.txt
我想唯一地识别a和b。我想我成功了。接下来我想使用这个包含a和b的列表,并返回到原始的文件列表。这是我使用的代码
import os, fnmatch
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
ids_train=[]
#base='/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/'
base = 'attempt/'
for path in find('*.txt',base):
ids_train.append(path)
ids2=[]
for idd in ids_train:
ids2.append(idd.split('test')[0])
ids2=list(set(ids2))
# I will do some operations on ids2 here which I am skipping for simplicity
for idd in ids2:
print(idd)
print("now printing resconstructed")
for idd in ids2:
for data in find(idd.strip(base)+'*',base):
print(data,idd.strip(base)+'*')
attempt/a_
attempt/b_
now printing resconstructed
attempt/b_test_1.txt b_*
attempt/b_test_2.txt b_*
attempt/b_test_3.txt b_*
现在我尝试绝对路径而不是相对路径
import os, fnmatch
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
ids_train=[]
base='/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/'
#base = 'attempt/'
for path in find('*.txt',base):
ids_train.append(path)
ids2=[]
for idd in ids_train:
ids2.append(idd.split('test')[0])
ids2=list(set(ids2))
# I will do some operations on ids2 here which I am skipping for simplicity
for idd in ids2:
print(idd)
print("now printing resconstructed")
for idd in ids2:
for data in find(idd.strip(base)+'*',base):
print(data,idd.strip(base)+'*')
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/b_
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/a_
now printing resconstructed
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/b_test_1.txt b*
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/b_test_2.txt b*
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/b_test_3.txt b*
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/a_test_3.txt *
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/b_test_1.txt *
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/a_test_2.txt *
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/b_test_2.txt *
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/b_test_3.txt *
/data/data_us4/home/plaquestudy/nhm_processing/plaque_CNNSeg/attempt/a_test_1.txt *
在这两种情况下,我都未能重建所需的列表
strip命令被错误地理解为strip从字符串的开头和结尾删除单个字符。需要的是替换。此代码工作正常
输出
相关问题 更多 >
编程相关推荐