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

时间:2017-04-05 15:19:08

标签: python python-2.7 list nested-lists

正确的说法是,几周的编程将为您节省数小时的工作量我开始编写一些.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)

上面这部分只是为列表添加'\',以便当下面的os.dirlist扫描目录时我得到正确的输出

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_copy,以便在各方面让我的生活更美好。

我认为可能有一种更有效的方法来做到这一点,我很想知道,但与此同时我也想解决这个问题,因为我已经概述了它,因为它会大大增加我的列表产量,我现在只在这个python的东西上呆了几天,所以我还没有完成所有工具。

如果我在这里做的事情和我怀疑的一样痛苦和痛苦,请指出我正确的方向,我会重新开始。

Ngiyabonga! Ingwe

########### NEXT DAY EDIT ###########

所以我睡着了,一直在攻击逻辑。仍然相信这是混乱和笨拙但我必须继续战斗。

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

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让我发疯。

同样,欢迎提出任何意见或建议。

Ingwe。

0 个答案:

没有答案