How to split and combine embedded lines using less code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to split and combine embedded lines using less code (/thread-29009.html) |
How to split and combine embedded lines using less code - pjfarley3 - Aug-13-2020 Below is a test program to illustrate what I want to achieve. It is done using several for loops with intermediate variables. Is there a way to combine these operations into one or two lines using list comprehension in some way? I have not been able to work out how to do this, so I would appreciate any advice you can provide. Peter # Input lists l1 = ["abc\ndef\nghi\n", "jkl\nmno\npqr\n"] l2 = ["123\n456\n789\n", "987\n654\n321\n"] # Desired result list lwant = ["abc|123\ndef|456\nghi|789\n", "jkl|987\nmno|654\npqr|321\n"] print("from:\n", l1, "\n", l2, "\nwant:\n", lwant, "\n") l1s = [] l2s = [] for ln in range(len(l1)): l1s.append([]) l1s[ln] = l1[ln].splitlines() l2s.append([]) l2s[ln] = l2[ln].splitlines() print("after splitlines:\n", l1s, "len(l1s)=%d,len(l1s[0])=%d\n" % (len(l1s), len(l1s[0])), l2s, "len(l2s)=%d,len(l2s[0])=%d\n" % (len(l2s), len(l2s[0]))) lcomb = [] for ln in range(len(l1)): lcomb.append([]) for sn in range(len(l1s[0])): lcomb[ln].append([]) lcomb[ln][sn] = l1s[ln][sn] + "|" + l2s[ln][sn] + "\n" print("after combining:\n%s" % lcomb, "\n") ljoin = [] for ln in range(len(lcomb)): ljoin.append("") ljoin[ln] = "".join(lcomb[ln]) print("after join:\n", ljoin)
RE: How to split and combine embedded lines using less code - buran - Aug-13-2020 spam = ["abc\ndef\nghi\n", "jkl\nmno\npqr\n"] eggs = ["123\n456\n789\n", "987\n654\n321\n"] result = [] for chars, nums in zip(spam, eggs): element = '\n'.join('|'.join(item) for item in zip(chars.strip().split('\n'), nums.strip().split('\n'))) result.append(f'{element}\n') print(result) of course line 6 is ugly and probably should be split in multiple lines, e.g. lines 5-7 above will becomefor chars, nums in zip(spam, eggs): chars = chars.strip().split('\n') nums = nums.strip().split('\n') element = '\n'.join('|'.join(item) for item in zip(chars, nums)) result.append(f'{element}\n') RE: How to split and combine embedded lines using less code - pjfarley3 - Aug-13-2020 Thanks much for the prompt reply. One question: Why strip().split("\n") instead of just splitlines()? Peter RE: How to split and combine embedded lines using less code - buran - Aug-13-2020 (Aug-13-2020, 08:14 PM)pjfarley3 Wrote: Why strip().split("\n") instead of just splitlines()?no reason - it's perfectly fine to use splitlines() insteadit was easier to do the steps one by one - strip the trailining new line, split at '\n' spam = ["abc\ndef\nghi\n", "jkl\nmno\npqr\n"] eggs = ["123\n456\n789\n", "987\n654\n321\n"] result = [] for chars, nums in zip(spam, eggs): element = '\n'.join('|'.join(item) for item in zip(chars.splitlines(), nums.splitlines())) result.append(f'{element}\n') print(result) RE: How to split and combine embedded lines using less code - pjfarley3 - Aug-13-2020 Thanks again, that at least shows me I understand the process. Is it possible to solve this in python V2.7? As I understand it f-strings are V3.6+ specific, correct? Peter RE: How to split and combine embedded lines using less code - buran - Aug-13-2020 f-strings are 3.6+ earlier versions, incl. 2.7 result.append('{}\n'.format(element))but you shouldn't be using 2.7/ python2 support has ended RE: How to split and combine embedded lines using less code - pjfarley3 - Aug-13-2020 Thank you again for your prompt and precise answers. I will mark this one solved. Peter |