如何读取文件并跳过一些空格
这与我的上一个问题类似,
但是有是另一个即兴创作,如果我想跳过一些空格,代码是怎样的,在这种情况下是“输入”,例如:
5 5 10
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
<- this white space
3 4 4
1 2 3 4
1 2 3 4
1 2 3 4
我尽力但找不到如何跳过空格 谢谢你的帮助:)
这就是答案,感谢拉蒙:
let readMap (path:string) =
let lines = File.ReadAllLines path
let [|x; y; n|] = lines.[0].Split() |> Array.map int
let data =
[|
for l in (lines |> Array.toSeq |> Seq.skip 1 |> Seq.filter(System.String.IsNullOrEmpty >> not)) do
yield l.Split()
|]
x,y,n,data
this is similiar to my previous question,
but there is another improvisation, how is the code if i want to skip some white spaces, for this case is "enter", for example:
5 5 10
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
<- this white space
3 4 4
1 2 3 4
1 2 3 4
1 2 3 4
i try to my best but couldn't find how to skip the white space
thank you for the help :)
This is the answer, thanks to Ramon :
let readMap (path:string) =
let lines = File.ReadAllLines path
let [|x; y; n|] = lines.[0].Split() |> Array.map int
let data =
[|
for l in (lines |> Array.toSeq |> Seq.skip 1 |> Seq.filter(System.String.IsNullOrEmpty >> not)) do
yield l.Split()
|]
x,y,n,data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写
readMap
函数的另一种方法是在列表理解中使用if
表达式。我认为如果您使用推导式,这实际上更具可读性(因为您不必结合两种编写方式):我还删除了对 Array.toSeq 的调用,因为 F# 允许您在不需要显式转换的情况下在需要 seq 的地方使用 array(seq 实际上是 IEnumerable 并且 array 实现了它)。
Another way to write your
readMap
function is to useif
expression inside the list comprehension. I think this is actually more readable if you're using comprehensions (because you don't have to combine two ways of writing things):I also removed the call to
Array.toSeq
, because F# allows you to use array in a place where seq is expected without an explicit conversion (seq is actuallyIEnumerable
and array implements it).这个怎么样:
?
What about this:
?