使用相同的字符串但附加不同的值在 python 中创建列表

Creating a list in python using the same string but with different values attached to it

在我问我的问题之前,我只想让你们知道我是 Python 的新手,所以请多多包涵! :)

无论如何这是我的问题:

我正在编写一些有趣的代码,它调用 API 并获取最新的比特币 Nonce 数据。我希望能够在我的代码本身中保存过去的几个 nonce 值。

有人建议我使用列表来执行此操作。我已经这样做了,但是每次出现新的 nonce 值时,它只是替换列表中的原始值,而不是添加新值。

有没有人知道我做错了什么或者是否有更好的选择?

非常感谢! :D

from __future__ import print_function
import blocktrail, time

def code():

    client = blocktrail.APIClient(api_key="x", api_secret="x", network="BTC", testnet=False)
    address = client.address('x')

    latest_block = client.block_latest()

    nonce = latest_block['nonce']

    blockhash = latest_block['hash']

    print(nonce)

    noncestr = str(nonce)

    noncelist = []
    noncelist.append(noncestr);
    print(noncelist)
    time.sleep(60)

while True:
    code()

您在函数中每次都将 noncelist 设置为一个空列表,因此显然您只在其中保留一个值,您将追加到下一行,您需要在函数外部声明该列表,并且将列表作为参数传递,使用 class 可能是使列表成为属性的更好方法。

import blocktrail, time
class Bit():
    def __init__(self):
        self.nonce_list = []
    def run(self):
        while True:
            client = blocktrail.APIClient(api_key="x", api_secret="x", network="BTC", testnet=False)
            address = client.address('x')
            latest_block = client.block_latest()
            nonce = latest_block['nonce']
            blockhash = latest_block['hash']
            nonce_str = str(nonce)
            self.nonce_list.append(nonce_str)
            print(self.nonce_list)
            time.sleep(60)



Bit().run()     

如果你想持续 运行 你应该将进程守护进程的代码,而且不断调用自身的函数确实不是一个好主意

如果您要使用函数,将列表传入并在函数中循环,只需调用一次:

def code(noncelist):
    while True:
        client = blocktrail.APIClient(api_key="x", api_secret="x", network="BTC", testnet=False)
        address = client.address('x')
        latest_block = client.block_latest()
        nonce = latest_block['nonce']
        blockhash = latest_block['hash']    
        print(nonce)   
        noncestr = str(nonce)
        noncelist.append(noncestr)
        print(noncelist)
        time.sleep(60)


code([])

如果您只想使用一个集合来跟踪已添加的内容,我创建了一个使用具有多个属性的 class 的示例,您将需要捕获更多可能的异常:

import blocktrail, time


class Bit():
    def __init__(self, key, secret, net, retry_fail=1, update=60,max_fail=10):
        self.nonce_list = []
        self.seen = set()
        self.key = key
        self.secret = secret
        self.net = net
        self.key = key
        self.retry_fail = retry_fail
        self.update = update
        self.max_fails = max_fail

    def connect(self):
          return blocktrail.APIClient(api_key=self.key, api_secret=self.secret, network=self.net, testnet=False)

    def run(self):
        while True:
            if not self.max_fails:
                break
            try:
                client = self.connect()
                latest_block = client.block_latest()
            except blocktrail.exceptions.MissingEndpoint as e:
                self.max_fails -= 1
                print("Error: {}\n sleeping for {} second(s) before retrying".format(e.msg, self.retry_fail))
                time.sleep(self.retry_fail)
                continue
            nonce = latest_block['nonce']
            if nonce not in self.seen:             
                self.nonce_list.append(nonce)
                self.seen.add(nonce)
            print(self.nonce_list)
            time.sleep(self.update)

key,secret和net是必填项,你可以传递其他的或者使用默认值:

Bit("key","secret", "net").run()

您正在清空列表,然后附加您期望的内容:

noncelist = []
noncelist.append(noncestr);

可以这样:

noncelist=[]
while True:
    code(noncelist)

修改后的代码:

from __future__ import print_function
import blocktrail, time

def code(noncelist):

    client = blocktrail.APIClient(api_key="x", api_secret="x", network="BTC", testnet=False)
    address = client.address('x')

    latest_block = client.block_latest()

    nonce = latest_block['nonce']

    blockhash = latest_block['hash']

    print(nonce)

    noncestr = str(nonce)

    noncelist.append(noncestr);
    print(noncelist)
    time.sleep(60)
noncelist=[]
while True:
    code()

更合适的方式是:

from __future__ import print_function
import blocktrail, time

def code():

    client = blocktrail.APIClient(api_key="x", api_secret="x", network="BTC", testnet=False)
    address = client.address('x')

    latest_block = client.block_latest()

    nonce = latest_block['nonce']

    blockhash = latest_block['hash']

    print(nonce)

    noncestr = str(nonce)
    time.sleep(60)
    return noncestr 
noncelist=[]
while True:
    noncelist.append(code())
    print noncelist