![](/img/trans.png)
[英]I want to create the list from the datasest and use in another function in palantir foundry but not able to find any solution
[英]I want to use a list from a class in another class, but i am not being able to achieve said goal
所以,我正在做一個項目,我需要在 class 中引用屬於另一個 class 的列表,這是我到目前為止得到的:
import pandas as pd
import numpy as np
data = pd.read_excel(r'D:\Coisas fixes\London.connections.xlsx')
df = pd.DataFrame(data, columns=['station1', 'station2'])
data2 = pd.read_excel(r'D:\Coisas fixes\London.stations.xlsx')
df2 = pd.DataFrame(data2, columns=['id', 'name'])
class testing_tests:
def __init__(self):
self.edge = []
self.vector = []
np_array = df.to_numpy()
for i in np_array:
no1, no2 = i
self.edge.append((no1, no2))
print(self.edge)
class Edge:
def __init__(self):
for i in range(len(testing_tests.edge)):
self.v1 = df.iloc[i, 0]
self.v2 = df.iloc[i, 1]
基本上發生的事情是我創建了一個程序來讀取 excel 文件,我設法做到了,並將一些值保存在新的 class 中,但我沒有想出一種方法來訪問其他 ZA2F2ED4F8EBC2CBBD4C21A2 中的列表在 python 控制台上。
c = testing_tests()
-[(11, 163), (11, 212), (49.........
E = Edge()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:/Users/vasco/Downloads/Ze wrk.py", line 26, in __init__
for i in range(len(testing_tests.edge)):
AttributeError: type object 'testing_tests' has no attribute 'edge'
我放了多個點,因為結果就像 400 個數字。 有誰知道該怎么做? 感謝所有幫助
c = testing_tests()
在這里,您正在創建testing_tests
class 的實例,並將該實例分配給c
。 __init__
將運行初始化 class 實例,設置self.edge
( c.edge
) 列表。
但是在你的Edge
class 里面,你有
for i in range(len(testing_tests.edge)):
在這里,您沒有使用您的實例,而是直接引用testing_tests
class (不是它的實例)。 class(類似於模板)沒有edge
屬性。 只有 class 的實例有這個。
一種解決方案是在 Edge 中創建一個實例,例如
class Edge:
def __init__(self):
self.tt = testing_tests()
for i in range(len(self.tt.edge)):
順便說一句,類和它們的實例之間的這種混淆是一個很好的例子,說明為什么使用命名約定可以使代碼更具可讀性。 如果您將CamelCase
用於您的 class 名稱,則更容易發現此類問題。 例如
class TestingTests:
def __init__(self):
self.edges = []
np_array = df.to_numpy()
for i in np_array:
no1, no2 = i
self.edges.append((no1, no2))
print(self.edges)
class Edge:
def __init__(self):
self.tt = TestingTests()
for i in range(len(self.tt.edges)):
self.v1 = df.iloc[i, 0]
self.v2 = df.iloc[i, 1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.