[英]Printing using globals() in Python
我正在嘗試打印每個數據集的形狀,但它沒有按我的意願打印。
for i in train_set, train_labels, test_set, test_labels:
name = [x for x in globals() if globals()[x] is i][1]
print("The {0} shape is {1}".format(name, i.shape))
output 是:
i 形狀是 (1700, 20)
i 形狀是 (1700,)
i 形狀是 (300, 20)
i 形狀是 (300,)
'i' 的值不會被替換。 有人可以指導我這有什么問題:
如果這只是用於調試或狀態信息,那么正確的方法是:
for name,table in (
('train_set', train_set),
('train_labels', train_labels),
('test_set', test_set),
('test_labels', test_labels) ):
print("The {0} shape is {1}".format(name, table.shape)
如果您需要的名稱不止於此,請不要在單個變量中創建它們:
data['train_set'] = train_set
data['train_labels'] = train_labels
data['test_set'] = test_set
data['test_labels'] = test_labels
現在,您可以將名稱和表格放在一個方便的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.