小编Sal*_*Ali的帖子

检查给定字典中给定多个键中是否存在任何键

我知道检查给定字典中是否存在多个键的最佳方法。

if {'foo', 'bar'} <= my_dict.keys():
    # True
Run Code Online (Sandbox Code Playgroud)

现在我必须检查给定字典中是否存在任何键并到目前为止:

if any(k in given_keys for k in my_dict):
    # True
Run Code Online (Sandbox Code Playgroud)

我想知道是否有任何方法可以使用子集在第一种情况下检查上面检查的内容。

python dictionary subset python-3.6

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

计算列表中重复元组的数量

我想计算列表中重复元组的数量。

例如num_list?

num_list = [(3,14),(2,8),(10,25),(5,17),(3,2),(7,25),(4,30),(8,7),(int(2),8),(1,22)]
Run Code Online (Sandbox Code Playgroud)

我想返回结果:Total duplicates: 1 这是(2, 8)一对。

到目前为止,我的效率不是很高,所以我想知道是否有更有效的方法可以做到这一点?

count = 0
for a in num_list:
  for b in num_list:

     if a is b:
         continue

     if a[0] == b[0] and a[1] == b[1]:
         count += 1
Run Code Online (Sandbox Code Playgroud)

python

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

看起来像 b'\x1f\x8b\.....' 的文件格式到底是什么

当我从 aws s3 获取文件时,我得到的格式如下:

b'\x1f\x8b\x08\x00\x0e@\xfd[\x00\x03\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)

这到底是什么?

此外,当获取数据 -unzip - 上传回 s3 时,可以使用以下命令来完成:

s3 = boto3.client('s3', use_ssl=False)  
s3.upload_fileobj(
    Fileobj = gzip.GzipFile(
                None,
                'rb',
                fileobj=BytesIO(s3.get_object(Bucket=bucket, Key=gzipped_key)['Body'].read())),
    Bucket=bucket,
    Key=uncompressed_key)
Run Code Online (Sandbox Code Playgroud)

b'\x1f\x8b\x08\x00\x0e@\xfd[\x00\x03\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00'当我运行s3.get_object(Bucket=bucket)线路时我明白了,但为什么要把它绕起来BytesIO呢?

python file

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

标签 统计

python ×3

dictionary ×1

file ×1

python-3.6 ×1

subset ×1