Python 给出坐标的位置参数并将它们聚集在一起

Python 给出坐标的位置参数并将它们聚集在一起,python,list,dictionary,Python,List,Dictionary,所以我有一个数据文件,我想找到两件事: 我给出的坐标是在一个区域内还是在一个区域外,如果它是真的或不是真的,则返回 将“1”的每个坐标放在各自列表中的每一行中。这应该在字典中返回 该文件包含以下内容: 1 1 0 1 0 1 0 0 1 1 1 0 1 1 1 1 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 0 0 0 0 1 我已将上述内容放入一个列表中,每个列表都带有代码: lines = [] with open('area.dat', 'r') as a:

所以我有一个数据文件,我想找到两件事:

  • 我给出的坐标是在一个区域内还是在一个区域外,如果它是真的或不是真的,则返回

  • 将“1”的每个坐标放在各自列表中的每一行中。这应该在字典中返回

  • 该文件包含以下内容:

    1 1 0 1 0 1
    0 0 1 1 1 0
    1 1 1 1 1 0
    1 0 0 0 0 1
    0 0 1 1 0 1
    1 0 0 0 0 1
    
    我已将上述内容放入一个列表中,每个列表都带有代码:

    lines = []
    with open('area.dat', 'r') as a:
        for line in a:
            line = line.strip()
            if not line:
                continue
            lines.append(list(map(int, line.split())))
            data.extend(map(int, line.split()))
    
    
    print(lines)
    
    我试图通过代码获取坐标,以及坐标是在区域外部还是内部(对于x和y)

    区域是列表列表的列表

    x = int(input('enter x: '))
    y = int(input('enter y: '))
    
    
    def cords(x, y, area):
        if x > 6 or y > 6:
            print('Outside Area')
            return(False)
        else:
            print('Inside Area')
            return(True)
    
    我想得到列表“区域”内的坐标x和y,并返回它是在该区域内还是在该区域外

    例如,如果我放入跳线(0,4,面积),它将返回“True”;如果我放入跳线(3,7,面积),它将返回“False”

    在这之后,我想把它们放在一起,每行1个一组

    例如,第1行和第2行将给出:

    {1:[(0,4),(0,5)],2:[(1,0),(1,1)]}


    感谢您的帮助。

    对于第一部分,您有两种选择:

    def cords(x, y):
        return x >= 0 and x < 6 and y >= 0 and y < 6
    
    def cords(x, y, area):
        return x >= 0 and x < len(area) and y >= 0 and y < len(area[0])
    
    d = {}
    for i,row in enumerate(lines):
        n = []
        for j,v in enumerate(row):
            if v == 1:
                n.append([i,j])
        d[i+1] = n