Python - 将列表中的一个元素分配给循环中另一个列表中的每个元素

时间:2017-06-30 08:49:24

标签: python

我正在尝试对一些文件进行排序,我首先在dir中使用glob.glob ex查找文件名。 (filename1,filename2,filename3)所以我得到这个文件列表[(Filename1,),(Filename2,),(Filename3,)]

在此之后,我有一个for循环,.readline()从每个文件中提取一个字符串。因此,在每个循环执行完后,filename1打印10个字符串,下一个循环filename2打印3个字符串,下一个循环filename3打印5个字符串。

loop
[string1]
[string2]
[string3]
.....
loop
[string1]
[string2]
[string3]
....
loop
[string1]
[string2]
[string3]
....

问题是我知道10个字符串来自filename1,但我想表明他们确实这样做了。

    How i would like it to look after print 
loop
[filename1 , string1]
[filename1 , string2]
[filename1 , string3]
    .......
new loop   
[filename2 , string1]
[filename2 , string2]
[filename2 , string3]
    .......
new loop
[filename3 , string1]
[filename3 , string2]
[filename3 , string3]

有人可以帮助我或者向我发送正确的方向吗?我搜索“为另一个列表中的元素分配列表”和类似的标签,但无法找到我需要的...帮助表示赞赏谢谢!

修改

    with open (filename) as filesearch:
            filesearch = filesearch.readlines()
# ---------- Loop 1 for filename and filedate-----------------------------------
    for line in filesearch:
            if createdate in line:  # extract "Create Time"
                date = []
                file = []
                file.append(filename[42:])
                #print(file)
                date.append(line[15:])    # store all found filedates in array
                #print(date)
                list_logfile = list(zip(file,date))
                #print(list_logfile)


# ---------- Loop 2 for finding string ------------------------------------------------------
    for line in filesearch:
            if tester in line:  # extract "Sending Request: Tester"
                data = []
                message = []
                start = '-> '   # take string from
                end = ':\ '     # ens string at
                number = line[line.find(start) + 3: line.find(end)]  # [ord('-> '):ord(' :\ ')]
                data.append(number)  # store all found tester in a array
                text = line[line.find(end) + 3:]
                message.append(text)    # store all found comments in a array
                print(data)

    down here i want to append the first file filename1 to the frist loop of strings , 
then append filename2 with the next loop of strings, 
then the next and next...

0 个答案:

没有答案