【发布时间】:2020-06-25 09:05:30
【问题描述】:
我有一个这样的数字列表;
7072624 through 7072631
7072672 through 7072687
7072752 through 7072759
7072768 through 7072783
下面的代码是我目前所拥有的,我已经删除了“通过”这个词,现在它会打印一个数字列表。
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
我一直在搜索,但找不到与我的情况相似的任何东西。
提前致谢!
【问题讨论】:
-
这能回答你的问题吗? How to Split Python list every Nth element
-
.. 对你来说是
print (l[::2],l[1::2])