Python 中可以硬声明变量吗?

发布于 2025-01-14 00:35:29 字数 531 浏览 4 评论 0原文

我正在尝试在子结构内使用变量。我想该变量应该是整数数据类型,我试图在此处添加一个循环,但我的数据类型是列表,因为它包含多个整数。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2025-01-21 00:35:29

你说:“我想循环并一一获取数字。”

你的意思是这样吗:

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

我不知道你想用match_db做什么

更新:

“该单个数字也被声明为列表。就像这样 - ['125364']”

那么如果 match == ['125364'] 那么这取决于您是否想: “125364”125364。我假设是后者,因为您谈论了很多关于整数的内容:

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

下一次更新:

所以您有: INV_match_id = ['3749052','3749522']

这意味着列表是字符串列表,因此代码更改为:

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

您的原始代码是将 match 放入每个数字的数字列表中。 (例如 match = [1,2,5,3,6,4]

版本更新:

这次我们有:INV_match_id = [['3749052'],['3749522'] ]

这只是意味着回到上面代码的第二个版本:

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

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)
謸气贵蔟 2025-01-21 00:35:29

它很简单:

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)

您有一个列表列表,尽管子列表仅包含一项。

match_id 可以是字符串或整数

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文