如何在python3中的分隔标记处合并两个列表

发布于 2024-10-26 14:51:37 字数 2235 浏览 0 评论 0原文

我是阿拉巴马大学的计算机科学专业学生,我们的 python 课上有一个项目,但我陷入了困境......可能是出于某种愚蠢的原因,但我似乎找不到答案。

这是该项目的链接,因为在这里尝试和解释会很痛苦。

http://beastie.cs.ua.edu/cs150/projects/project1.html

这是我的代码:

import sys
from scanner import scan

def clInput():
    #Gets command line input

    log1 = sys.argv[1]
    log2 = sys.argv[2]
    name = sys.argv[3]

    if len(sys.argv) != 4:
        print('Incorrect number of arguments, should be 3')
        sys.exit(1)
    return log1,log2,name

def openFiles(log1,log2):
    #Opens sys.argv[1]&[2] for reading

    f1 = open(log1, 'r')
    f2 = open(log2, 'r')
    return f1, f2


def merge(log1,log2):
    #Merges parsed logs into list without '---'

    log1Parse = [[]]
    log2Parse = [[]]
    log1Count = 0
    log2Count = 0
    for i in log1:
        if i != ['---']:
            log1Parse[log1Count].append(i)
        else:
            log1Count += 1
            log1Parse.append([])

    for i in log2:
        if i != ['---']:
            log2Parse[log2Count].append(i)
        else:
            log2Count += 1
            log2Parse.append([])

return(log1Parse[0] + log2Parse[0] + log1Parse[1] + log2Parse[1])


def searchMerge(name,merged):
    #Searches Merged list for sys.argv[3]

    for i in range(len(merged)):
        if (merged[i][1] == name):
            print(merged[i][0],merged[i][1]," ".join(merged[i][2:]))



def main():
    log1,log2,name = clInput()
    f1,f2 = openFiles(log1,log2)

    #Sets the contents of the two scanned files to variables
    tokens1 = scan(f1)
    tokens2 = scan(f2)

    #Call to merge and search
    merged = merge(tokens1,tokens2)
    searchMerge(name,merged)




main()

好的。所以这就是问题所在。我们要将两个列表合并到一个排序的主列表中,以 --- 分隔,

我的两个日志文件与我上面链接的网站上发布的日志文件相匹配。该代码可以工作,但是如果每个列表中有两个以上的 --- 实例,它将不会跳转到下一个列表以获取其他标记,依此类推。我用合并功能让它可以为两个人工作。在该函数结束时我返回

return(log1Parse[0] + log2Parse[0] + log1Parse[1] + log2Parse[1])

,但这仅适用于 --- 的两个实例。无论如何,我是否可以更改返回以查看所有索引,而不必手动输入 [0]、[1]、[2] 等?我需要它来分隔和合并任意数量。请帮忙!!

ps 忽略新手...我是一个新手,我们都必须从某个地方开始

p.ps - from Scanner import scan 是我编写的一个扫描仪,用于接收给定列表中的所有标记

I am a CS major at the University of Alabama, we have a project in our python class and I am stuck...probably for some stupid reason, but I cant seem to find the answer.

here is the link to the project, as it would be a pain to try and explain on here.

http://beastie.cs.ua.edu/cs150/projects/project1.html

here is my code:

import sys
from scanner import scan

def clInput():
    #Gets command line input

    log1 = sys.argv[1]
    log2 = sys.argv[2]
    name = sys.argv[3]

    if len(sys.argv) != 4:
        print('Incorrect number of arguments, should be 3')
        sys.exit(1)
    return log1,log2,name

def openFiles(log1,log2):
    #Opens sys.argv[1]&[2] for reading

    f1 = open(log1, 'r')
    f2 = open(log2, 'r')
    return f1, f2


def merge(log1,log2):
    #Merges parsed logs into list without '---'

    log1Parse = [[]]
    log2Parse = [[]]
    log1Count = 0
    log2Count = 0
    for i in log1:
        if i != ['---']:
            log1Parse[log1Count].append(i)
        else:
            log1Count += 1
            log1Parse.append([])

    for i in log2:
        if i != ['---']:
            log2Parse[log2Count].append(i)
        else:
            log2Count += 1
            log2Parse.append([])

return(log1Parse[0] + log2Parse[0] + log1Parse[1] + log2Parse[1])


def searchMerge(name,merged):
    #Searches Merged list for sys.argv[3]

    for i in range(len(merged)):
        if (merged[i][1] == name):
            print(merged[i][0],merged[i][1]," ".join(merged[i][2:]))



def main():
    log1,log2,name = clInput()
    f1,f2 = openFiles(log1,log2)

    #Sets the contents of the two scanned files to variables
    tokens1 = scan(f1)
    tokens2 = scan(f2)

    #Call to merge and search
    merged = merge(tokens1,tokens2)
    searchMerge(name,merged)




main()

ok. so heres the problem. We are to merge two lists together into a sorted master list, delimited at the ---'s

my two log files match the ones posted on the website i linked to above. This code works, however if there are more than two instances of the ---'s in each list, it will not jump to the next list to get the other tokens, and so forth. I have it working for two with the merge function. at the end of that function i return

return(log1Parse[0] + log2Parse[0] + log1Parse[1] + log2Parse[1])

but this only works for two instances of ---. Is there anyway i can change my return to look at all of the indexes instead of having to manually put in [0],[1],[2], etc.? I need it to delimit and merge for an arbitrary amount. Please help!!

p.s. disregard the noobness...im a novice, we all gotta start somewhere

p.p.s. - the from scanner import scan is a scanner i wrote to take in all of the tokens in a given list

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫人生路 2024-11-02 14:51:37

so.py:

import sys

def main():
    # check and load command line arguments
    # your code
    if len(sys.argv) != 4:
        print('Incorrect number of arguments, should be 3')
        sys.exit(1)

    # open files using file io
    # your code
    f1 = open(log1, 'r')
    f2 = open(log2, 'r')

    # list comprehension to process and filter log files
    l1 = [ x.strip().split(" ",2) for x in f1.readlines() if x.strip() != "---" ]
    l2 = [ x.strip().split(" ",2) for x in f2.readlines() if x.strip() != "---" ]

    f1.close()
    f2.close()

    sorted_merged_lists = sorted(l1 + l2)

    results = [ x for x in sorted_merged_lists if x[1] == name ]

    for result in results:
        print result

main()

CLI:

$ python so.py log1.txt log2.txt Matt
['12:06:12', 'Matt', 'Logged In']
['13:30:07', 'Matt', 'Opened Terminal']
['15:02:00', 'Matt', 'Opened Evolution']
['15:31:16', 'Matt', 'Logged Out']

文档:

http://docs.python.org/ release/3.0.1/tutorial/datastructs.html#list-compressives
http://docs.python.org /release/3.0.1/library/stdtypes.html?highlight=strip#str.strip
http://docs.python.org /release/3.0.1/library/stdtypes.html?highlight=split#str.split
http://docs.python.org/release /3.0.1/library/functions.html?highlight=sorted#sorted

so.py:

import sys

def main():
    # check and load command line arguments
    # your code
    if len(sys.argv) != 4:
        print('Incorrect number of arguments, should be 3')
        sys.exit(1)

    # open files using file io
    # your code
    f1 = open(log1, 'r')
    f2 = open(log2, 'r')

    # list comprehension to process and filter log files
    l1 = [ x.strip().split(" ",2) for x in f1.readlines() if x.strip() != "---" ]
    l2 = [ x.strip().split(" ",2) for x in f2.readlines() if x.strip() != "---" ]

    f1.close()
    f2.close()

    sorted_merged_lists = sorted(l1 + l2)

    results = [ x for x in sorted_merged_lists if x[1] == name ]

    for result in results:
        print result

main()

CLI:

$ python so.py log1.txt log2.txt Matt
['12:06:12', 'Matt', 'Logged In']
['13:30:07', 'Matt', 'Opened Terminal']
['15:02:00', 'Matt', 'Opened Evolution']
['15:31:16', 'Matt', 'Logged Out']

docs:

http://docs.python.org/release/3.0.1/tutorial/datastructures.html#list-comprehensions
http://docs.python.org/release/3.0.1/library/stdtypes.html?highlight=strip#str.strip
http://docs.python.org/release/3.0.1/library/stdtypes.html?highlight=split#str.split
http://docs.python.org/release/3.0.1/library/functions.html?highlight=sorted#sorted

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文