我有一个文本文件,必须进行处理.这些行包含国家名称和一些州,如下所示.我想把它放在一个数组中.是否有可以实施的馆藏?
if(line.contains("India")){
//add their states to the collections
}
Run Code Online (Sandbox Code Playgroud)
印度
卡纳塔克邦
泰米尔纳德
安德拉邦
巴基斯坦
卡拉奇
拉合尔
澳大利亚
堪培拉
Adelide
美国
纽约
华盛顿
新的Jersy
我有一个名为 Record 的模型,属于 Product 模型,
所述price列类型是十六进制的。Rails 已经将其转换为字符串。但我想在我的控制台查询中获取它们。
pluck 的示例代码:
Product.first.records.pluck(:price)
Run Code Online (Sandbox Code Playgroud)
此查询将数组中的值显示为十六进制。有一种方法to_sentences 需要 pluck 值,但对我来说还不够。
问题在collect方法上是一样的:
Product.first.records.collect(&:price)
Run Code Online (Sandbox Code Playgroud)
将我的十六进制数据显示为字符串数组的 pluck 查询是什么,例如:
["45,46","75,42"]
Run Code Online (Sandbox Code Playgroud) 给出如下列表:
val dane = List(
("2011-01-04", -137.76),
("2011-01-04", 2376.45),
("2011-01-04", -1.70),
("2011-01-04", -1.70),
("2011-01-04", -1.00),
// ... skip a few ...
("2011-12-22", -178.02),
("2011-12-29", 1800.82),
("2011-12-23", -83.97),
("2011-12-24", -200.00),
("2011-12-24", -30.55),
("2011-12-30", 728.00)
)
Run Code Online (Sandbox Code Playgroud)
我想01按照指定的顺序使用以下操作对特定月份(例如1月或1月)的值(即内部列表的第二项)求和:
groupByslicecollectsum编译如下:
pub fn build_proverb(list: &[&str]) -> String {
if list.is_empty() {
return String::new();
}
let mut result = (0..list.len() - 1)
.map(|i| format!("For want of a {} the {} was lost.", list[i], list[i + 1]))
.collect::<Vec<String>>();
result.push(format!("And all for the want of a {}.", list[0]));
result.join("\n")
}
Run Code Online (Sandbox Code Playgroud)
以下不是(请参见Playground):
pub fn build_proverb(list: &[&str]) -> String {
if list.is_empty() {
return String::new();
}
let mut result = (0..list.len() - 1)
.map(|i| format!("For want of a {} the {} was lost.", …Run Code Online (Sandbox Code Playgroud)