小编Shu*_*ota的帖子

在python字典中推送数据的更好方法

我有四件事要使用push_to_dict方法推送到 dict user_post_dict

user_post_dict= {}

def push_to_dict(user_email, post_id, question_text, question_answer):
    if user_email in user_post_dict:
        if post_id in user_post_dict[user_email]:
            user_post_dict[user_email][post_id][question_text] = question_answer
        else:
            user_post_dict[user_email][post_id] = {}
            user_post_dict[user_email][post_id][question_text] = question_answer
    else:
        user_post_dict[user_email] = {}
        user_post_dict[user_email][post_id] = {}
        user_post_dict[user_email][post_id][question_text] = question_answer

push_to_dict('abc@gmail.com',1,'what is this?', 'this is something')
push_to_dict('abc@gmail.com',2,'what is that?', 'that is something')
push_to_dict('def@gmail.com',1,'what is this?', 'this is something')
push_to_dict('def@gmail.com',2,'what is that?', 'that is something')
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来优化代码或缩短代码。

python dictionary list python-3.x

1
推荐指数
1
解决办法
217
查看次数

javascript 重试异步等待

我有多个异步函数,它们都向服务器发送请求,如果有错误,它们会捕获它然后重试该函数,这些函数依赖于前一个函数的数据,因此它们必须一个接一个地发送,问题是每当我调用这些函数并且出现错误时,它会像我想要的那样不断重试,但它会继续执行下一个函数,而不是等待前一个函数返回已解决的响应。

const request1 = async () => {
    try {
        const data = await rp.get(link, options)
        return data
    } catch (err) {
        request1()
    }

}

const request2 = async (data) => {
    try {
        const data = await rp.get(link, options)
        return data
    } catch (err) {
        request2()
    }

}

const getData = async() => {
await request1()
await request2()

})

getData()
Run Code Online (Sandbox Code Playgroud)

每当我调用 getData() 函数时,它都会等待第一个请求,但即使它有错误,它也会在第二个请求之后立即移动,而不是等待第一个请求解决,我也需要一个 try catch for all我发送的请求而不是一个,因为如果出现错误,我只想重试这一步,而不是完整的

javascript node.js async-await

1
推荐指数
1
解决办法
366
查看次数