使用地图创建嵌套字典
creating a nested dictionary using map
我有以下变量:
LANGUAGES = ['EN','DE','FR','NL']
STOPWORDS_EN = [1,2,3,4]
STOPWORDS_DE = [5,6,7,8]
STOPWORDS_FR = [9,10,11,12]
STOPWORDS_NL = [13,14,15,16]
STOPWORDS = [STOPWORDS_EN, STOPWORDS_DE,STOPWORDS_FR, STOPWORDS_NL]
ARTICLES_EN = ['the','a']
ARTICLES_DE = ['der', 'die', 'das']
ARTICLES_FR = ['le', 'la', 'les', 'las']
ARTICLES_NL = ['het','die']
ARTICLES = [ARTICLES_EN, ARTICLES_DE,ARTICLES_FR,ARTICLES_NL]
尝试 1:
{lang: {'stopwords':sw, 'articles':art} for lang in LANGUAGES for sw in STOPWORDS for art in ARTICLES}
尝试 2:
{lang: {'stopwords':STOPWORDS[i], 'articles':ARTICLES[i]} for lang in LANGUAGES for i in range(4)}
如何在没有循环的情况下进行,即使用一个衬垫?
期望的输出:
{'EN': {'stopwords': [1,2,3,4], 'articles': ['het', 'die']},
'DE': {'stopwords': [5,6,7,8], 'articles': ['the','a']},
'FR': {'stopwords': [9,10,11,12], 'articles': ['le', 'la', 'les', 'las']},
'NL': {'stopwords': [13, 14, 15, 16], 'articles': ['het', 'die']}}
感谢
您可以将 zip()
用作:
{ lan: {'stopwords': stop, 'articles': art } for lan, stop, art in zip(LANGUAGES, STOPWORDS, ARTICLES)}
您可以使用 enumerate():
{lang: {'stopwords': STOPWORDS[i], 'articles': ARTICLES[i]} for i, lang in enumerate(LANGUAGES)}
{'EN': {'stopwords': [1, 2, 3, 4], 'articles': ['the', 'a']},
'DE': {'stopwords': [5, 6, 7, 8], 'articles': ['der', 'die', 'das']},
'FR': {'stopwords': [9, 10, 11, 12], 'articles': ['le', 'la', 'les', 'las']},
'NL': {'stopwords': [13, 14, 15, 16], 'articles': ['het', 'die']}}
我有以下变量:
LANGUAGES = ['EN','DE','FR','NL']
STOPWORDS_EN = [1,2,3,4]
STOPWORDS_DE = [5,6,7,8]
STOPWORDS_FR = [9,10,11,12]
STOPWORDS_NL = [13,14,15,16]
STOPWORDS = [STOPWORDS_EN, STOPWORDS_DE,STOPWORDS_FR, STOPWORDS_NL]
ARTICLES_EN = ['the','a']
ARTICLES_DE = ['der', 'die', 'das']
ARTICLES_FR = ['le', 'la', 'les', 'las']
ARTICLES_NL = ['het','die']
ARTICLES = [ARTICLES_EN, ARTICLES_DE,ARTICLES_FR,ARTICLES_NL]
尝试 1:
{lang: {'stopwords':sw, 'articles':art} for lang in LANGUAGES for sw in STOPWORDS for art in ARTICLES}
尝试 2:
{lang: {'stopwords':STOPWORDS[i], 'articles':ARTICLES[i]} for lang in LANGUAGES for i in range(4)}
如何在没有循环的情况下进行,即使用一个衬垫?
期望的输出:
{'EN': {'stopwords': [1,2,3,4], 'articles': ['het', 'die']},
'DE': {'stopwords': [5,6,7,8], 'articles': ['the','a']},
'FR': {'stopwords': [9,10,11,12], 'articles': ['le', 'la', 'les', 'las']},
'NL': {'stopwords': [13, 14, 15, 16], 'articles': ['het', 'die']}}
感谢
您可以将 zip()
用作:
{ lan: {'stopwords': stop, 'articles': art } for lan, stop, art in zip(LANGUAGES, STOPWORDS, ARTICLES)}
您可以使用 enumerate():
{lang: {'stopwords': STOPWORDS[i], 'articles': ARTICLES[i]} for i, lang in enumerate(LANGUAGES)}
{'EN': {'stopwords': [1, 2, 3, 4], 'articles': ['the', 'a']},
'DE': {'stopwords': [5, 6, 7, 8], 'articles': ['der', 'die', 'das']},
'FR': {'stopwords': [9, 10, 11, 12], 'articles': ['le', 'la', 'les', 'las']},
'NL': {'stopwords': [13, 14, 15, 16], 'articles': ['het', 'die']}}