记录一些数组、对象常用的代码段,经常切换语言写代码有点遭不住了,好记性不如烂笔头,随时整理随时补充吧
判断元素是否在数组中
Python3
pets = ['cat', 'dog', 'bat'];
assert ('cat' in pets) == True
Javascript
let pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
Java
ArrayList<String> pets = new ArrayList<String>(Arrays.asList("cat", "dog", "bat"));
System.out.println(pets.contains("cat"));
Rust
let pets = vec!["cat", "dog", "bat"];
assert_eq!(pets.contains(&"cat"), true);
// 或者遍历判断
assert_eq!(pets.iter().any(|v| v == &"cat"), true);
数组集合运算
Python3
a = [1, 2, 3]
b = [2, 4, 5]
# 并集
union = list(set().union(a, b))
# [1, 2, 3, 4, 5]
# 交集
intersection = [item for item in a if item in b]
# [2]
# 差集
difference = [item for item in a + b if item not in a or item not in b]
# [1, 3, 4, 5]
# 两个数组合并为一个新数组
all = a + b
# [1, 2, 3, 2, 4, 5]
Javascript
let a = [1, 2, 3]
let b = [2, 4, 5]
// 并集
let union = a.concat(b.filter(v => !a.includes(v)))
// [1, 2, 3, 4, 5]
// 交集
let intersection = a.filter(v => b.includes(v))
// [2]
// 差集
let difference = a.concat(b).filter(v => !a.includes(v) || !b.includes(v))
// [1, 3, 4, 5]
// 两个数组合并为一个新数组
let all = [...a, ...b]
// [1, 2, 3, 2, 4, 5]
Java
CollectionUtils 来自 commons-collections4,文档:CollectionUtils
ArrayList<Integer> a = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
ArrayList<Integer> b = new ArrayList<Integer>(Arrays.asList(2, 4, 5));
// 并集
List<Integer> union = new ArrayList<Integer>(CollectionUtils.union(a, b));
// [1, 2, 3, 4, 5]
// 交集
List<Integer> intersection = new ArrayList<Integer>(CollectionUtils.intersection(a, b));
// [2]
// 差集
List<Integer> difference = new ArrayList<Integer>(CollectionUtils.disjunction(a, b));
// [1, 3, 4, 5]
// 两个数组合并为一个新数组
List<Integer> all = new ArrayList<Integer>(CollectionUtils.collate(a, b));
// [1, 2, 2, 3, 4, 5]
Rust
用到了 array_tool 模块
use array_tool::vec::*;
use itertools::concat;
let a = vec![1, 2, 3];
let b = vec![2, 4, 5];
// 并集
let union = a.union(b.clone());
// [1, 2, 3, 4, 5]
// 交集
let intersect = a.intersect(b.clone());
// [2]
// 差集
let input = array_tool::uniques(a.clone(), b.clone());
let difference = concat(input);
// [1, 3, 4, 5]
// 两个数组合并为一个新数组
let mut all = a.clone();
all.extend(&b);
// [1, 2, 3, 2, 4, 5]
判断数组内是否包含值为某值的对象
Python3
persons = [{"name": "Harry"}, {"name": "Alice"}, {"name": "Peter"}];
# 判断是否存在
is_exist = any([p for p in persons if p["name"] == "Peter"])
# True
# 获取符合条件的对象
data = [p for p in persons if p["name"] == "Peter"]
# [ { name: 'Peter' } ]
Javascript
let persons = [{name: "Harry"}, {name: "Alice"}, {name: "Peter"}];
// 判断是否存在
let is_exist = persons.some(person => person.name === "Peter")
// true
// 获取符合条件的对象
let data = persons.filter(person => person.name === "Peter")
// [ { name: 'Peter' } ]
Java
List<HashMap<String, String>> persons = Arrays.asList(
new HashMap<String, String>() {{ put("name", "Harry"); }},
new HashMap<String, String>() {{ put("name", "Alice"); }},
new HashMap<String, String>() {{ put("name", "Peter"); }}
);
// 判断是否存在
Boolean isExist = persons.stream()
.anyMatch(obj -> obj.get("name").equals("Peter"));
// true
// 获取符合条件的对象
List<HashMap<String, String>> data = persons.stream()
.filter(obj -> obj.get("name").equals("Peter"))
.collect(Collectors.toList());
// [{name=Peter}]
Rust
let mut one = HashMap::new();
one.insert("name".to_string(), "Harry".to_string());
let mut two = HashMap::new();
two.insert("name".to_string(), "Alice".to_string());
let mut three = HashMap::new();
three.insert("name".to_string(), "Peter".to_string());
let persons: Vec<HashMap<String, String>> = vec![one, two, three];
// 判断是否存在
let is_exist: bool = persons.iter()
.any(|x| x.get("name") == Some(&"Peter".to_string()));
// true
// 获取符合条件的对象
let data: Vec<HashMap<String, String>> = persons.iter()
.filter(|&x| x.get("name") == Some(&"Peter".to_string()))
.cloned()
.collect();
// [{"name": "Peter"}]