在python中,通过使用set派生的列表进行迭代并不像预期的那样

2024-06-07 17:05:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图首先在一个值列表中识别一些惟一的值,然后从惟一的值重建原始列表。例如,假设在一个目录中我有以下数据集

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的列表,并返回到原始的文件列表。这是我使用的代码

尝试1

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_*

尝试2

现在我尝试绝对路径而不是相对路径

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 *

在这两种情况下,我都未能重建所需的列表


Tags: intesttxthomefordataprocessingidd
1条回答
网友
1楼 · 发布于 2024-06-07 17:05:50

strip命令被错误地理解为strip从字符串的开头和结尾删除单个字符。需要的是替换。此代码工作正常

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.replace(base,'')+'*',base):
                print(data,idd.replace(base,'')+'*')

输出

attempt/a_
attempt/b_
now printing resconstructed
attempt/a_test_3.txt a_*
attempt/a_test_2.txt a_*
attempt/a_test_1.txt a_*
attempt/b_test_1.txt b_*
attempt/b_test_2.txt b_*
attempt/b_test_3.txt b_*

相关问题 更多 >

    热门问题