在Python中使用步骤阻止切片

我想知道是否有可能在python中对第n个块进行子索引。可以

import UIKit

class ProductCellVC: UICollectionViewCell
{
    @IBOutlet weak var productImage: UIImageView!

    override func awakeFromNib()
    {
        super.awakeFromNib()

        self.layer.borderWidth = 1
        self.layer.cornerRadius = 25
    }
}

给出第二个值

to have one row of products,and  the width of each cell (product) in the

但是如果我想要类似的东西怎么办?

a = [1,2,3,4,5,6,7,8]
a[::2]

所以基本上每个长度为2的第二个块都可以通过某种方式建立子索引吗?

qq2290279 回答:在Python中使用步骤阻止切片

您可以这样解决

import itertools
a = [1,2,3,4,5,6,7,8]
b = list(itertools.chain(*[a[i:i+2] for i in range(0,len(a),2)][::2]))
assert b == [1,6]
,

有趣的问题。

您可以执行以下操作,但会丢失数组的顺序

a[::4] + a[1::4]

输出:

[1,6]

使用numpy,您可以创建具有以下模数的蒙版:

import numpy as np
mod4 = np.arange(len(a)) % 4
np.array(a)[(mod4==0) | (mod4==1)]

输出:

array([1,6])
本文链接:https://www.f2er.com/3158570.html

大家都在问