合并两个列表,其中一个列表是从另一个列表创建的子列表

2024-09-23 09:44:07 发布

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

我开始写一点.py来备份我的项目文件,方法是遍历所有的网络驱动器,找到一个根项目文件夹,查看内部是否有设置文件夹,如果有,在站点外备份。你知道吗

我首先创建一个我感兴趣的文件夹列表:

Ant = "I:"
antD = os.listdir(Ant+'\\') 
# antD is the list of all stuff on ANT's D:\
antDpath = (Ant+'\\') 
namingRegex = re.compile( r"\d\d\d\d_\w[^.]*$")
# the above regex returns all folders that begin with four digits
# and a _ but do not contain a . so as not to include files
toCopyAnt = filter(namingRegex.match, antD) 
#List of folders to look in

因此,我有一个很好的文件夹列表,我对这个表格感兴趣:

>>>toCopyAnt
['9001_Project44_IDSF', '5015_Project0_517', '8012_Project_whatever']

下一步我继续把事情搞得一团糟:

inAnt = []
for i in range(len(toCopyAnt)):
    dirAnt = (antDpath+toCopyAnt[i])
    inAnt.append(dirAnt)

上面这部分仅仅是在列表中添加“\”,以便操作系统目录列表下面的目录扫描我得到正确的输出

antList = []
for i in range(len(toCopyAnt)):
    antL = os.listdir(inAnt[i])
    antList.append(antL)

这一部分将浏览我缩小的每个目录,并列出其中的内容。你知道吗

列表antList如下所示:

>>>antList
    [['Data', 'MagicStuff', 'Info', 'settings'], ['Data', 'processing', 'Settings', 'modularTakeOff'], ['Blue_CCheese', 'Data', 'Rubbishbin', 'songBird', 'images', 'Settings', 'Cakes'], ['canDelete', 'Backup']]

它是一个列表的一些列表。。。啊。你知道吗

我的目标是将这两个家伙连接在一起,这样我就有了另一个列表,它给了我每个子目录的完整路径名,就像toCopyAnt[2]和antList[2]一样

['I:\8012_Project_whatever\canDelete','I:\8012_Project_whatever\Backup']

最终的目标是使用这个新的列表,删除我不需要的字符串,然后将其解析为tree\u copy,让我的生活在各个方面都变得更好。你知道吗

我想可能有一个更有效的方法来做这件事,我很想知道,但同时我也想解决这个问题,因为我已经概述了它,因为它将大大提高我的列表产量,我只是在python的东西只有几天了,所以我还没有把所有的工具出箱。你知道吗

如果我在这里所做的是痛苦和劳累,因为我怀疑它是请指出我在正确的方向,我会重新开始。你知道吗

恩吉亚邦加! 英圭

第二天编辑

所以我睡了一觉,又开始攻击逻辑。我仍然认为这是混乱和笨拙的,但我必须坚持下去。你知道吗

下面是我昨晚工作的第一部分,包括一些不同的内容:

inant = []
for i in range(len(toCopyAnt)):
    dirant = (Ant+'\\'+toCopyAnt[i])   ##makes full path of higher folder
    inant.append(dirant)

antList = []
for i in range(len(toCopyAnt)):
    antL = os.listdir(inant[i])   ##provides names of all subfolders
    antList.append(antL)

for i in range(len(antList)):
    antList[i] = filter(settingRegex.match, antList[i])   ##filters out only inpho project folders


## This mess here next builds a list of full path folders to backup. 
preAntpath = []
postAntpath = []
for i in range(len(inant)):
    try:
        preAntpath=("%s\\%s\\"%(inant[i],antList[i][0]))
        postAntpath.append(preAntpath)
        preAntpath=("%s\\%s\\"%(inant[i],antList[i][1]))
        postAntpath.append(preAntpath)
        preAntpath=("%s\\%s\\"%(inant[i],antList[i][2]))
        postAntpath.append(preAntpath)
        preAntpath=("%s\\%s\\"%(inant[i],antList[i][3]))
        postAntpath.append(preAntpath)
    except:
        pass    

好的,所以有4次迭代的原因是为了确保我包含了我想要保留的文件夹的备份副本。有时会有“设置”和“设置-副本”。我两个都要。你知道吗

##
##
## some debuggering next...

for i in range(len(postAntpath)):
    print postAntpath[i]

它起作用了。我从两个列表中得到一个合并数据的新列表。 所以inant[x]antList[x]匹配的地方(因为它们总是相同的长度)antList[x]将与inant[x][0-3]合并。你知道吗

如果像通常情况一样,项目文件夹中没有设置文件夹,try:似乎会处理它。尽管我需要测试一些项目文件夹在列表中是第一个或第二个的情况。在我目前的情况下,这是最后一次。我不知道try:是否尝试每次迭代,或者在遇到第一个错误时停止。。。你知道吗

所以这就是我如何使我的两个列表(一个列表中有一个列表)成为一个可用的列表。你知道吗

下一步是尝试找出如何复制设置文件夹,而不仅仅是它的所有内容。Copytree快把我逼疯了。你知道吗

同样,欢迎提出任何意见或建议。你知道吗

英圭。你知道吗


Tags: of项目in文件夹列表forlenrange