给定另一个索引号列表,将数字插入列表

时间:2019-07-25 19:33:14

标签: python list

我正在根据索引号列表(indexNumberList)将数字列表(firstNumberList)插入另一个列表(secondNumberList)中的特定点。

code block

我尝试了以下列表理解,但返回了indexNumberList = [1, 2, 5, 8] firstNumberList = [0, 0, 0, 0] secondNumberList = [ 3, 1, 3, 2, 4, 1, 1, 4, 4, 4, 9, 9, 12, 12, 18, 19, 18, 9]

[none, none, none, none]

最终输出应如下所示

result =[secondNumberlist.insert(indexNumberList[elem],firstNumberList[elem]) for elem in range(len(indexNumberList))]

2 个答案:

答案 0 :(得分:3)

bool pairsDivisible(int* nums, int count, int k) {
    if(count == 0) return true;
    if(count % 2 != 0) return false; // count must be even

    // 1.
    int n0 = nums[0]; 

    // 2.
    for(int i = 1; i < count; i++) {
        int n1 = nums[i]; 

        // 3.
        if(isDivisible(n0 + n1, k)) {
            // Move the ith number so it's now nums[1]
            std::swap(nums[1], nums[i]); 

            if(pairsDivisible(nums + 2, count - 2, k)) {
                return true; // 3.a
            } else {
                // Reset the array
                std::swap(nums[1], nums[i]); 
            }
        }
    }
    return false; 
}

打印:

indexNumberList = [1, 2, 5, 8]
firstNumberList = [0, 0, 0, 0]
secondNumberList = [3, 1, 3, 2, 4, 1, 1, 4, 4, 4, 9, 9, 12, 12, 18, 19, 18, 9]

for i, v in zip(indexNumberList, firstNumberList):
    secondNumberList = secondNumberList[:i] + [v] + secondNumberList[i:]

print(secondNumberList)

编辑:带有[3, 0, 0, 1, 3, 0, 2, 4, 0, 1, 1, 4, 4, 4, 9, 9, 12, 12, 18, 19, 18, 9] 的版本(感谢@Chris_Rands):

list.insert

答案 1 :(得分:0)

如果您已经安装了numpy,则最好阅读并遵循:

1      = Send messages
1 << 1 = Kick users
1 << 2 = Ban users
1 << 3 = Invite users
...
1 << 63 = Delete the server