Creating a dictionary with list of lists in Python

前端 未结 3 1872
夕颜
夕颜 2020-12-01 18:32

I have a huge file (with around 200k inputs). The inputs are in the form:

A B C D
B E F
C A B D
D  

I am reading this file and storing it i

3条回答
  •  醉话见心
    2020-12-01 18:37

    A dictionary comprehension makes short work of this task:

    >>> s = [['A','B','C','D'], ['B','E','F'], ['C','A','B','D'], ['D']]
    >>> {t[0]:t[1:] for t in s}
    {'A': ['B', 'C', 'D'], 'C': ['A', 'B', 'D'], 'B': ['E', 'F'], 'D': []}
    

提交回复
热议问题