以列表作为值的字典,只需要第一个元素,如"code:%"



我有一个data

的字典
dict = {1:["code: 1", "date: 01-01-2000", "date: 01-02-2000", "date: 01-03-2000", "spent: 20$"]}

我想得到这样的输出:

{1:["code: 1",  "date: 01-01-2000", "spent: 20$"]}

基本上我只需要第一个包含"date:"的元素其余元素

我如何获得这个?

尝试:

dct = {
1: [
"code: 1",
"date: 01-01-2000",
"date: 01-02-2000",
"date: 01-03-2000",
"spent: 20$",
]
}
out = {}
for k, v in dct.items():
tmp = {}
for i in reversed(v):
tmp[i.split(":")[0]] = i
out[k] = list(reversed(tmp.values()))
print(out)

打印:

{1: ['code: 1', 'date: 01-01-2000', 'spent: 20$']}

d = {1:["code: 1", "date: 01-01-2000", "date: 01-02-2000", "date: 01-03-2000", "spent: 20$"]}
a=[]
x=d[1][0:2]
y=d[1][-1]
a.append(x)
x.append(y)
output={1:x}
print(output)

相关内容

  • 没有找到相关文章

最新更新