英文:
Convert list into dict of prefix with different delimiters
问题
我正在尝试将具有三个唯一前缀的项目列表进行转换(例如:apple_,banana_,water_melon_)。
初始列表如下:
table_ids = ["apple_1", "apple_2", "apple_3", "banana_1", "banana_2", "banana_3", "water_melon_1", "water_melon_2", "water_melon_3"]
我期望的结果如下:
{"apple": ["_1", "_2", "_3"], "banana": ["_1", "_2", "_3"], "water_melon": ["_1", "_2", "_3"]}
我尝试过以下代码:
prefixes = ["apple_", "banana_", "water_melon_"]
res =[[id for id in table_ids if(id.startswith(prefix))] for prefix in prefixes]
但是,这会创建一个按前缀分组的列表的列表。
英文:
I am trying to convert a list of items that have three unique prefixes (e.g. apple_, banana_, water_melon_)
The initial list looks like this
table_ids = ["apple_1", "apple_2", "apple_3", "banana_1", "banana_2", "banana_3", "water_melon_1", "water_melon_2", "water_melon_3"]
My desired outcome would look like this:
{"apple": ["_1", "_2", "_3"], "banana": ["_1", "_2", "_3"], "water_melon": ["_1", "_2", "_3"]}
I've tried this
prefixes = ["apple_", "banana_", "water_melon_"]
res =[[id for id in table_ids if(id.startswith(prefix))] for prefix in prefixes]
However, this creates a list of list grouped by prefixes.
答案1
得分: 1
你可以使用 str.rsplit
和 collections.defaultdict
。
from collections import defaultdict
res = defaultdict(list)
for t in table_ids:
res[t.rsplit('_', 1)[0]].append('_' + t.rsplit('_', 1)[1])
print(res)
输出:
defaultdict(<class 'list'>, {'apple': ['_1', '_2', '_3'], 'banana': ['_1', '_2', '_3'], 'water_melon': ['_1', '_2', '_3']})
英文:
You can use str.rsplit
and collections.defaultdict
.
from collections import defaultdict
res = defaultdict(list)
for t in table_ids:
res[t.rsplit('_', 1)[0]].append('_' + t.rsplit('_', 1)[1])
print(res)
Output:
defaultdict(<class 'list'>, {'apple': ['_1', '_2', '_3'], 'banana': ['_1', '_2', '_3'], 'water_melon': ['_1', '_2', '_3']})
答案2
得分: 0
以下是已翻译的内容:
-
You can't do this with a list comprehension because you're trying to create a dict (not a list), and you can't do it with a dict comprehension efficiently because you can't determine which entries go in each sublist without iterating over the original list in its entirety.
-
Here's an example of how to do it by iterating over the list and appending to entries in a dictionary:
>>> table_ids = ["apple_1", "apple_2", "apple_3", "banana_1", "banana_2", "banana_3", "water_melon_1", "water_melon_2", "water_melon_3"] >>> tables = {} >>> for x in table_ids: ... t, _, i = x.rpartition("_") ... tables.setdefault(t, []).append("_" + i) ... >>> tables {'apple': ['_1', '_2', '_3'], 'banana': ['_1', '_2', '_3'], 'water_melon': ['_1', '_2', '_3']}
-
If you really wanted to do it in a nested dict/list comprehension, that'd look like:
>>> {t: ["_" + x.rpartition("_")[2] for x in table_ids if x.startswith(t)] for t in {x.rpartition("_")[0] for x in table_ids}} {'apple': ['_1', '_2', '_3'], 'banana': ['_1', '_2', '_3'], 'water_melon': ['_1', '_2', '_3']}
-
Note that the list comprehensions inside the dict comprehension make this O(N^2) whereas the first version is O(N).
英文:
You can't do this with a list comprehension because you're trying to create a dict (not a list), and you can't do it with a dict comprehension efficiently because you can't determine which entries go in each sublist without iterating over the original list in its entirety.
Here's an example of how to do it by iterating over the list and appending to entries in a dictionary:
>>> table_ids = ["apple_1", "apple_2", "apple_3", "banana_1", "banana_2", "banana_3", "water_melon_1", "water_melon_2", "water_melon_3"]
>>> tables = {}
>>> for x in table_ids:
... t, _, i = x.rpartition("_")
... tables.setdefault(t, []).append("_" + i)
...
>>> tables
{'apple': ['_1', '_2', '_3'], 'banana': ['_1', '_2', '_3'], 'water_melon': ['_1', '_2', '_3']}
If you really wanted to do it in a nested dict/list comprehension, that'd look like:
>>> {t: ["_" + x.rpartition("_")[2] for x in table_ids if x.startswith(t)] for t in {x.rpartition("_")[0] for x in table_ids}}
{'apple': ['_1', '_2', '_3'], 'banana': ['_1', '_2', '_3'], 'water_melon': ['_1', '_2', '_3']}
Note that the list comprehensions inside the dict comprehension make this O(N^2) whereas the first version is O(N).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论