如何在python中从dict内部列表中提取值

2024-06-26 12:59:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的dict内部列表,dictlistg1g4的键,在许多键中有一对。我想打印带有组标题的a23以及每个组的值之和,希望输出如下:

dictlist = { "g1" : [], "g2" : [] , "g3" : [] }

a = ["a", 23]
a1 = ["asd", 3]
a2 = ["asdf", 10]
a3 = ["adg", 5]

b1 = ["df", 5]
b2 = ["dfg", 1]

c = ["dfg", 50]

dictlist.["g1"].append[a]
dictlist.["g1"].append[a1]
dictlist.["g1"].append[a2]
dictlist.["g1"].append[a3]

dictlist.["g2"].append[b]
dictlist.["g2"].append[b1]

dictlist.["g3"].append[c]

我尝试了以下方法:

for i in dictlist.items():
  for k,v in i.item():
     print(k,v)

期望输出:

g1
a, 23
asd,3
asdf,10
adg,5
total, 41

g2
df,5
dfg,1
total, 6

 ...

有人能帮助提取每组数据的总和吗


Tags: a2dffora1a3b1appendg1
3条回答

如果要将dictlist中的每个项目视为字典,为什么它都是一个列表?我建议采用dict中的dict方法:

dictlist = { "g1" : {}, "g2" : {}, "g3" : {} }
a = ["a", 23] # does this really need to be a list?

# assign `a` value (23) into `a` key ("a")
dictlist['g1'][a[0]] = a[1]

# print all items from dictlist
for key, val in dictlist.items():
    print(key)
    for subkey, subval in val.items():
        print(subkey, ",", subval)
    

如果您想将列表保留在dict中,它将如下所示

dictlist = { "g1" : {}, "g2" : {}, "g3" : {} }
a = ["a", 23] # does this really need to be a list?

# assign `a` value (23) into `a` key ("a")
dictlist['g1'].append(a)

# print all items from dictlist
for key, val in dictlist.items():
    print(key)
    for subkey, subval in val: # only difference: dont use `.items()`
        print(subkey, ",", subval)
    

注意:要添加total,请阅读至结尾

首先,要获取dict中某个值的值,索引如下

dict["key"]而不是dict.["key"]

其次,要在列表中添加内容,请使用括号而不是方括号。所以,
.append(x)不是{}

修复我们目前拥有的两件事:

dictlist = { "g1" : [], "g2" : [] , "g3" : [] }

a = ["a", 23]
a1 = ["asd", 3]
a2 = ["asdf", 10]
a3 = ["adg", 5]

b1 = ["df", 5]
b2 = ["dfg", 1]

c = ["dfg", 50]

dictlist["g1"].append(a)
dictlist["g1"].append(a1)
dictlist["g1"].append(a2)
dictlist["g1"].append(a3)

dictlist["g2"].append(b1)
dictlist["g2"].append(b2)

dictlist["g3"].append(c)
#Now, you want to print the key of the dict as the heading, so our first loop will be:
for heading in dict.keys(): # dictlist.keys() returns a list of all the keys
    print(heading)

#The next thing you want is to print the values of the respective key seperated by ",":
    for value in dict[heading]:
        print(*value, sep=",")  # we can use the sep keyword to print it as we require

# The at the end, an empty print() to seperate the headings
    print()

输出:

g1
a, 23
asd, 3
asdf, 10
adg, 5

g2
df, 5
dfg, 1

g3
dfg, 50

现在,对于添加total

for heading in dict.keys():
    print(heading)

    total = 0  # We make a variable, becomes zero for every heading
    for value in dict[heading]: # value is a list like ["a", 23], has str and int.. So;
        total += sum(item for item in value if type(item) == int)
        print(*value, sep=",")

    #print the total before starting the next heading
    print("total," total)
    print()

输出:

g1
a, 23
asd, 3
asdf, 10
adg, 5
total,  41

g2
df, 5
dfg, 1
total,  6

g3
dfg, 50
total,  50

列表中没有dict。您在dict中有列表。一旦您修复了拼写错误(append是一个函数,应该使用括号,而不是括号,并且您没有bb1,您有b1b2),您的代码将生成您描述的结构。它不是这样打印的,因为Python不是这样打印列表的,但数据都在那里:

dictlist = { "g1" : [], "g2" : [] , "g3" : [] }

a = ["a", 23]
a1 = ["asd", 3]
a2 = ["asdf", 10]
a3 = ["adg", 5]

b1 = ["df", 5]
b2 = ["dfg", 1]

c = ["dfg", 50]

dictlist["g1"].append(a)
dictlist["g1"].append(a1)
dictlist["g1"].append(a2)
dictlist["g1"].append(a3)

dictlist["g2"].append(b1)
dictlist["g2"].append(b2)

dictlist["g3"].append(c)
print(dictlist)

for k, v in dictlist.items():
   print(k)
   for i in v:
       print(i)

输出:

timr@tims-gram:~/src$ python x.py
{'g1': [['a', 23], ['asd', 3], ['asdf', 10], ['adg', 5]], 'g2': [['df', 5], ['dfg', 1]], 'g3': [['dfg', 50]]}
g1
['a', 23]
['asd', 3]
['asdf', 10]
['adg', 5]
g2
['df', 5]
['dfg', 1]
g3
['dfg', 50]
timr@tims-gram:~/src$ 

相关问题 更多 >