【问题标题】:Map operation Last Element exceptionMap 操作 Last Element 异常
【发布时间】:2017-06-21 22:08:19
【问题描述】:
#Right now
list(map(lambda x: f1.write(x + ','),feature))
# Would like it to be:
list(map(lambda x: if(x = map.end) f1.write(x) else: f1.write(x),feature))

像上面的示例代码一样,我可以做些什么来排除或产生异常,以便地图的最后一个元素做其他事情

【问题讨论】:

标签: python list


【解决方案1】:

也许您只能在 feature[:-1] 上使用 map,它们是 feature 的所有元素,除了最后一个元素。 然后写最后一个元素:

编辑:因为特征是一个地图对象,我们之前将其转换为列表

feature = list(feature)
res = list(map(lambda x: f1.write(x + ','),feature[:-1]))
res.append(f1.write(feature[-1]))

【讨论】:

  • TypeError: 'map' 对象不可下标。我不认为你可以访问这样的地图,feature[:-1]
  • “特征”的类型是什么?
  • 特征类型是地图
  • 好吧,我不知道!我编辑了我的帖子,您可以先将其转换为列表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 2013-08-24
  • 1970-01-01
  • 2017-03-03
  • 2023-01-27
相关资源
最近更新 更多