在Python中将数组拆分为两个数组

在Python中将数组拆分为两个数组,python,arrays,Python,Arrays,我有一个这样的数字列表 7072624 through 7072631 7072672 through 7072687 7072752 through 7072759 7072768 through 7072783 下面的代码是我到目前为止的代码,我已经删除了单词“through”,它现在打印了一个数字列表 import os def file_read(fname): content_array = [] with open (fname) as f: f

我有一个这样的数字列表

7072624 through 7072631
7072672 through 7072687
7072752 through 7072759
7072768 through 7072783
下面的代码是我到目前为止的代码,我已经删除了单词“through”,它现在打印了一个数字列表

import os

def file_read(fname):
    content_array = []

    with open (fname) as f:
        for line in f:
            content_array.append(line)
        #print(content_array[33])

        #new_content_array = [word for line in content_array[33:175] for word in line.split()]

        new_content_array = [word for line in content_array[33:37] for word in line.split()]
        while 'through' in new_content_array: new_content_array.remove('through')

        print(new_content_array)

file_read('numbersfile.txt')
这给了我以下输出

    ['7072624', '7072631', '7072672', '7072687', '7072752', '7072759', '7072768', '7072783']
所以我想做但很难找到的是如何将“new_content_array”分成两个数组,输出如下

    array1 = [7072624, 7072672, 7072752, 7072768]

    array2 = [7072631, 7072687, 7072759, 7072783]
然后我希望能够从数组1的值中获取数组2中的每个值

7072631-7072624

7072687-7072672

7072759-7072752

7072783-7072768

我一直在搜索,但找不到与我情况类似的东西

提前谢谢

l = ['7072624', '7072631', '7072672', '7072687', '7072752', '7072759','7072768', '7072783']
l1 = [l[i] for i in range(len(l)) if i % 2 == 0]
l2 = [l[i] for i in range(len(l)) if i % 2 == 1]
print(l1) # ['7072624', '7072672', '7072752', '7072768']
print(l2) # ['7072631', '7072687', '7072759', '7072783']
result = list(zip(l1,l2))
因此,您将获得:

[('7072624','7072631'), ('7072672', '7072687'), ('7072752', '7072759'), ('7072768','7072783')]

我认为这是一个理解列表,但您也可以使用过滤器尝试以下操作:

list_data = ['7072624', '7072631', '7072672', '7072687', '7072752', '7072759', '7072768', '7072783']
    array1 = [int(list_data[i]) for i in range(len(list_data)) if i % 2 == 0]
    array2 = [int(list_data[i]) for i in range(len(list_data)) if i % 2 != 0]

您可以尝试使用through关键字拆分行, 然后在列表中使用lambda函数和regex删除所有非数字字符,例如新行或空格

import os
import re
def file_read(fname):
    new_content_array = []
    with open (fname) as f:
        for line in f:
            line_array = line.split('through')
            new_content_array.append([(lambda x: re.sub(r'[^0-9]', "", x))(element) for element in line_array])

    print(new_content_array)

file_read('numbersfile.txt')
输出如下所示:

[7072624'、[7072631']、[7072672'、[7072687']、[7072752'、[7072759']、[7072768'、[7072783']

然后,您可以提取每个嵌套列表的第一个元素,分别存储在一个变量中,然后使用第二个元素依此类推


祝你好运这能回答你的问题吗。。对于你来说,哪一个是打印(l[::2],l[1::2])