我正在嘗試在子結構中使用變數。我猜變數應該是整數資料型別,我想在這里添加一個回圈,但我的資料型別是串列,因為它包含多個整數。
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 ,則子結構會正確獲取該值。
我想嘗試的下一件事是將“匹配”變數宣告為整數。任何輸入表示贊賞。我對 Python 很陌生。
編輯:在此處從@quamrana 添加此解決方案。“所以,回答你最初的問題:是否可以在 Python 中硬宣告變數?答案是否定的。python 中的變數只是對物件的參考。物件可以是它們想要的任何型別。”
uj5u.com熱心網友回復:
你說:" I want to loop and take the numbers one by one."
你是不是這個意思:
for match in INV_match_id:
match_db = sb.events(match_id=match)
我不知道你想做什么match_db
更新:
"that single number is also declared as a list. like this- ['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)
uj5u.com熱心網友回復:
它很簡單:
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 可以是字串或整數
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/442241.html
