python : how to add number between them, when is digit is TRUE

Viewed 35

I have a problem, I want to add some number between them, but only for some condition, I have a list from web scraping with number and space, the number is a time a soccer match is played by the player, and the space is if the player have score during this match, 1 space = 1 goal, 2 space = 2 goal 3 space : 3 goal.

So when I import in a excel I can make a rule color, with "space" and see really quick witch match he have scored or not.

Now I want to know, since how many min the player have not score, and when it score, the counter of min reset to 0.

So I wrote this :


    for list in lists:
        
        go = list.find('td', class_="tj1").text.replace('\xa0', '').replace("'","")
        goo = [go]
        print(goo)

and the result is :

['90   ']
['90 ']
['90   ']
['90   ']
['90 ']
['68 ']
['27']
['90']
['78 ']

As you can see, there is space in the string, it's the number of score the player have done in this match ( here it's Neymar ), you can see there is two result here :

['27']
['90']

there is no space, so the player have no score in there match, so I have to tell python, to add the number between them and tell me the result as " MAX MIN WITHOUT SCORE "

and the count : " NOT SCORE A GOAL SINCE ? MIN ", if the player score, the count : "NOT SCORE A GOAL SINCE ? MIN " reset to 0.

So I wrote this

for list in lists:
        
        
        go = list.find('td', class_="tj1").text.replace('\xa0', '').replace("'","")
        goo = [go]
        
        print(go)
        x = go.isdigit()
        print(x)

and the result is :

90   
False
90 
False
90   
False
90   
False
90 
False
68 
False
27
True
90
True
78 
False

So now, python can see witch one have space, and which one don't have, so my question, how can I do for add number with the condition "True" and tell me the " MAX MIN WHITOUT SCORING " and reset to zero if in the next match it's score for having " NOT SCORED SINCE ? MIN "

I hope I was clear in my question, thanks for your time, it's very appreciated.

some exemple of what I want :

90   
False
90 
False
90   
False
90   
False
90 
False
68 
False
27
True
90
True
78 
False

Result :

MAX MIN WITHOUT SCORING = 90 + 27 = 117 min

NOT SCORED SINCE ? MIN = 0 min

other exemple :


90   
True
90 
True
90   
True
90   
False
90 
False
68 
False
27
False
90
True
78 
True

Result :

MAX MIN WITHOUT SCORING = 90 + 90 = 90 = 270 min

NOT SCORED SINCE ? MIN = 90 + 78 = 168 min

1 Answers

if appears as though the method for storing data as lists with spaces is suboptimal.

a richer form of storage would be as a dict or a list-of-dicts which would look like this:

d = [
    {"mins":90, "goals":0},
    {"mins":90, "goals":0},
    {"mins":90, "goals":2},
    {"mins":27, "goals":0}
]

in particular, because the dict has richer data, for example, you could add other fields (called keys):

d = [
    {"player name":"ronaldo", "mins":27, "goals":0},
    {"player name":"messi", "mins":27, "goals":5}
]

There other formats too.

The advantage is that you can access the dict like this:

print(d[0])
print(d[0]['player name'])
print(d[0]['mins'])

which would give this:

{'player name': 'ronaldo', 'mins': 27, 'goals': 0}
ronaldo
27

I hope this helps you progress.

Related