gpt4 book ai didi

julia - 扫描文本文件每行最多 N 个单词,用 Julia 中的单词列和行填充数组。事先不知道字数

转载 作者:行者123 更新时间:2023-12-02 18:04:39 25 4
gpt4 key购买 nike

我只有部分解决方案

data = split.(readlines(file))
a = map(x -> x[1],data)
b = map(x -> x[2],data)
c = map(x -> x[3],data)
d = map(x -> x[4],data)
e = map(x -> x[5],data)
f = map(x -> x[6],data)

但仅当所有输入行都包含完整的预期单词数时,它才有效。不幸的是,行可以更短,我想用空字符串“”或一些原生的 Julia 优点来填充缺失值的“空词”。

可以看出,我是 Julia 新手。

谢谢

最佳答案

这是一个如何做到这一点的示例(我添加了注释和解释):

julia> str = """
a b c
a b
a b c d
a
a b c d e
""" # source data
"a b c\na b\na b c d\na\na b c d e\n"

julia> data = split.(readlines(IOBuffer(str))) # use IOBuffer to turn string into file-like object
5-element Vector{Vector{SubString{String}}}:
["a", "b", "c"]
["a", "b"]
["a", "b", "c", "d"]
["a"]
["a", "b", "c", "d", "e"]

julia> res = fill("", length(data), maximum(length, data)) # pre-fill matrix with "" as you requested - this should probably be easiest for you to work with
5×5 Matrix{String}:
"" "" "" "" ""
"" "" "" "" ""
"" "" "" "" ""
"" "" "" "" ""
"" "" "" "" ""

julia> foreach(((out, in),) -> copyto!(out, in), zip(eachrow(res), data)) # fill each row of res with consecutive elements of data using copyto!

julia> res # the result you wanted is now in res
5×5 Matrix{String}:
"a" "b" "c" "" ""
"a" "b" "" "" ""
"a" "b" "c" "d" ""
"a" "" "" "" ""
"a" "b" "c" "d" "e"

如有不清楚的地方请评论。因此,您将得到一个矩阵,其每一行都是源数据的行。

按键操作的更高级模式是:

foreach(Base.splat(copyto!), zip(eachrow(res), data))

如果您都不清楚,您也可以选择双循环,例如:

for i in 1:length(data)
row = data[i]
for j in 1:length(row)
res[i, j] = row[j]
end
end

关于julia - 扫描文本文件每行最多 N 个单词,用 Julia 中的单词列和行填充数组。事先不知道字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73572429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com