This is what I am trying to do: part 1: I look for names and ages in a text using regex and putting them is separate lists. part 2: Then I want to create a 3D list that would store name and age pairs.
Part one works out fine. Then I try to do part two and the problem starts.
(In this example) My names list look like this: ['Peter', 'Barbara', 'John'] My ages look like this: ['11', '7', '103']
Python 3.11 code:
import re
def f1(t):
#part1
names = re.findall("\[A-Z\]+\[a-z\]\*", t)
ages = re.findall("\[0-9\]+", t)
#part2
new_array = [["", ""]]*len(names)
for i in range(len(new_array)):
new_array[i][0] = names[i]
new_array[i][1] = ages[i]
return new_array
print(f1("Peter is 11, Barbara is 7 and their grandfather John is 103 !"))
What I am expecting as output: [['Peter', '11'], ['Barbara', '7'], ['Jhon', '103']]
What I get as output: [['John', '103'], ['John', '103'], ['John', '103']]