【问题标题】:TypeError: '<' not supported between instances of 'set' and 'tuple'TypeError:'set'和'tuple'的实例之间不支持'<'
【发布时间】:2020-04-04 18:13:42
【问题描述】:

我不断收到以下错误:TypeError: '&lt;' not supported between instances of 'set' and 'tuple',我不知道为什么?

这是我的代码:

print(reduce(lambda x,y: x<y, set(list(map(tuple,list1))), set(list(map(tuple,list2)))))

【问题讨论】:

  • 这对我来说绝对不简单。
  • 向我们展示完整的错误回溯。

标签: python python-3.x list set higher-order-functions


【解决方案1】:

如果我对问题的理解正确,可能的简化是:

set(map(tuple, list1)).issubset(set(map(tuple, list2)))

或者使用你的符号:

set(map(tuple, list1)) < set(map(tuple, list2))

一些备注:

  • 无需构建这些中间列表。 set 可以接受迭代,就像 list 一样
  • 您只需要检查一次集合是否是另一个集合的子集。减少在这里真的没有意义

【讨论】:

  • 我不想用issubset函数,这里reduce的重点是返回一个布尔值
  • 这会返回一个布尔值。和子集与&lt;@misu 相同
猜你喜欢
  • 2020-11-15
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 2020-09-07
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
相关资源
最近更新 更多