将一个项目留在python列表中并打印路径

时间:2011-12-25 19:52:58

标签: python string

鉴于有大量研究课题(“SUBJ”),我需要创建绝对路径(作为字符串)的块,每次都会留下一个主题。

例如,我需要类似的东西:

/ path / to / data / SUBJ02
/ path / to / data / SUBJ03
/ path / to / data / SUBJ04
/ path / to / data / SUBJ05

/ path / to / data / SUBJ01
/ path / to / data / SUBJ03
/ path / to / data / SUBJ04
/ path / to / data / SUBJ05

等...

假设:

x = ["SUBJ01","SUBJ02","SUBJ03","SUBJ04","SUBJ05"]
loso = ["SUBJ01","SUBJ02","SUBJ03","SUBJ04","SUBJ05"]

def returnLoso(x,loso):
     x1 = [(z) for (z) in x if z !=loso]
     print x1

我的交互式会话的结果是这样的:

In [1]: for i, v in enumerate(loso):
   .....:     returnLoso(x,v)
   .....:     
['SUBJ02', 'SUBJ03', 'SUBJ04', 'SUBJ05']
['SUBJ01', 'SUBJ03', 'SUBJ04', 'SUBJ05']
['SUBJ01', 'SUBJ02', 'SUBJ04', 'SUBJ05']
['SUBJ01', 'SUBJ02', 'SUBJ03', 'SUBJ05']
['SUBJ01', 'SUBJ02', 'SUBJ03', 'SUBJ04']

到目前为止,这么好。

我的问题是,如何将这些插入我的文件路径,以获得如上所示的结果?我需要将数组中的每个“位置”插入一个独立的文本字符串。提前谢谢,

1 个答案:

答案 0 :(得分:2)

怎么样

directory = "c:\\..."
import os.path
paths = [os.path.join(directory, filename) for filename in filenames]

您可以通过

等功能保存主题名称的重复次数
def loo(x):
    return [[el for el in x if el!=x[i]] for i in range(len(x))]

一起更新所有内容:

import os.path

def loo(x):
    return [[el for el in x if el!=x[i]] for i in range(len(x))]

def p(subjects, directory):
    l = loo(subjects)
    for group in l:
        for subj in group:
            print os.path.join(directory, subj)
        print

p(['S1','S2','S3','S4','S5'], 'c:\\')

尝试运行,结果

c:\S2
c:\S3
c:\S4
c:\S5

c:\S1
c:\S3
c:\S4
c:\S5

c:\S1
c:\S2
c:\S4
c:\S5

c:\S1
c:\S2
c:\S3
c:\S5

c:\S1
c:\S2
c:\S3
c:\S4