小编Lan*_*ana的帖子

如何从原始数据字符串创建字典

基本上我需要从一串数据创建一个字典

鉴于:

data = "electron1, gamma5, proton43, boson98, ..."
Run Code Online (Sandbox Code Playgroud)

d(data) 会导致:

{'electron':1, 'gamma':5, 'proton':43, 'boson':98, ...}
Run Code Online (Sandbox Code Playgroud)

我当前的代码显示"基数10 ..."的错误消息

def d(n):
        pair = dict()
        for i in range(0,n):
                word = input().split()
                key = word[0]
                value = word[1]
                pair[key]=value
        print(pair)

n = int(input())          
d ={}                     
for i in range(n):        
    text = input().split()
    d[text[0]] = text[1]
print(d)
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
2
解决办法
167
查看次数

标签 统计

python ×1