Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to parse through a list of strings and return joined strings based on slices of these strings?

Tags:

python

Here is an example list and the desired output:

list = ['1 Michael Jessica', '2 Christopher Ashley', '3 Matthew Brittany', '4 Joshua Amanda']
output = [ 'Michael 1', 'Jessica 1', 'Christopher 2', 'Ashley 2', 'Matthew 3', 'Brittany 3', etc]

# Then I sort it but that doesn't matter right now

I'm a python newbie and combined the concepts I understand to yield this horrendously ridiculous code that I'm almost embarrassed to post. No doubt there is a proper and easier way! I'd love some advice and help. Please don't worry about my code or editing it. Just posting it for reference if it helps. Ideally, brand new code is what I'm looking for.


list = ['1 Michael Jessica', '2 Christopher Ashley', '3 Matthew Brittany', '4 Joshua Amanda']
list3 = []
list4 = []
y = []

for n in list:
    x = n.split()
    y.append(x)

print(y)

for str in y:
    for pos in range(0, 3, 2):  # Number and Name 1
        test = str[pos]
        list3.append(test)


for str in y:
    for pos in range(0, 2):  # Number and Name 2
        test = str[pos]
        list4.append(test)

list3.reverse()
list4.reverse()

print(list3)
print(list4)

length = int(len(list3) / 2)
start = 0
finish = 2

length2 = int(len(list4) / 2)
start2 = 0
finish2 = 2

for num in range(0, length):
    list3[start:finish] = [" ".join(list3[start:finish])]
    start += 1
    finish += 1

for num in range(0, length):
    list4[start2:finish2] = [" ".join(list4[start2:finish2])]
    start2 += 1
    finish2 += 1

print(list3)
print(list4)

list5 = list3 + list4
list5.sort()
print(list5)

like image 968
Chris E Avatar asked Jun 11 '20 06:06

Chris E


Video Answer


2 Answers

Other answers are also looks good, I believe this would be the much dynamic way if there is any displacement in numbers. So re will be the good choice to slice and play.

import re
ls = ['1 Michael Jessica', '2 Christopher Ashley', '3 Matthew Brittany', '4 Joshua Amanda']
result = []
for l in ls:
    key = re.findall('\d+',l)[0]

    for i in re.findall('\D+',l):
        for val in i.split():
            result.append('{} {}'.format(val, key))

print(result)

Below is the one liner for the same:

result2 = ['{} {}'.format(val, re.findall('\d+',l)[0]) for l in ls for i in re.findall('\D+',l) for val in i.split()]
print(result2)

Happy Coding !!!

like image 81
Vanarajan Natarajan Avatar answered Oct 18 '22 21:10

Vanarajan Natarajan


This is one approach using a simple iteration and str.split

Ex:

lst = ['1 Michael Jessica', '2 Christopher Ashley', '3 Matthew Brittany', '4 Joshua Amanda']
result = []
for i in lst:
    key, *values = i.split()
    for n in values:
        result.append(f"{n} {key}")   #or result.append(n + " " + key)
print(result)

Output:

['Michael 1', 'Jessica 1', 'Christopher 2', 'Ashley 2', 'Matthew 3', 'Brittany 3', 'Joshua 4', 'Amanda 4']
like image 29
Rakesh Avatar answered Oct 18 '22 21:10

Rakesh