小编bon*_*tit的帖子

如果变量不是 None,则将 key:value 添加到字典中

我有一个函数clans(),它接受 8 个参数。如果参数不是,我需要将键和值对添加到字典中None。我的解决方案是:

def clans(self, name: str = None, warFrequency: str = None, location: str = None,
            minMembers: int = None, maxMembers: int = None,
            minClanPoints: int = None, minClanLevel: int = None,
            labels: list[str] = None):
    params = {} # <- the dictionary to add to

    # very much if-conditions
    if name is not None:
        params['name'] = name
    if warFrequency is not None:
        params['warFrequency'] = warFrequency
    ... # <- other function arguments and if conditions …
Run Code Online (Sandbox Code Playgroud)

python dictionary if-statement python-3.x nonetype

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

如何使用 scanf 从缓冲区中提取一些格式化字符串?

我需要从那个长字符串中提取“rudolf”“12”"hello, i know that rudolph=12 but it so small..." :使用scanf,我该怎么做?

该缓冲区可以包含任何格式化字符串,例如ruby=45bomb=1,但我事先并不知道。

我正在尝试类似的事情,但没有成功

#include <stdio.h>

int main()
{
    char sentence[] = "hello, i know that rudolph=12 but it so small...";
    char name[32];
    int value;

    sscanf(sentence, "%[a-z]=%d", name, &value);
    printf("%s -> %d\n", name, value);

    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c scanf formatted formatted-text

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