有人可以建议如何构建一个模式来从这些数据中提取第一个连续数字列表吗?
sample = {52.2624, 54.4003, 60.7418, 61.3801, 62.6397, 61.7992,
63.2282, "", "", "", "", "", "", "", "", "", "", 62.3921, 61.897,
60.299, 59.053, 61.3778, 64.3724, 63.4251, 78.1912, 79.7451,
80.4741, "", 81.324, 79.9114, 93.7509};
我尝试过像sample //. {useable : _?NumberQ .., ___} -> {useable}
这样的变体无济于事。
useable = TakeWhile[sample, NumberQ]
效果很好,但我想知道如何使用模式匹配来做到这一点。
答案 0 :(得分:7)
试图保留你的逻辑:
sample /. {useable : Longest[_?NumberQ ..], ___} -> {useable}
如果您想要最长的数字序列:
sample /. {___, useable : Longest[_?NumberQ ..], ___} -> {useable}
修改
获取所有数字序列:
Cases[SplitBy[sample, NumberQ], {_?NumberQ ..}]
或
Last@Reap[sample //. {x___, useable : Longest[_?NumberQ ..], y___} :>
(Sow@{useable}; {x}~Join~{y})]
答案 1 :(得分:7)
另一种选择是寻找第一个非数字条目:
sample /. {useable___, _?(!NumberQ[#]&), ___} :> {useable}
答案 2 :(得分:6)
一种方法是
sample /. {Longest[useable___?NumberQ], ___} :> {useable}
从样本中返回{52.2624, 54.4003, 60.7418, 61.3801, 62.6397, 61.7992, 63.2282}
。