通过多个for循环在Python中创建字典

通过多个for循环在Python中创建字典,python,for-loop,dictionary,Python,For Loop,Dictionary,我知道下面的语法是关闭的,但是我很难弄清楚应该如何编写它。我试图做的是获取列表“list”并创建一个字典,其中键是每个单词与其他非自身单词的组合,每个键的值为0。这是我的坏代码: lst = ('human', 'loud', 'big') for words in lst: first = words for words in lst: if words != first: scores = {'%s + %s': 0, % (first

我知道下面的语法是关闭的,但是我很难弄清楚应该如何编写它。我试图做的是获取列表“list”并创建一个字典,其中键是每个单词与其他非自身单词的组合,每个键的值为0。这是我的坏代码:

lst = ('human', 'loud', 'big')
for words in lst:
    first = words
    for words in lst:
        if words != first:
            scores = {'%s + %s': 0, % (first, words)}
字典如下所示:

scores = {'human + loud': 0, 'human + big': 0, 'loud + big': 0, 'loud + human': 0, 'big + loud': 0, 'big + human': 0}
非常感谢您的帮助


已编辑:要将列表类型更改为lst。

词典分配行存在语法问题,但更重要的问题是,每次都分配词典

相反,在开始时创建字典,然后添加到字典中

最后,列表不是一个好的变量名,因为它与类型的名称冲突

mylist = ('human', 'loud', 'big')
scores = {}
for w in mylist:
    for v in mylist:
        if w != v:
            scores['%s + %s'  % (w, v)] = 0

print scores

字典赋值行有语法问题,但更重要的问题是每次都在赋值字典

相反,在开始时创建字典,然后添加到字典中

最后,列表不是一个好的变量名,因为它与类型的名称冲突

mylist = ('human', 'loud', 'big')
scores = {}
for w in mylist:
    for v in mylist:
        if w != v:
            scores['%s + %s'  % (w, v)] = 0

print scores

字典赋值行有语法问题,但更重要的问题是每次都在赋值字典

相反,在开始时创建字典,然后添加到字典中

最后,列表不是一个好的变量名,因为它与类型的名称冲突

mylist = ('human', 'loud', 'big')
scores = {}
for w in mylist:
    for v in mylist:
        if w != v:
            scores['%s + %s'  % (w, v)] = 0

print scores

字典赋值行有语法问题,但更重要的问题是每次都在赋值字典

相反,在开始时创建字典,然后添加到字典中

最后,列表不是一个好的变量名,因为它与类型的名称冲突

mylist = ('human', 'loud', 'big')
scores = {}
for w in mylist:
    for v in mylist:
        if w != v:
            scores['%s + %s'  % (w, v)] = 0

print scores
请尝试以下操作:

lst = ('human', 'loud', 'big')
scores = {}
for f in lst:
    for s in lst:
        if f != s:
            scores['{0} + {1}'.format(f, s)] = 0

print scores
因此:

>>> lst = ('human', 'loud', 'big')
>>> scores = {}
>>> for f in lst:
...     for s in lst:
...         if f != s:
...             scores['{0} + {1}'.format(f, s)] = 0
... 
>>> print scores
{'human + big': 0, 'big + loud': 0, 'big + human': 0, 'human + loud': 0, 'loud + big': 0, 'loud + human': 0}
>>> 
请尝试以下操作:

lst = ('human', 'loud', 'big')
scores = {}
for f in lst:
    for s in lst:
        if f != s:
            scores['{0} + {1}'.format(f, s)] = 0

print scores
因此:

>>> lst = ('human', 'loud', 'big')
>>> scores = {}
>>> for f in lst:
...     for s in lst:
...         if f != s:
...             scores['{0} + {1}'.format(f, s)] = 0
... 
>>> print scores
{'human + big': 0, 'big + loud': 0, 'big + human': 0, 'human + loud': 0, 'loud + big': 0, 'loud + human': 0}
>>> 
请尝试以下操作:

lst = ('human', 'loud', 'big')
scores = {}
for f in lst:
    for s in lst:
        if f != s:
            scores['{0} + {1}'.format(f, s)] = 0

print scores
因此:

>>> lst = ('human', 'loud', 'big')
>>> scores = {}
>>> for f in lst:
...     for s in lst:
...         if f != s:
...             scores['{0} + {1}'.format(f, s)] = 0
... 
>>> print scores
{'human + big': 0, 'big + loud': 0, 'big + human': 0, 'human + loud': 0, 'loud + big': 0, 'loud + human': 0}
>>> 
请尝试以下操作:

lst = ('human', 'loud', 'big')
scores = {}
for f in lst:
    for s in lst:
        if f != s:
            scores['{0} + {1}'.format(f, s)] = 0

print scores
因此:

>>> lst = ('human', 'loud', 'big')
>>> scores = {}
>>> for f in lst:
...     for s in lst:
...         if f != s:
...             scores['{0} + {1}'.format(f, s)] = 0
... 
>>> print scores
{'human + big': 0, 'big + loud': 0, 'big + human': 0, 'human + loud': 0, 'loud + big': 0, 'loud + human': 0}
>>> 

你的想法是对的——这是你的基本逻辑,语法是固定的。两个主要更改是:1)您需要将内部循环中的迭代器变量命名为与外部循环中的迭代器变量不同的名称,否则作用域将发生冲突;2)要向字典添加内容,请使用[key]索引符号,而不是尝试使用大括号符号创建一个全新的字典

list = ['human', 'loud', 'big']
scores = {}
for i in range(len(list)):
    for j in range(len(list)): 
        if i != j:
            key = list[i] + " + " + list[j]
            scores[key] = 0

你的想法是对的——这是你的基本逻辑,语法是固定的。两个主要更改是:1)您需要将内部循环中的迭代器变量命名为与外部循环中的迭代器变量不同的名称,否则作用域将发生冲突;2)要向字典添加内容,请使用[key]索引符号,而不是尝试使用大括号符号创建一个全新的字典

list = ['human', 'loud', 'big']
scores = {}
for i in range(len(list)):
    for j in range(len(list)): 
        if i != j:
            key = list[i] + " + " + list[j]
            scores[key] = 0

你的想法是对的——这是你的基本逻辑,语法是固定的。两个主要更改是:1)您需要将内部循环中的迭代器变量命名为与外部循环中的迭代器变量不同的名称,否则作用域将发生冲突;2)要向字典添加内容,请使用[key]索引符号,而不是尝试使用大括号符号创建一个全新的字典

list = ['human', 'loud', 'big']
scores = {}
for i in range(len(list)):
    for j in range(len(list)): 
        if i != j:
            key = list[i] + " + " + list[j]
            scores[key] = 0

你的想法是对的——这是你的基本逻辑,语法是固定的。两个主要更改是:1)您需要将内部循环中的迭代器变量命名为与外部循环中的迭代器变量不同的名称,否则作用域将发生冲突;2)要向字典添加内容,请使用[key]索引符号,而不是尝试使用大括号符号创建一个全新的字典

list = ['human', 'loud', 'big']
scores = {}
for i in range(len(list)):
    for j in range(len(list)): 
        if i != j:
            key = list[i] + " + " + list[j]
            scores[key] = 0

这是我的代码片段。我建议先创建字典,然后再添加到字典中

    scores = {}

    lst = ('human', 'loud', 'big')

    for words in list:
        first = words
        for words in list:
            if words != first:
                scores.update({first + " + " +  words: 0}) 

    print scores
我建议更改一些变量名,以区分每个嵌套列表中使用的单词。这样,就可以完全取消“first”变量

    scores = {}

    lst = ('human', 'loud', 'big')

    for word1 in list:
        for word2 in list:
            if word2 != word1:
                scores.update({word1 + " + " +  word2: 0}) 

    print scores

这是我的代码片段。我建议先创建字典,然后再添加到字典中

    scores = {}

    lst = ('human', 'loud', 'big')

    for words in list:
        first = words
        for words in list:
            if words != first:
                scores.update({first + " + " +  words: 0}) 

    print scores
我建议更改一些变量名,以区分每个嵌套列表中使用的单词。这样,就可以完全取消“first”变量

    scores = {}

    lst = ('human', 'loud', 'big')

    for word1 in list:
        for word2 in list:
            if word2 != word1:
                scores.update({word1 + " + " +  word2: 0}) 

    print scores

这是我的代码片段。我建议先创建字典,然后再添加到字典中

    scores = {}

    lst = ('human', 'loud', 'big')

    for words in list:
        first = words
        for words in list:
            if words != first:
                scores.update({first + " + " +  words: 0}) 

    print scores
我建议更改一些变量名,以区分每个嵌套列表中使用的单词。这样,就可以完全取消“first”变量

    scores = {}

    lst = ('human', 'loud', 'big')

    for word1 in list:
        for word2 in list:
            if word2 != word1:
                scores.update({word1 + " + " +  word2: 0}) 

    print scores

这是我的代码片段。我建议先创建字典,然后再添加到字典中

    scores = {}

    lst = ('human', 'loud', 'big')

    for words in list:
        first = words
        for words in list:
            if words != first:
                scores.update({first + " + " +  words: 0}) 

    print scores
我建议更改一些变量名,以区分每个嵌套列表中使用的单词。这样,就可以完全取消“first”变量

    scores = {}

    lst = ('human', 'loud', 'big')

    for word1 in list:
        for word2 in list:
            if word2 != word1:
                scores.update({word1 + " + " +  word2: 0}) 

    print scores

使用
itertools.compositions

from itertools import combinations as comb
l = ['human', 'loud', 'big']
scores = dict((" + ".join(pair), 0)
              for ordered_pair in comb(l, 2)
                for pair in (ordered_pair, reversed(ordered_pair)))

使用
itertools.compositions

from itertools import combinations as comb
l = ['human', 'loud', 'big']
scores = dict((" + ".join(pair), 0)
              for ordered_pair in comb(l, 2)
                for pair in (ordered_pair, reversed(ordered_pair)))

使用
itertools.compositions

from itertools import combinations as comb
l = ['human', 'loud', 'big']
scores = dict((" + ".join(pair), 0)
              for ordered_pair in comb(l, 2)
                for pair in (ordered_pair, reversed(ordered_pair)))

使用
itertools.compositions

from itertools import combinations as comb
l = ['human', 'loud', 'big']
scores = dict((" + ".join(pair), 0)
              for ordered_pair in comb(l, 2)
                for pair in (ordered_pair, reversed(ordered_pair)))

我有一个更紧凑的函数

import itertools
scores = {}
for a,b in itertools.permutations(('human', 'loud', 'big'), 2):
    scores["{0} + {1}".format(a,b)] = 0
print scores

我有一个更紧凑的函数

import itertools
scores = {}
for a,b in itertools.permutations(('human', 'loud', 'big'), 2):
    scores["{0} + {1}".format(a,b)] = 0
print scores

我有一个更紧凑的函数

import itertools
scores = {}
for a,b in itertools.permutations(('human', 'loud', 'big'), 2):
    scores["{0} + {1}".format(a,b)] = 0
print scores

我有一个更紧凑的函数

import itertools
scores = {}
for a,b in itertools.permutations(('human', 'loud', 'big'), 2):
    scores["{0} + {1}".format(a,b)] = 0
print scores

同样值得一提的是,
list
是一个错误的变量名,因为它与列表类型冲突。同样值得一提的是,
list
是一个错误的变量名,因为它与列表类型冲突。同样值得一提的是,
list
是一个错误的变量名,因为它与列表类型冲突
list
是一个不好的变量名,因为它与列表类型wow——super-fast冲突。这很有效。非常感谢。此外,如果您在for循环的顶部使用enumerate,您可以在第二个循环中迭代子列表,从索引+1开始,这样您就不必检查您是否是那个单词…因为您的单词是唯一的,对吗?哇,超快。这很有效。非常感谢。此外,如果您在for循环的顶部使用enumerate,您可以在第二个循环中迭代子列表,从索引+1开始,这样您就不会