简体   繁体   English

如何从 python 中的字符串列表创建字典或 json?

[英]how to create dictionary or json from list of strings in python?

I am trying to create dictionary or json from list of strings which is going to be value for dictionary and I have dictionary key that going to be match value.我正在尝试从字符串列表中创建字典或 json,这将是字典的值,并且我有字典键将是匹配值。 I think it is intuitive for me to get this done, but I have following error:我认为完成这项工作对我来说很直观,但我有以下错误:

> Traceback (most recent call last):
>     output[elm].append(k) TypeError: list indices must be integers or slices, not str

I am curious what cause this problem.我很好奇是什么导致了这个问题。 can anyone point me out what is wrong with me code?谁能指出我的代码有什么问题?

my attempt我的尝试

here is my code:这是我的代码:

update更新

lst = {api, 1,0,0}
mystring = lst.split(",")

mystring = ['api', '1', '0', '0']
names = {'name', 'mayor', 'minor', 'patch'}

output =[]
for elm in range(len(names)):
    for k in range(len(mystring)):
        output[elm].append(k)

print(output)

how can I fix the error?我该如何解决这个错误? Is there efficient way to make dictionary or json from list of strings without using nested for loop?有没有有效的方法从字符串列表中制作字典或 json 而不使用嵌套的 for 循环? any better idea?有更好的主意吗?

desired output :所需的 output

I want to get dictionary or json something like this:我想得到字典或 json 是这样的:

{
  "mayor": 1,
  "minor": 0,
  "name": "api",
  "patch": 0
}

I am sorry if my coding defect is minor, I couldn't locate source of the problem.如果我的编码缺陷很小,我很抱歉,我找不到问题的根源。 thanks谢谢

A neat one-liner:一个整洁的单线:

my_string = ['api', '1', '0', '0']
names = ['name', 'mayor', 'minor', 'patch']

output = {names[i]: my_string[i] for i in range(len(my_string))}
print(output)

This should give:这应该给出:

{'name': 'api', 'mayor': '1', 'minor': '0', 'patch': '0'}

Note that your variable names is no longer a set;请注意,您的变量names不再是一个集合; There was no need to have a set.没有必要有一套。 Also, if there was a set, the code wouldn't work because sets are not subscriptable.此外,如果有集合,代码将无法工作,因为集合不可下标。

There are a lot of errors in your code: Over here, you are declaring a list, not a dictionary:您的代码中有很多错误:在这里,您声明的是一个列表,而不是字典:

output =[]

Over here you are going over the length of your set and list, you have to go over them:在这里,您将查看您的集合和列表的长度,您必须对它们进行 go :

for elm in range(len(names)):
    for k in range(len(mystring)):

Use builtin zip function.使用内置zip function。 Zip works on lists, so you need to convert your set to list first. Zip 适用于列表,因此您需要先将您的集合转换为列表。 Of cause it's not a good idea to rely on the order in the set, so it's better to create both lists as list当然,依赖集合中的顺序并不是一个好主意,所以最好将两个列表都创建为列表

mystring = ['api', '1', '0', '0']
names = {'name', 'mayor', 'minor', 'patch'}
output = zip(list(names),mystring)
print(dict(output))

or using two lists as an input或使用两个列表作为输入

mystring = ['api', '1', '0', '0']
names = ['name', 'mayor', 'minor', 'patch']
output = zip(names, mystring)
print(dict(output))

Both prints两种印刷品

{'name': 'api', 'patch': '1', 'mayor': '0', 'minor': '0'}

Of cause if you want numbers to be a number and not string, make sure their types are correct in the list that you pass to zip :当然,如果您希望数字是数字而不是字符串,请确保它们的类型在您传递给zip的列表中是正确的:

mystring = ['api', 1, 0, 0]
names = {'name', 'mayor', 'minor', 'patch'}
output = zip(list(names),mystring)
print(dict(output))

prints印刷

{'patch': 'api', 'minor': 1, 'mayor': 0, 'name': 0}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM