将列表转换为具有不同分隔符的前缀的字典

huangapple go评论52阅读模式
英文:

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.rsplitcollections.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(&#39;_&#39;, 1)[0]].append(&#39;_&#39; + t.rsplit(&#39;_&#39;, 1)[1])
print(res)

Output:

defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;apple&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;], &#39;banana&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;], &#39;water_melon&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;]})

答案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:

&gt;&gt;&gt; table_ids = [&quot;apple_1&quot;, &quot;apple_2&quot;, &quot;apple_3&quot;, &quot;banana_1&quot;, &quot;banana_2&quot;, &quot;banana_3&quot;, &quot;water_melon_1&quot;, &quot;water_melon_2&quot;, &quot;water_melon_3&quot;]
&gt;&gt;&gt; tables = {}
&gt;&gt;&gt; for x in table_ids:
...     t, _, i = x.rpartition(&quot;_&quot;)
...     tables.setdefault(t, []).append(&quot;_&quot; + i)
...
&gt;&gt;&gt; tables
{&#39;apple&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;], &#39;banana&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;], &#39;water_melon&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;]}

If you really wanted to do it in a nested dict/list comprehension, that'd look like:

&gt;&gt;&gt; {t: [&quot;_&quot; + x.rpartition(&quot;_&quot;)[2] for x in table_ids if x.startswith(t)] for t in {x.rpartition(&quot;_&quot;)[0] for x in table_ids}}
{&#39;apple&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;], &#39;banana&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;], &#39;water_melon&#39;: [&#39;_1&#39;, &#39;_2&#39;, &#39;_3&#39;]}

Note that the list comprehensions inside the dict comprehension make this O(N^2) whereas the first version is O(N).

huangapple
  • 本文由 发表于 2023年2月6日 03:45:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定