轻松过滤数据:Julia 中的提示和技巧

轻松过滤数据:Julia 中的提示和技巧
data_list = [1, 4, -5, 11, -7, 4, 3, -1]

值大于零

positives = [n for n in data_list if n > 0]
#positives = filter(>(0),data_list)
println(positives)

值小于零

negatives = [n for n in data_list if n < 0]
#negatives = filter(<(0),data_list)
println(negatives)

负值替换为 0

neg_to_zero = [n > 0 ? n : 0 for n in data_list]
println(neg_to_zero)

正值替换为 0

pos_to_zero = [n < 0 ? n : 0 for n in data_list]
println(pos_to_zero)

过滤到一个范围

rang = filter(x-> -3<x<1, data_list) #between -3 and 1
println(rang)

限制在低值和高值之间

clamp.(data_list,-2,4)
8-element Vector{Int64}:
  1
  4
 -2
  4
 -2
  4
  3
 -1

根据其他列表中的值从列表中选择值

addresses = [
    "5412 N Alappuzha",
    "5148 N Munnar", 
    "5800 E Kovalam",
    "2122 N Palakkad",
    "5645 N Wayanad",
    "1060 W Kumarakom",
    "4801 N Murukani",
    "1039 W Kollengode",
]

counts = [0, 3, 10, 4, 1, 7, 6, 1]

more5 = [n > 5 for n in counts]
a = addresses[more5]
a = [addresses[i] for i in eachindex(more5) if true == more5[i]]
a = [a for (a, c) in Iterators.zip(addresses, counts) if c > 5]
a = addresses[filter(i -> counts[i] > 5, 1:length(counts))]
a = addresses[counts .> 5]
println(a)

给TA打赏
共{{data.count}}人
人已打赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索