strL = [['a'], ['b', 'c'], ['d']]
words = ['a', 'b', 'c']
dirc = {}
for word in words:
dirc[word] = set() ## “set()” 换成“{}”会报错“'dict' object has no attribute 'add'”,看来{}表示空字典而不是空集
for (index, value) in enumerate(strL):
if word in value:
dirc[word] = dirc[word].add(index)
以上代码提示“'NoneType' object has no attribute 'add'”, 问题在哪里?
另外把列表转集合应该是set()函数,可是:
a = {1 : [1, 2, 3, 4], 2 : [1, 2] }
{key:set(value) for (key, value) in a}
提示“too many values to unpack (expected 2)”,想要的结果是
{1 : {1, 2, 3, 4}, 2 : {1, 2} },求错在哪里?
words = ['a', 'b', 'c']
dirc = {}
for word in words:
dirc[word] = set() ## “set()” 换成“{}”会报错“'dict' object has no attribute 'add'”,看来{}表示空字典而不是空集
for (index, value) in enumerate(strL):
if word in value:
dirc[word] = dirc[word].add(index)
以上代码提示“'NoneType' object has no attribute 'add'”, 问题在哪里?
另外把列表转集合应该是set()函数,可是:
a = {1 : [1, 2, 3, 4], 2 : [1, 2] }
{key:set(value) for (key, value) in a}
提示“too many values to unpack (expected 2)”,想要的结果是
{1 : {1, 2, 3, 4}, 2 : {1, 2} },求错在哪里?