假设给出了两个元素列表,A和B。我有兴趣检查是否A包含B. 具体来说,元素必须以相同的顺序出现,并且它们不需要是连续的。如果是这种情况,我们说它B是 的子序列A。
这里有些例子:
A = [4, 2, 8, 2, 7, 0, 1, 5, 3]
B = [2, 2, 1, 3]
is_subsequence(A, B) # True
Run Code Online (Sandbox Code Playgroud)
A = [4, 2, 8, 2, 7, 0, 1, 5, 3]
B = [2, 8, 2]
is_subsequence(A, B) # True
Run Code Online (Sandbox Code Playgroud)
A = [4, 2, 8, 2, 7, 0, 1, 5, 3]
B = [2, 1, 6]
is_subsequence(A, B) # False
Run Code Online (Sandbox Code Playgroud)
A = [4, …Run Code Online (Sandbox Code Playgroud) I am trying to complete a coding challenge which requires me to count all of the digits between 1 and X, where can reach up to 400,000,000,000,000,000
My approach was to iterate over a list and add the amount of digits to my result in the end, like so:
def page_digits(pages):
list_of_pages = list(range(1, pages + 1))
res = 0
for num in list_of_pages:
res += len((str(num)))
return res
Run Code Online (Sandbox Code Playgroud)
but obviously creating a list of every digit between 1 and …
我对编程(Python)很陌生,所以我不明白这里发生了什么。我有一个图像 (35x64) 作为 3D 数组,并使用 np.mean(),我尝试提取一行的一个颜色通道的平均值:
print(np.mean(img[30][:][0]))
Run Code Online (Sandbox Code Playgroud)
为了进行比较,我还编写了一个 for 循环来在列表中附加完全相同的值并用其计算平均值:
for i in range(64):
img_list.append(img[30][i][0])
print(np.mean(img_list))
Run Code Online (Sandbox Code Playgroud)
现在,由于一个奇怪的原因,它给出了不同的值:
First output: 117.1
Second output: 65.7
Run Code Online (Sandbox Code Playgroud)
通过查看列表,我发现第二个是正确的。有更多经验的人可以向我解释为什么会发生这种情况以及如何解决这个问题吗?我不想在程序中使用第二个更长的代码块,但正在寻找能够提供正确值的单行解决方案。
我有一个二维数组arr。这是一个例子:
>> arr
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Run Code Online (Sandbox Code Playgroud)
我还有一个二维索引数组indices(带有indices.shape[0] == arr.shape[0]、 和indices.shape[1] <= arr.shape[1])。这是一个例子:
>>> indices
array([[0, 1],
[1, 2],
[0, 2],
[0, 2]])
Run Code Online (Sandbox Code Playgroud)
现在,我想将 的一些元素设置arr为-1。具体来说,在 的第一行上arr,第一个和第二个元素应设置为 -1(因为indices[0] == [0, 1])。在 的第二行中arr,第二个和第三个元素应设置为 -1(因为indices[1] == [1, 2])。等等。
这将是预期的结果:
array([[ -1, -1, 2],
[ 3, -1, -1],
[ -1, 7, -1],
[ -1, …Run Code Online (Sandbox Code Playgroud) 我正面临熊猫的问题。输入数据是单列:
MixedColumn
-------------
20_5, 20_5**1
20_7**9
20_4, 40_4, 15_4**2
Run Code Online (Sandbox Code Playgroud)
我想拆分并将其转换为这样的内容:
Col1 Col2
--------------
20_5 1
20_5 1
20_7 9
20_4 2
40_4 2
15_4 2
Run Code Online (Sandbox Code Playgroud)
该逻辑基于逗号(如果存在)拆分每个行项目(20_5、20_5)并将它们放置在同一列(Col1)的下一行。以及基于 **拆分每个行项目(**1)并将它们与单独列 (Col2) 中的各个值相关联。
对不起,如果这是一个菜鸟问题。任何提示肯定会帮助我。谢谢并祝大家假期愉快。
我有字典,里面有字典列表
字典列表有另一个字典列表
我需要提取值并将其附加到列表中
我需要写一个函数。如果孩子有name附加到父列表,以便任何字典传递函数列表将创建一个输出,如下所示
a = [{"id": "1", "Area": [{"id": "2", "name": "Clinical"},
{"id": "23", "name": "Delivery"}]},
{"id": "2", "Area": [{"id": "2", "name": "Clinical"},
{"id": "23", "name": "Delivery"}]}]
Run Code Online (Sandbox Code Playgroud)
预期输出:
[{"id": "1", "Area": ["Clinical", "Delivery"]},
{"id": "2", "Area": ["Clinical", "Delivery"]}]
Run Code Online (Sandbox Code Playgroud)
代码如下
result = []
temp = {}
for i in range(0,len(a)):
templist = []
b = a[i]['Area'][i]['name']
c = a[i]['id']
temp['id'] = c
templist.append(b)
temp['Area'] = templist
result.append(temp)
print (result)
Run Code Online (Sandbox Code Playgroud)
我的输出没有提取并放入列表?
python ×6
numpy ×2
algorithm ×1
arrays ×1
dictionary ×1
image ×1
mean ×1
pandas ×1
python-3.x ×1