Python convert pairs list to dictionary

前端 未结 4 1906
执念已碎
执念已碎 2020-12-06 09:04

I have a list of about 50 strings with an integer representing how frequently they occur in a text document. I have already formatted it like shown below, and am trying to c

4条回答
  •  温柔的废话
    2020-12-06 09:41

    Like this, Python's dict() function is perfectly designed for converting a list of tuples, which is what you have:

    >>> string = [('limited', 1), ('all', 16), ('concept', 1), ('secondly', 1)]
    >>> my_dict = dict(string)
    >>> my_dict
    {'all': 16, 'secondly': 1, 'concept': 1, 'limited': 1}
    

提交回复
热议问题