社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Massifox

Massifox 最近创建的主题
Massifox 最近回复了
7 年前
回复了 Massifox 创建的主题 » 需要有关python 3.7的简单脚本的帮助[duplicate]

如果没有dict,请尝试以下解决方案:

x, y, z = 0, 1, 3    
offset = ord('c')
[chr(i + offset) for i in (x,y,z)]

并给出:

['c', 'd', 'f']
7 年前
回复了 Massifox 创建的主题 » 如何在python的结构中正确填充数字

给定以下输入:

compressed = [(0, 137), (0, 80), (0, 78), (0, 71), (0, 13), (0, 10), (0, 26), (6, 0), (0, 0), (9, 13), (0, 73), (0, 72), (0, 68), (0, 82), (9, 0), (0, 1), (0, 44), (15, 1), (17, 8), (0, 2), (15, 0), (0, 246)]

请尝试以下代码来写入数据:

import struct

with open('file.dat', 'wb') as f:
    for idx, v in compressed:   
        f.write(struct.pack(">I", idx))
        f.write(struct.pack(">I", v)) 

下面是阅读代码:

with open('file.dat', 'rb') as f:
    data = f.read(4)                     
    nums = []
    while data:     
        idx = struct.unpack(">I", data)[0]  
        data = f.read(4)      
        v = struct.unpack(">I", data)[0]
        data = f.read(4)      
        nums.append((idx,v)) 

和 nums 包含:

[(0, 137), (0, 80), (0, 78), (0, 71), (0, 13), (0, 10), (0, 26), (6, 0), (0, 0), (9, 13), (0, 73), (0, 72), (0, 68), (0, 82), (9, 0), (0, 1), (0, 44), (15, 1), (17, 8), (0, 2), (15, 0), (0, 246)]

和输入一样,事实上 nums == compressed 给予 True 是的。

7 年前
回复了 Massifox 创建的主题 » 如何在python的嵌套列表中向字符串元素追加整数

请尝试以下代码:

words = ['AA', 'AB', 'AB']
words_num = [[w, 0] for w in words]

# output: [['AA', 0], ['AB', 0], ['BB', 0]]

但是,如果我理解正确,为了解决你的主要问题,这可能就足够了:

from collections import Counter
words = ['AA', 'AB', 'BB', 'AA']
counts = Counter(words)

# output: Counter({'AA': 2, 'AB': 1, 'BB': 1})