获得单身"设置"来自字典中存在的列表值的对象

时间:2018-06-03 20:38:52

标签: python python-3.x set list-comprehension set-comprehension

我试图根据字典的值构建一个集合。每个字典值都是一个字符串列表。

{'a': ['a','b','c'],'b':['a','b','d'],...}

我正在尝试使用.update(x)连接包含字典值的集合。我已经成功使用标准for循环:

ingredientSet = set()
for values in recipes.values():
    ingredientSet.update(values)

如果可能的话,我想做的是在集合理解中做到这一点。到目前为止,我有这个:

ingredientSet = { ingredientSet.update(x) for x in recipes.values() }

但是我的IDE给了我一个错误" ingredientSet"在分配之前引用。

是否可以在理解中使用.update(x),还是有另一种方法可以在理解中将项目连接到集合中?

2 个答案:

答案 0 :(得分:4)

以下是使用itertools.chain.from_iterable(...)实现此目的的功能性方法:

>>> from itertools import chain
>>> my_dict = {'a': ['a','b','c'],'b':['a','b','d']}

>>> set(chain.from_iterable(my_dict.values()))
{'a', 'b', 'c', 'd'}

此外,使用set().union(...)在评论中添加 jonsharpe&#39> amswer:

>>> set().union(*my_dict.values())
{'a', 'b', 'd', 'c'}

性能比较

以下是Python3上所有答案的timeit比较:

  • 使用itertools.chain.from_iterable - 每个循环0.558次使用

    mquadri$ python3 -m timeit -s "from itertools import chain; my_dict = {'a': ['a','b','c'],'b':['a','b','d']}" "set(chain.from_iterable(my_dict.values()))"
    1000000 loops, best of 3: 0.558 usec per loop
    
  • 使用设置理解 - 每个循环0.585 usec

    mquadri$ python3 -m timeit -s "from itertools import chain; my_dict = {'a': ['a','b','c'],'b':['a','b','d']}" "{item for items in my_dict.values() for item in items}"
    1000000 loops, best of 3: 0.585 usec per loop
    
  • 使用set().union(...) - 每个循环使用0.614个

    mquadri$ python3 -m timeit -s "from itertools import chain; my_dict = {'a': ['a','b','c'],'b':['a','b','d']}" "set().union(*my_dict.values())"
    1000000 loops, best of 3: 0.614 usec per loop
    

答案 1 :(得分:3)

如果你想要理解,你可以用两个0.02885来做到这一点:

代码:

# Python Version

λ python
Python 3.7.0b5 (v3.7.0b5:abb8802389, May 31 2018, 01:54:01) [MSC v.1913 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>




λ pip3 install web.py
Collecting web.py
  Using cached https://files.pythonhosted.org/packages/fc/58/21649fc1849b1f688f3d42e25e79615cc573469ea57eaa9e6af70b1e3b87/web.py-0.39.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\KARANJ~2\AppData\Local\Temp\pip-install-isj4gcc5\web.py\setup.py", line 6, in <module>
        from web import __version__
      File "C:\Users\KARANJ~2\AppData\Local\Temp\pip-install-isj4gcc5\web.py\web\__init__.py", line 14, in <module>
        import utils, db, net, wsgi, http, webapi, httpserver, debugerror
      File "C:\Users\Karanjit Singh\AppData\Roaming\Python\Python37\site-packages\db\__init__.py", line 69
        print "var", var
                  ^
    SyntaxError: Missing parentheses in call to 'print'. Did you mean print("var", var)?

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\KARANJ~2\AppData\Local\Temp\pip-install-isj4gcc5\web.py\

测试代码:

for

结果:

values_set = {item for items in data.values() for item in items}