mclb 发表于 2021-2-3 09:43:13

求大佬帮解答


blocks = {'itemName': 'minecraft:glass', 'auxValue': 0}
blockSet = set(blocks.items())

playItemList = [{'itemId': 395, 'count': 1, 'enchantData': [], 'durability': 0, 'customTips': '', 'extraId': '', 'modId': '', 'modItemId': '', 'itemName': 'minecraft:glass', 'auxValue': 0, 'showInHand': True},
{'itemId': 806, 'count': 1, 'enchantData': [], 'durability': 15, 'customTips': '', 'extraId': '', 'modId': '', 'modItemId': '', 'itemName': 'lbmod_builder_wands:lb_item_wand_stone', 'auxValue': 0, 'showInHand': True},
None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

for playItemDict in playItemList:
    print type(playItemDict)
    playItemSet = set(playItemDict.items())
    if not(playItemDict == None):
               if blockSet.issubset(playItemSet):
                        print "背包里有此方块"


playItemDict明明是字典类型,为什么playItemSet = set(playItemDict.items())为什么报错为列表?

逃兵 发表于 2021-2-3 09:43:14

本帖最后由 逃兵 于 2021-2-3 11:19 编辑

mclb 发表于 2021-2-3 11:07
我是想问,那啥set不是可以将列表转化为集合吗?blocks不就成功了吗?

列表是可以的,但是里面嵌套另一个列表就不行了
>>> temp =
>>> set(temp)
{1, 2, 3}
>>> temp = [,]
>>> set(temp)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
    set(temp)
TypeError: unhashable type: 'list'
>>> temp = ]
>>> set(temp)
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
    set(temp)
TypeError: unhashable type: 'list'


>>> temp = [(1,2),(3,4)]
>>> set(temp)
{(1, 2), (3, 4)}
>>> temp = [(1,2),]
>>> set(temp)
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
    set(temp)
TypeError: unhashable type: 'list'

逃兵 发表于 2021-2-3 10:34:32

playItemDict.items()不是一围的,里面嵌套list了
dict_items([('itemId', 395), ('count', 1), ('enchantData', []), ('durability', 0), ('customTips', ''), ('extraId', ''), ('modId', ''), ('modItemId', ''), ('itemName', 'minecraft:glass'), ('auxValue', 0), ('showInHand', True)])

mclb 发表于 2021-2-3 11:07:08

逃兵 发表于 2021-2-3 10:34
playItemDict.items()不是一围的,里面嵌套list了
dict_items([('itemId', 395), ('count', 1), ('enchant ...

我是想问,那啥set不是可以将列表转化为集合吗?blocks不就成功了吗?

mclb 发表于 2021-2-3 11:25:09

那算了,我换个判断
页: [1]
查看完整版本: 求大佬帮解答