data = {'results' => [1, 1, 1, 1, 0, 0, 1, 1, 1, 0],
'weather' => ["bad", "bad", "bad", "good", "good", "good", "good", "good", "bad", "bad"]}
结果有十个条目,天气有十个条目,它们是对齐的并且彼此对应。如何从变量数据中删除两个来自天气的条目,以及相应的结果条目,其中天气=="坏" ?
答案 0 :(得分:2)
如果我理解正确,你应该在weather
数组中找到第一次出现'bad',得到这个出现的索引并删除两个数组中的元素(并做两次):
data = {'results' => [1, 1, 1, 1, 0, 0, 1, 1, 1, 0],
'weather' => ["bad", "bad", "bad", "good", "good", "good", "good", "good", "bad", "bad"]}
2.times do
idx = data['weather'].index('bad')
data['weather'].delete_at(idx)
data['results'].delete_at(idx)
end
p data
结果:
{"results"=>[1, 1, 0, 0, 1, 1, 1, 0],
"weather"=>["bad", "good", "good", "good", "good", "good", "bad", "bad"]}
答案 1 :(得分:0)
以下作品:
index_array = []
counter = 0
data['weather'].each_with_index do |a,i|
if a=="bad" and counter !=2
data['results'][i] = nil
data['weather'][i] = nil
counter = counter + 1
end
end
data['results'] = data['results'].compact
data['weather'] = data['weather'].compact