1

Hello, i currently have a list with one element inside each. Is there a way python can combine the first two lists into one? I tried my code down below. The last for loop Is my attempt. If you see the actual output, it only duplicates it but doesnt get the second element. I need the first and second element to be listed together.

input:

['d0123456789']
['49416494949']
['46497616494']
['497522339533388']

current output:

['d0123456789', 'd0123456789']
['49416494949','49416494949']
['46497616494', '46497616494']
['497522339533388','497522339533388']

expected output:

micr_ocr_dat_l =[]
['d0123456789','49416494949']
['46497616494', '497522339533388']

code:

for fp in dat_filepath:
        with open(fp, 'r', encoding="ISO-8859-1") as in_file:
        data = in_file.readlines()
        for row in data:
                micr_ocr_line = re.findall(r'd[^d]*d[^d]*c[0-9]+|d[^d]*d[^d]*c\s+[0-9]+', row)
                for r in micr_ocr_line:
                    rmve_spcl_char = re.sub (r'([^a-zA-Z-0-9]+?)', '', r)
                    rmve_spcl_char = re.sub(r'(c\d{4,}).*', r'\1', rmve_spcl_char)
                    a = [l for l in rmve_spcl_char.split('\n')]
                     for previous, current in zip_longest(a, a[::1]):
                        print(previous, current)
                        micr_ocr_dat = [previous, current]
                        print(micr_ocr_dat)
                        micr_ocr_dat_l.append(micr_ocr_dat)
  • 4
    `zip(a[::2],a[1::2])` is the easy way. – Tim Roberts Feb 14 '22 at 19:01
  • I'm a bit confused about where in your code you're getting the one-element lists. Are they from the `findall`? If so, you might want to restructure the code to search over the whole text, rather than one line at a time, so that `findall` will find things on every line and put them all into a single list, which you can later split up into pairs (after dealing with special characters, or whatever the other regex stuff does). It's hard to pair up your values when you're only dealing with one at a time. – Blckknght Feb 14 '22 at 19:16
  • Welcome to Stack Overflow! Please take the [tour]. I closed your question under an existing one that covers the same sort of problem, though it's a bit more generic (any-sized chunks instead of pairs). BTW, for help in the future, it'd help you a lot to make a [mre] including complete but minimal code and example input. For more tips, see [ask]. – wjandrea Feb 14 '22 at 19:23

0 Answers0