Python 中可以硬声明变量吗?
我正在尝试在子结构内使用变量。我想该变量应该是整数数据类型,我试图在此处添加一个循环,但我的数据类型是列表,因为它包含多个整数。
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)
我尝试使用另一个变量逐一提取数据,但它仍然被声明为列表。每当我给出直接值来“匹配”时,它就会起作用。例如:如果我添加一行 match=12546 子结构会正确获取该值。
接下来我想尝试的是将“match”变量硬声明为整数。任何意见都会受到赞赏。我对 Python 还很陌生。
编辑:在此处添加来自 @quamrana 的解决方案。 “所以,要回答你最初的问题:在 Python 中是否可以硬声明一个变量?答案是否定的。Python 中的变量只是对对象的引用。对象可以是它们想要的任何类型。”
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."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说:
“我想循环并一一获取数字。”
你的意思是这样吗:
我不知道你想用
match_db
做什么更新:
“该单个数字也被声明为列表。就像这样 - ['125364']”
那么如果
match == ['125364']
那么这取决于您是否想:“125364”
或125364
。我假设是后者,因为您谈论了很多关于整数的内容:下一次更新:
所以您有:
INV_match_id = ['3749052','3749522']
这意味着列表是字符串列表,因此代码更改为:
您的原始代码是将
match
放入每个数字的数字列表中。 (例如match = [1,2,5,3,6,4]
)版本更新:
这次我们有:
INV_match_id = [['3749052'],['3749522'] ]
这只是意味着回到上面代码的第二个版本:
You said:
" I want to loop and take the numbers one by one."
Did you mean this:
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"
or125364
. I assume the latter since you talk a lot about integers: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:
Your original code was making
match
into a list of the digits of each number. (egmatch = [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:
它很简单:
您有一个列表列表,尽管子列表仅包含一项。
match_id 可以是字符串或整数
It's as simple as:
You have a list of lists albeit that the sub-lists only contain one item.
match_id can be either a string or int