Home > database >  Is it possible to hard declare a variable in Python?
Is it possible to hard declare a variable in Python?

Time:03-13

I am trying to use a variable inside a substructure. I guess the variable should be of integer data type, and I am trying to add a loop here but it my data type is list since it contains multiple integers.

INV_match_id = [['3749052'],['3749522']]
from statsbombpy import sb
for x in range(2):
   match=INV_match_id[x]
   match_db = sb.events(match_id=match)
   print(match)

I have tried to extract the data one by one using another variable, but still it got declared as list. Whenever I give direct values to "match" it works. for eg: if I add a line match=12546 the substructure takes the value properly.

Next thing I want to try is hard declare "match" variable as integer. Any input is appreciated. I am pretty new to Python.

Edit: Adding this solution from @quamrana here. "So, to answer your original question: Is it possible to hard declare a variable in Python?, the answer is No. Variables in python are just references to objects. Objects can be of whatever type they want to be."

CodePudding user response:

You said: " I want to loop and take the numbers one by one."

Did you mean this:

for match in INV_match_id:
   match_db = sb.events(match_id=match)

I don't know what you want to do with match_db

Update:

"that single number is also declared as a list. like this- ['125364']"

Well if match == ['125364'] then it depends on whether you want: "125364" or 125364. I assume the latter since you talk a lot about integers:

for match in INV_match_id:
   match = int(match[0])
   match_db = sb.events(match_id=match)

Next Update:

So you have: INV_match_id = ['3749052','3749522']

This means that the list is a list of strings, so the code changes to this:

for match in INV_match_id:
   match_db = sb.events(match_id=int(match))

Your original code was making match into a list of the digits of each number. (eg match = [1,2,5,3,6,4])

Reversionary Update:

This time we have: INV_match_id = [['3749052'],['3749522']]

that just means going back to the second version of my code above:

for match in INV_match_id:
   match = int(match[0])
   match_db = sb.events(match_id=match)

CodePudding user response:

It's as simple as:

from statsbombpy import sb
INV_match_id = [['3749052'],['3749522']]
for e in INV_match_id:
   match_db = sb.events(match_id=e[0])
   print(match_db)

You have a list of lists albeit that the sub-lists only contain one item.

match_id can be either a string or int

  • Related