小编Tom*_*koo的帖子

如何用值列表展平字典?

我正在尝试“压平”一本看起来像这样的字典:

d = {
    "USA": ["US", "United States"],
    "SGP": ["JP", "Japan", "Singapore"]
}
Run Code Online (Sandbox Code Playgroud)

我想要的格式是这样的:

new_d = {
    "United States": "USA",
    "US": "USA",
    "JP": "SGP",
    "Japan": "SGP",
    "Singapore": "SGP"
}
Run Code Online (Sandbox Code Playgroud)

python dictionary python-3.x python-3.7

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

字典列表中的第一个非空值

假设我想获取此字典列表中的第一个key为空:

arr = [
    {
      "key": None,
      "anotherkey": 0
    },
    {
      "another": "ignore"
    },
    {
      "bool": True,
      "key": "this!"
    }
  ]
Run Code Online (Sandbox Code Playgroud)

有一些单行线可以做到这一点吗?我用 for 循环做到了。

python

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

使用 Python 按字典列表中的任何字符进行搜索

我想按字典列表中的任何字符进行搜索。

my_list = [
           {"name": "MrA", "age": 20, "height": 185},
           {"name": "MrsB", "age": 28, "height": 192},
           {"name": "MrC", "age": 18, "height": 170},
           {"name": "MrD", "age": 50, "height": 177},
           {"name": "MrsE", "age": 32, "height": 200},
           {"name": "Mrs18F", "age": 21, "height": 175}
          ]

keywords = "MrA"
my_list  = [item for item in my_list if keywords in list(item.values())]
print(my_list) # result is [{"name": "MrA", "age": 20, "height": 185}]
Run Code Online (Sandbox Code Playgroud)

正如所见,我只能按完整字符进行搜索。但我想处理这个列表,其预期结果是通过所有字段中的任何字符进行搜索:

keywords = "Mrs"

[{"name": "MrsB", "age": 28, "height": 192},
 {"name": "MrsE", "age": …
Run Code Online (Sandbox Code Playgroud)

python

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

正则表达式按元音出现一次的顺序匹配

我试图找出一个正确的正则表达式来匹配单词中出现的元音。单词中可以有任意数量的辅音;但是,除了上面列出的 5 个元音之外,应该没有其他元音。例如,单词“sacrilegious”不应匹配,因为尽管它包含按字母顺序排列的五个元音,但在“a”和“e”之间还有一个额外的“i”元音。不允许使用连字符。事实上,您的正则表达式不应匹配任何包含大写和小写字母以外的任何字符的“单词”。

这里有一些它应该匹配的词

abstemious
facetious
arsenious
acheilous
anemious
caesious
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我想出的,但是当我运行程序时,它似乎没有做它应该做的事情。

#!/usr/bin/perl -w
use strict;


my $test =  "abstemious";

if( $test =~ /a[^eiou]*e[^aiou]*i[^aeou]*o[^aeiu]u/ )
{

    print "yes";

}
Run Code Online (Sandbox Code Playgroud)

regex perl

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

前 100 个素数

我知道有多种方法可以找到前 100 个素数,但请帮助我采用我的方法。我发现 的值count正在增加,但由于某种原因while循环条件不适用:

count = 0

while(count <= 20):
    for i in range(2, 20):
        for j in range(2, i):
            if i < j:
                print("The number",i,"is prime")
            elif i % j == 0:
                break
        else:
            print("The number",i,"is prime")
            count = count + 1
            print(count)
Run Code Online (Sandbox Code Playgroud)

python primes

0
推荐指数
1
解决办法
2万
查看次数

如何处理缩放和api调用错误

当我创建 jwt 并调用 Zoom api 时,出现错误{'code': 124, 'message': 'Invalid access token.'}。这是什么意思?

ApiKey = 'xxx'
ApiSercret = 'xxx'
mail = request.POST['mail']
print(mail)
today = datetime.today()
header = {
'alg':'HS256'
}

payload = { 
'iss': ApiKey,
'exp': today + timedelta(hours=1),
}

#https://docs.authlib.org/en/latest/specs/rfc7519.html#authlib.jose.rfc7519.JWT.check_sensitive_data
token = jwt.encode(header,payload,ApiSercret,check='true')
print(token)
import http.client

        conn = http.client.HTTPSConnection("api.zoom.us")

        headers = {
            'authorization': "Bearer 39ug3j309t8unvmlmslmlkfw853u8",
            'content-type': "application/json"
        }   

        conn.request("GET", "/v2/users?status=active&page_size=30&page_number=1", headers=headers)

        res = conn.getresponse()
        data = res.read()

        print(data.decode("utf-8"))
        
params = {
mail:token
}

return render(request,'api/index.html',params)
Run Code Online (Sandbox Code Playgroud)

错误内容:

{'code': 124, …
Run Code Online (Sandbox Code Playgroud)

python django jwt zoom-sdk

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

How to glue (stitch) two python code together?

I am wondering what is the best way to put together codes from different python files into a new python file. Lets say the first file 1.pywould consist of:

def(...):
    a=1
    b=2
    return c=a+b
Run Code Online (Sandbox Code Playgroud)

And another file 2.py consists of:

def(...):
    d = 4
    e = c*d
    return e
Run Code Online (Sandbox Code Playgroud)

Now let's say you have a final file, final.py, which you want all the codes from 1.py and 2.py copied word by word to it in a specific order …

python python-3.x

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

如何在只知道某些字符的情况下在单词表中查找单词

所以我有一个包含 3 个词的词表:

Apple
Christmas Tree
Shopping Bag
Run Code Online (Sandbox Code Playgroud)

而且我只知道单词中的某些字符和单词的长度,例如:

???i???as ?r??

在哪里意味着它是一个未知字符,我想将它输入到控制台中,并获得单词列表中所有单词的输出,这些单词在这些地方包含这些字符并具有此数量的字符。

有什么办法可以实现这一目标吗?我希望我的程序以与https://onelook.com/相同的方式运行。

python string

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

如何将空格分隔的数字作为同一行的输入?

a, b = int(input(), int(input())
print(a)
print(b)
Run Code Online (Sandbox Code Playgroud)

我想在同一行输入两个输入,640 48但出现错误:

invalid literal for int() with base 10 : '640 480'
Run Code Online (Sandbox Code Playgroud)

python user-input python-3.x

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

如何在不使用循环的情况下将一维列表转换为二维列表?

我有一个浮动列表:

[1.0, 2.0, 3.0, 4.0] 
Run Code Online (Sandbox Code Playgroud)

如何将此列表重塑为多维数组,例如:

[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 3.0, 3.0], [4.0, 4.0 ,4.0]]
Run Code Online (Sandbox Code Playgroud)

不使用循环?是否可以使用numpy或任何其他?

python arrays numpy list

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