我编写了以下代码来存储数据,检索值并将数据存储到对象中
// The _to and _from fields become account entries
8441142 { Accounts: '0xda7C4aAb9bb74710498485ed01CB4D521f7b4456',
'0x24fa98CA3aB52D8CF562B641AbC4b12861e991Cf'}
// Add accounts that did not previously exist, ignore ones already in here
8441157 { Accounts: 0xda7C4aAb9bb74710498485ed01CB4D521f7b4456',
'0x24fa98CA3aB52D8CF562B641AbC4b12861e991Cf', '0x46340b20830761efd32832A74d7169B29FEB9758','0xeE7D76D5B00A10B49e37a9e26a7678be1EE607F4'}
// Add accounts that did not previously exist, ignore ones already in here
8441167 { Accounts: '0x2baa435Ca4B9dE4a135cAE11f4c04C4a1d334089','0x9fa7f05C9AaE89752fa9273CFAcADF602657599d'}
这很好用,它的输出是:
df = pd.DataFrame({
"col2": ['GE1', 'GE1', 'GE1', 'GE2', 'GE2'],
"col3": [np.nan, '1.2', np.nan, '1.3', np.nan]
})
unique_values = df.groupby("col2").first()
final_df = df.join(unique_values, on="col2", lsuffix="_orig")
我想将其转换为format,其中每个帐户(_to和_from)并作为帐户添加到块上方的索引中,即
--porcelain
我希望能找到一些有关如何有效完成此操作的指示。
答案 0 :(得分:1)
在循环外使用变量来保存上一个块中的帐户列表。复制它,然后将新帐户推送到它。
let last_accounts = [];
for (const [key, {blockNumber, returnValues: {_from, _to}}] of Object.entries(obj)) {
let these_accounts = last_accounts.slice(); // copy accounts from previous block
if (!these_accounts.includes(_from)) {
these_accounts.push(_from);
}
if (!these_accounts.includes(_to)) {
these_accounts.push(_to);
}
accounts[blockNumber] = {Accounts: these_accounts};
last_accounts = these_accounts;
}