如何允许通过单词而不是数字来访问Python中的索引?

问题描述 投票:0回答:1

我正在用Python编写一个脚本,它可以包含大量“动态”数据,因为它们一次排序一个,我遇到了这个问题。只能通过指定要访问的索引号来访问索引,但问题是数据太多,我不知道它到底在索引中的位置。

我想将索引内部的最后 40 个字符作为索引的名称,这样我就可以通过输入我知道的最后 40 个字符来访问所有索引。到目前为止我的代码是:

   from eth_keys import keys

def generate_key_pair(index):
    # Generate the private key from the index
    private_key = hex(index)[2:].zfill(64)  # Convert index to hexadecimal
    
    # Generate the corresponding wallet address
    private_key_bytes = bytes.fromhex(private_key)
    wallet_address = keys.PrivateKey(private_key_bytes).public_key.to_checksum_address()

    return private_key, wallet_address

def combine_strings(private_key, wallet_address):
    # Combine private key and wallet address into a single string
    combined_string = private_key + wallet_address

    return combined_string

def main():
    index = 1
    private_key, wallet_address = generate_key_pair(index)
    combined_string = combine_strings(private_key, wallet_address)
    print("Combined string:", combined_string)

if __name__ == "__main__":
    main()

 

此代码用于组合我的业务所需的以太坊钱包和私钥,以便允许用户存储他们的数据。会有很多用户,因此我们为他们创建以太坊钱包并将他们的数据存储在其中。有人可以告诉我如何使用每个单独索引内的组合私钥和钱包地址的最后 40 个字符来访问另一个索引吗?

编辑

有人说我可以用字典。有人可以告诉我如何做到这一点吗?我不希望将值存储在 python 中,我希望它们像在索引中一样动态存储,我可以跳转到索引并且数据就在那里。

python python-3.x indexing
1个回答
0
投票

我没有您的图书馆或您可以访问的数据,但我会尝试一下。您可以尝试下面的代码或创建一个字典,两者都应该可以工作。

将此函数添加到您的代码中


def access_index(combined_string_suffix):
    # Access index using the last 40 characters of the combined string
    if combined_string_suffix in key_index_map:
        private_key, wallet_address, index = key_index_map[combined_string_suffix]
        print("Index:", index)
        print("Private Key:", private_key)
        print("Wallet Address:", wallet_address)
    else:
        print("Index not found for the provided combined string suffix.")

def main():
    index = 1
    private_key, wallet_address = generate_key_pair(index)
    combined_string = combine_strings(private_key, wallet_address, index)
    print("Combined string:", combined_string)

    # Access another index using the last 40 characters
    access_index(combined_string[-40:])


© www.soinside.com 2019 - 2024. All rights reserved.