list - 如何通过列表中的索引从元组中选择元素?
问题描述
我想从列表中选择元素,[[1,2],[3,4],[5,6]] 第一个,第二个,第一个等等。我想我可以使用 zip 在对前面添加一个计数器并使用模数来选择零件,现在我的列表如下所示:
let a = [(0,[1,2]),(1,[3,4]),(2,[5,6]),(3,[7,8]),(4,[9,10])]
但是我现在如何选择元素?
伪代码将是
for each tuple in list:
first part of tuple is the selector, second part is the pair
if selector mod 2 : choose pair[0] else choose pair[1]
列表 a 的输出应为:1,4,5,7,9
解决方案
也许:
> zipWith (!!) [[1,2],[3,4],[5,6],[7,8],[9,10]] (cycle [0,1])
[1,4,5,8,9]
如果您知道您正在使用内部长度为 2 的列表,那么您可能应该使用对。
> zipWith ($) (cycle [fst, snd]) [(1,2),(3,4),(5,6),(7,8),(9,10)]
[1,4,5,8,9]
推荐阅读
- python - 如何同时合并 asyncio websockets 的输出?
- reactjs - 多部分:未找到边界 - 上传文件(使用 multer)
- python - 如何在 Python 中使用 3 个功能在 MLPClassifier 上绘制决策区域?
- jena - Fuseki DB 文件夹包含哪些信息
- javascript - 将 Google 工作表中的日期区域设置从 Jalali 日历转换为公历
- mysql - 返回属于同一产品的图像 - SQL SELF JOIN
- javascript - 在 NUXT 中使用 Embed.js
- ajax - 从 javascript 调用页面方法的问题
- jenkins - 如何调用java文件及其函数并从Jenkins的groovy文件中解析它们
- node.js - Node.js/http express post 方法失败,出现 404