This is an extension of this question: How to split a string within a list to create key-value pairs in Python
这是此问题的扩展:如何在列表中拆分字符串以在Python中创建键值对
The difference from the above question is the items in my list are not all key-value pairs; some items need to be assigned a value.
与上述问题的区别在于我列表中的项目并非都是键值对;某些项目需要分配一个值。
I have a list:
我有一个清单:
list = ['abc=ddd', 'ef', 'ghj', 'jkl=yui', 'rty']
I would like to create a dictionary:
我想创建一个字典:
dict = { 'abc':'ddd', 'ef':1, 'ghj':1, 'jkl':'yui', 'rty':1 }
I was thinking something along the lines of:
我在思考以下几点:
a = {}
for item in list:
if '=' in item:
d = item.split('=')
a.append(d) #I don't I can do this.
else:
a[item] = 1 #feel like I'm missing something here.
4 个解决方案
#1
10
For each split "pair", you can append [1]
and extract the first 2 elements. This way, 1 will be used when there isn't a value:
对于每个拆分“对”,您可以追加[1]并提取前两个元素。这样,当没有值时将使用1:
print dict((s.split('=')+[1])[:2] for s in l)
#2
3
I would be using something similar to the post you linked.
我会使用与你链接的帖子类似的东西。
d = dict(s.split('=') for s in a)
If you combine what you can learn from this post -- that is to use lists to create dictionaries -- and from using if/else in Python's list comprehension, you can come up with something like this:
如果你结合你可以从这篇文章中学到的东西 - 即使用列表来创建词典 - 以及在Python的列表理解中使用if / else,你可以想出这样的东西:
d = dict(s.split("=", maxsplit=1) if "=" in s else [s, 1] for s in l)
What this does is add 1
to the end of the split list if there is no equal sign in it.
如果没有相同的符号,它的作用是在拆分列表的末尾添加1。
#3
1
Here are the step-by-step approach.
以下是逐步的方法。
In [50]: mylist = ['abc=ddd', 'ef', 'ghj', 'jkl=yui', 'rty']
In [51]: [element.split('=') for element in mylist]
Out[51]: [['abc', 'ddd'], ['ef'], ['ghj'], ['jkl', 'yui'], ['rty']]
In [52]: [element.split('=') + [1] for element in mylist]
Out[52]: [['abc', 'ddd', 1], ['ef', 1], ['ghj', 1], ['jkl', 'yui', 1], ['rty', 1]]
In [53]: [(element.split('=') + [1])[:2] for element in mylist]
Out[53]: [['abc', 'ddd'], ['ef', 1], ['ghj', 1], ['jkl', 'yui'], ['rty', 1]]
In [54]: dict((element.split('=') + [1])[:2] for element in mylist)
Out[54]: {'abc': 'ddd', 'ef': 1, 'ghj': 1, 'jkl': 'yui', 'rty': 1}
- In order to convert your list in line 50 to a dictionary, you will need to convert it to the list in line 53.
- 要将第50行中的列表转换为字典,您需要将其转换为第53行中的列表。
- Line 51. The first step is to split each element in the list by the equal sign. Each element now is transformed into a list of 1- or 2 elements. Notice that some element like
'ef'
which does not have equal sign, we will have to fix that - 第51行。第一步是用等号分割列表中的每个元素。现在每个元素都转换为1或2个元素的列表。请注意,某些元素如'ef'没有等号,我们将不得不修复它
- Line 52. Next, we append 1 to each sub list. That should take care of the sublists with 1 element, but making some sublist 3 element long
- 第52行。接下来,我们在每个子列表中附加1。这应该照顾带有1个元素的子列表,但是要使一些子列表3元素长
- Line 53: We normalize all sublist into 2-element ones by taking just the first two element and discard the third one if applicable. This list now is in the correct format to convert into a dictionary
- 第53行:我们通过仅取前两个元素将所有子列表标准化为2个元素,并在适用时丢弃第三个元素。此列表现在具有正确的格式以转换为字典
- Line 54. The last step is to take this list and convert it into a dictionary. Since the
dict
class can take a generator expression, we can safely remove the square brackets. - 第54行。最后一步是获取此列表并将其转换为字典。由于dict类可以采用生成器表达式,因此我们可以安全地删除方括号。
With that, here is the snippet:
有了这个,这里是片段:
mylist = ['abc=ddd', 'ef', 'ghj', 'jkl=yui', 'rty']
mydict = dict((element.split('=') + [1])[:2] for element in mylist)
#4
1
input_list = ['abc=ddd', 'ef', 'ghj', 'jkl=yui', 'rty']
output_dict = {}
for item in input_list:
item_split = item.split('=')
key = item_split[0]
value = item_split[1] if len(item_split)>1 else 1
output_dict[key] = value
a bit more concisely
更简洁一点
for item in input_list:
i_s = item.split('=')
output_dict[i_s[0]] = i_s[1] if len(i_s)>1 else 1
This has the advantage that it doesn't append an extra element to each list created by splitting the elements of the input_list. Though, list comprehensions can be faster than a for
loop
这样做的好处是,它不会为通过拆分input_list的元素而创建的每个列表附加额外的元素。但是,列表推导可能比for循环更快
#1
10
For each split "pair", you can append [1]
and extract the first 2 elements. This way, 1 will be used when there isn't a value:
对于每个拆分“对”,您可以追加[1]并提取前两个元素。这样,当没有值时将使用1:
print dict((s.split('=')+[1])[:2] for s in l)
#2
3
I would be using something similar to the post you linked.
我会使用与你链接的帖子类似的东西。
d = dict(s.split('=') for s in a)
If you combine what you can learn from this post -- that is to use lists to create dictionaries -- and from using if/else in Python's list comprehension, you can come up with something like this:
如果你结合你可以从这篇文章中学到的东西 - 即使用列表来创建词典 - 以及在Python的列表理解中使用if / else,你可以想出这样的东西:
d = dict(s.split("=", maxsplit=1) if "=" in s else [s, 1] for s in l)
What this does is add 1
to the end of the split list if there is no equal sign in it.
如果没有相同的符号,它的作用是在拆分列表的末尾添加1。
#3
1
Here are the step-by-step approach.
以下是逐步的方法。
In [50]: mylist = ['abc=ddd', 'ef', 'ghj', 'jkl=yui', 'rty']
In [51]: [element.split('=') for element in mylist]
Out[51]: [['abc', 'ddd'], ['ef'], ['ghj'], ['jkl', 'yui'], ['rty']]
In [52]: [element.split('=') + [1] for element in mylist]
Out[52]: [['abc', 'ddd', 1], ['ef', 1], ['ghj', 1], ['jkl', 'yui', 1], ['rty', 1]]
In [53]: [(element.split('=') + [1])[:2] for element in mylist]
Out[53]: [['abc', 'ddd'], ['ef', 1], ['ghj', 1], ['jkl', 'yui'], ['rty', 1]]
In [54]: dict((element.split('=') + [1])[:2] for element in mylist)
Out[54]: {'abc': 'ddd', 'ef': 1, 'ghj': 1, 'jkl': 'yui', 'rty': 1}
- In order to convert your list in line 50 to a dictionary, you will need to convert it to the list in line 53.
- 要将第50行中的列表转换为字典,您需要将其转换为第53行中的列表。
- Line 51. The first step is to split each element in the list by the equal sign. Each element now is transformed into a list of 1- or 2 elements. Notice that some element like
'ef'
which does not have equal sign, we will have to fix that - 第51行。第一步是用等号分割列表中的每个元素。现在每个元素都转换为1或2个元素的列表。请注意,某些元素如'ef'没有等号,我们将不得不修复它
- Line 52. Next, we append 1 to each sub list. That should take care of the sublists with 1 element, but making some sublist 3 element long
- 第52行。接下来,我们在每个子列表中附加1。这应该照顾带有1个元素的子列表,但是要使一些子列表3元素长
- Line 53: We normalize all sublist into 2-element ones by taking just the first two element and discard the third one if applicable. This list now is in the correct format to convert into a dictionary
- 第53行:我们通过仅取前两个元素将所有子列表标准化为2个元素,并在适用时丢弃第三个元素。此列表现在具有正确的格式以转换为字典
- Line 54. The last step is to take this list and convert it into a dictionary. Since the
dict
class can take a generator expression, we can safely remove the square brackets. - 第54行。最后一步是获取此列表并将其转换为字典。由于dict类可以采用生成器表达式,因此我们可以安全地删除方括号。
With that, here is the snippet:
有了这个,这里是片段:
mylist = ['abc=ddd', 'ef', 'ghj', 'jkl=yui', 'rty']
mydict = dict((element.split('=') + [1])[:2] for element in mylist)
#4
1
input_list = ['abc=ddd', 'ef', 'ghj', 'jkl=yui', 'rty']
output_dict = {}
for item in input_list:
item_split = item.split('=')
key = item_split[0]
value = item_split[1] if len(item_split)>1 else 1
output_dict[key] = value
a bit more concisely
更简洁一点
for item in input_list:
i_s = item.split('=')
output_dict[i_s[0]] = i_s[1] if len(i_s)>1 else 1
This has the advantage that it doesn't append an extra element to each list created by splitting the elements of the input_list. Though, list comprehensions can be faster than a for
loop
这样做的好处是,它不会为通过拆分input_list的元素而创建的每个列表附加额外的元素。但是,列表推导可能比for循环更快