【问题标题】:Iterate over a list of dictionary with a list of dictionary inside迭代一个字典列表,里面有一个字典列表
【发布时间】:2021-09-22 10:02:12
【问题描述】:

我正在玩一个简单的区块链。我想在我的代码中获取交易的总数,但是这个方法只返回一个值!这是默认值。这是我的示例链。

"chain": [
        {
            "index": 1,
            "previous_hash": "1",
            "proof": 0,
            "timestamp": "2021-07-13 12:46:27.100441",
            "transactions": [
                {
                    "amount": 0,
                    "reciver": "-",
                    "sender": "-"
                }
            ]
        },
        {
            "index": 2,
            "previous_hash": "e267a3f77fe50af86466a9bc5fce5b8285cf3eff85260f402074505c6023a085",
            "proof": 115558,
            "timestamp": "2021-07-13 12:48:29.790718",
            "transactions": [
                {
                    "amount": 420.69,
                    "reciver": "John",
                    "sender": "Hoomehr"
                },
                {
                    "amount": 1142,
                    "reciver": "John",
                    "sender": "yaser"
                },
                {
                    "amount": 100,
                    "reciver": "Miner",
                    "sender": "01656f68288a45f993fc8c35c3d853d1"
                }
            ]
        },
        {
            "index": 3,
            "previous_hash": "217a9bb5994943e03cef59edac125cc33c1831dce9d3ce8424ca134068e808b5",
            "proof": 48245,
            "timestamp": "2021-07-13 12:49:18.885861",
            "transactions": [
                {
                    "amount": 462,
                    "reciver": "Yaser",
                    "sender": "Hoomehr"
                },
                {
                    "amount": 32,
                    "reciver": "John",
                    "sender": "Hoomehr"
                },
                {
                    "amount": 100,
                    "reciver": "Miner",
                    "sender": "01656f68288a45f993fc8c35c3d853d1"
                }
            ]
        }
    ]

这是我的代码:

我试图遍历每一层并做一个for循环,同时我不知道我的语法是错误还是正确。

def total_trans(self):
    balance=0
    for i in self.chain:
        for key , value in self.chain[0].items() :
            for x in self.chain[0]['transactions']:
                for k , v in self.chain[0]['transactions'][0].items():
                    balance += sum(self.chain[0]['transactions'][0]['amount'] for i in self.chain[0]['transactions'][0])

    return balance

【问题讨论】:

    标签: python loops dictionary for-loop iteration


    【解决方案1】:

    是的,您一直引用链中的第一项,而不是让 for 循环遍历数据。

    试试这个:

        def total_trans(self):
            balance = 0.0
            for i in self.chain:
                for x in i['transactions']:
                    balance += x['amount']    
            return balance
    

    使用您的数据,我得到balance 返回为2256.69

    一般来说,当你有这样一个深度嵌套的数据结构时,你应该编写你的程序,让它首先有最外层的循环:在你的例子中是for i in self.chain:。然后你可以添加print(i) 来看看你有什么。然后你会意识到你在transactions 之后,然后你继续前进,迭代其他东西,然后打印,直到你得到你想要的。

    【讨论】:

    • 像时钟一样工作,感谢您的提示...我需要这个
    【解决方案2】:

    我把原始链做成了一个 python 字典。如果您从外部文件导入此 JSON 对象,您可以使用 JSON built in libray 将其反序列化为 Python 对象。

    block_chain = {"chain": [
            {
                "index": 1,
                "previous_hash": "1",
                "proof": 0,
                "timestamp": "2021-07-13 12:46:27.100441",
                "transactions": [
                    {
                        "amount": 0,
                        "reciver": "-",
                        "sender": "-"
                    }
                ]
            },
            {
                "index": 2,
                "previous_hash": "e267a3f77fe50af86466a9bc5fce5b8285cf3eff85260f402074505c6023a085",
                "proof": 115558,
                "timestamp": "2021-07-13 12:48:29.790718",
                "transactions": [
                    {
                        "amount": 420.69,
                        "reciver": "John",
                        "sender": "Hoomehr"
                    },
                    {
                        "amount": 1142,
                        "reciver": "John",
                        "sender": "yaser"
                    },
                    {
                        "amount": 100,
                        "reciver": "Miner",
                        "sender": "01656f68288a45f993fc8c35c3d853d1"
                    }
                ]
            },
            {
                "index": 3,
                "previous_hash": "217a9bb5994943e03cef59edac125cc33c1831dce9d3ce8424ca134068e808b5",
                "proof": 48245,
                "timestamp": "2021-07-13 12:49:18.885861",
                "transactions": [
                    {
                        "amount": 462,
                        "reciver": "Yaser",
                        "sender": "Hoomehr"
                    },
                    {
                        "amount": 32,
                        "reciver": "John",
                        "sender": "Hoomehr"
                    },
                    {
                        "amount": 100,
                        "reciver": "Miner",
                        "sender": "01656f68288a45f993fc8c35c3d853d1"
                    }
                ]
            }
        ]
    }
    

    然后嵌套循环提取每个“金额”,并将其添加到余额变量中,然后返回:

    def total_trans(block_chain):
        balance = 0
        chain = block_chain["chain"]  # get chain (list of blocks)
        for block in chain:  # each element in array (block, type dict)
            for trans in block["transactions"]:  # get transactions key
                balance += trans["amount"]  # add each amaout for each transaction to the balance
        return balance
    
    print(total_trans(block_chain))  # returns : 2256.69
    

    在您的班级中,它看起来像:

    def total_trans(self):
        balance = 0
        chain = self.block_chain["chain"]  # get chain (list of blocks)
        for block in chain:  # each element in array (block, type dict)
            for trans in block["transactions"]:  # get transactions key
                balance += trans["amount"]  # add each amaout for each transaction to the balance
        return balance
    

    【讨论】:

    • 太棒了,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多