3

I have a list of numbers like so;

7072624 through 7072631
7072672 through 7072687
7072752 through 7072759
7072768 through 7072783

The below code is what I have so far, i've removed the word "through" and it now prints a list of numbers.

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')

This gives me the following output.

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

So what I'm wanting to do but struggling to find is how to split the 'new_content_array' into two arrays so the output is as follows.

    array1 = [7072624, 7072672, 7072752, 7072768]

    array2 = [7072631, 7072687, 7072759, 7072783]

I then want to be able to take each value in array 2 from the value in array 1

7072631 - 7072624

7072687 - 7072672

7072759 - 7072752

7072783 - 7072768

I've been having a search but can't find anything similar to my situation.

Thanks in advance!

nellie456
  • 57
  • 1
  • 5
  • Does this answer your question? [How to Split Python list every Nth element](https://stackoverflow.com/questions/26945277/how-to-split-python-list-every-nth-element) – Jongware Mar 13 '20 at 10:42
  • .. which for you would be `print (l[::2],l[1::2])` – Jongware Mar 13 '20 at 10:43

3 Answers3

1

Try this below:

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]
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8
0
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))

As a result you will get:

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

I think that as comprehension list, but you could also use filter

rgralma
  • 145
  • 7
0

You could try to split line using through keyword, then removing all non numeric chars such as new line or space using a lambda function and regex inside a list comprehension

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')

Output looks like this:

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

Then you just could extract first element of each nested list to store separately in a variable and so on with second element.

Good luck

P47 R1ck
  • 148
  • 1
  • 12