我正在尝试将两个数组一起映射到字典列表。像这样:
a_list = [1, 2, 3]
b_list = ["a", "b", "c"]
d = [{"key1": a, "key2": b} for a in a_list for b in b_list]
>> [{"key1": 1, "key2": "a"}, {"key1": 2, "key2": "b"}, {"key1": 3, "key2": "c"}]
但是,这给出了:
[{"key1": 1, "key2": "a"}, {"key1": 1, "key2": "b"}, [...] {"key1": 3, "key2": "b"}, {"key1": 3, "key2": "c"}]
我尝试用for
和and
替换第二个,
,以及将代码的一部分移到括号中来回移动。
答案 0 :(得分:2)
使用def count_consecutive(arr, n):
# pad a with False at both sides for edge cases when array starts or ends with n
d = np.diff(np.concatenate(([False], arr == n, [False])).astype(int))
# subtract indices when value changes from False to True from indices where value changes from True to False
return np.flatnonzero(d == -1) - np.flatnonzero(d == 1)
count_consecutive(a, 5)
# array([2, 1, 3])
zip