manavhs13
manavhs13

Reputation: 51

Split element of list twice in python consisting of multiple numbers as strings

I've got the following data stored in an 2D array(lists of lists).

my_array = ['Tigers', '2021', 'Round', 'Turf', 'Opponent', 'Quarter scores', 'Totalfor', 'Quarter opponent score', 'TotalOpp', 'Result', 'Margin', 'Record', 'Venue', 'Crowd', 'Date']
, ['Tigers', '2021', 'R1', 'H', 'Lions', '4.4 11.7 6.9 13.11', '103', '2.3 5.5 10.8 13.13', '91', 'W', '12', '1-0-0', 'Athenian', '26985', 'Sat 20-Mar-2021 4:05 PM'] 
, ['Tigers', '2021', 'R2', 'A', 'Grizzlies', '3.1 4.7 5.14 10.12', '88', '4.1 9.6 15.11 18.13', '121', 'L', '-33', '1-0-1', 'Soccer stadium', '23946', 'Sat 27-Mar-2021 1:45 PM']

However, I want to get the Quarter scores which is the 5th element and quarter opponent scores which is the 7th element and split them twice. For example: ['4.4 11.7 6.9 13.11'] and specifically split and get the last quarter scores. From this I want to get 13 and 11 and convert them to integers so I can after use them validate the totals.

Now I'm trying to do things like splitting the space first and then the split on the '.' . I was thinking of a for loop but I keep getting errors

for i in my_array:
my_array[5].split(' ')
my_array[5].split('.')

Any help?

Upvotes: 1

Views: 140

Answers (2)

Ayush Biswas
Ayush Biswas

Reputation: 86

You can store them in a variable and split again to obtain the individual results.

my_array = [['Tigers', '2021', 'Round', 'Turf', 'Opponent', 'Quarter scores', 'Totalfor', 'Quarter opponent score', 'TotalOpp', 'Result', 'Margin', 'Record', 'Venue', 'Crowd', 'Date']
, ['Tigers', '2021', 'R1', 'H', 'Lions', '4.4 11.7 6.9 13.11', '103', '2.3 5.5 10.8 13.13', '91', 'W', '12', '1-0-0', 'Athenian', '26985', 'Sat 20-Mar-2021 4:05 PM'] 
, ['Tigers', '2021', 'R2', 'A', 'Grizzlies', '3.1 4.7 5.14 10.12', '88', '4.1 9.6 15.11 18.13', '121', 'L', '-33', '1-0-1', 'Soccer stadium', '23946', 'Sat 27-Mar-2021 1:45 PM']]

for sets in my_array[1:]:
    scores = sets[5].split(' ')
    final_scores = scores[-1].split('.')
    print(final_scores)

Upvotes: 0

Henrik Bo
Henrik Bo

Reputation: 433

Create the my_array:

my_array = [
    [
        "Tigers",
        "2021",
        "Round",
        "Turf",
        "Opponent",
        "Quarter scores",
        "Totalfor",
        "Quarter opponent score",
        "TotalOpp",
        "Result",
        "Margin",
        "Record",
        "Venue",
        "Crowd",
        "Date",
    ],
    [
        "Tigers",
        "2021",
        "R1",
        "H",
        "Lions",
        "4.4 11.7 6.9 13.11",
        "103",
        "2.3 5.5 10.8 13.13",
        "91",
        "W",
        "12",
        "1-0-0",
        "Athenian",
        "26985",
        "Sat 20-Mar-2021 4:05 PM",
    ],
    [
        "Tigers",
        "2021",
        "R2",
        "A",
        "Grizzlies",
        "3.1 4.7 5.14 10.12",
        "88",
        "4.1 9.6 15.11 18.13",
        "121",
        "L",
        "-33",
        "1-0-1",
        "Soccer stadium",
        "23946",
        "Sat 27-Mar-2021 1:45 PM",
    ],
]

The first item [0] is the headers so I skip them.

This loop get the desired result. I take the 5th and 7th element of the list and then split by space and return the last item:

for i in my_array[1:]:
    print(i[5].split(" ")[-1], i[7].split(" ")[-1])

Output:

13.11 13.13
10.12 18.13

Upvotes: 1

Related Questions