首页 > 解决方案 > Python数组跳过索引的问题

问题描述

我有一个在数组中跳过索引的问题,如下所示 -


    start = linesa.index("enum {\n")

    print(linesa)
    i = start
    while True:
        print(i)
        print(linesa[i])
        if "};\n" in linesa[i]:
            break
        print(linesa.pop(i))
        i += 1
    print(linesa)
    exit()

索引 39 是枚举{,而 40 是};但它跳过了 '\tID_CA_REQ_GAME_GUARD_CHECK = 0x0258\n',索引

['/***************************************************\n', ' *       _   _            _                        *\n', ' *      | | | |          (_)                       *\n', ' *      | |_| | ___  _ __ _ _______  _ __          *\n', " *      |  _  |/ _ \\| '__| |_  / _ \\| '_  \\        *\n", ' *      | | | | (_) | |  | |/ / (_) | | | |        *\n', ' *      \\_| |_/\\___/|_|  |_/___\\___/|_| |_|        *\n', ' ***************************************************\n', ' * This file is part of Horizon (c).\n', ' * Copyright (c) 2019 Horizon Dev Team.\n', ' *\n', ' * Base Author - Sagun Khosla. (sagunxp@gmail.com)\n', ' *\n', ' * This library is free software; you can redistribute it and/or modify\n', ' * it under the terms of the GNU General Public License as published by\n', ' * the Free Software Foundation, either version 3 of the License, or\n', ' * (at your option) any later version.\n', ' *\n', ' * This library is distributed in the hope that it will be useful,\n', ' * but WITHOUT ANY WARRANTY; without even the implied warranty of\n', ' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n', ' * GNU General Public License for more details.\n', ' *\n', ' * You should have received a copy of the GNU General Public License\n', ' * along with this library.  If not, see <http://www.gnu.org/licenses/>.\n', ' **************************************************/\n', '\n', '#ifndef HORIZON_AUTH_CA_REQ_GAME_GUARD_CHECK_HPP\n', '#define HORIZON_AUTH_CA_REQ_GAME_GUARD_CHECK_HPP\n', '\n', '#include "Server/Common/PacketBuffer.hpp"\n', '\n', 'namespace Horizon\n', '{\n', 'namespace Auth\n', '{\n', 'namespace Packet\n', '{\n', '\n', 'enum {\n', '\tID_CA_REQ_GAME_GUARD_CHECK = 0x0258\n', '};\n', '/**\n', ' * @brief Main object for the aegis packet: CA_REQ_GAME_GUARD_CHECK\n', ' * Size : 2 @ 0\n', ' *\n', ' */ \n', 'class CA_REQ_GAME_GUARD_CHECK : public PacketBuffer\n', '{\n', 'public:\n', '\tCA_REQ_GAME_GUARD_CHECK() : Packet(ID_CA_REQ_GAME_GUARD_CHECK) { }\n', '\t~CA_REQ_GAME_GUARD_CHECK() { }\n', '\n', '\tvirtual CA_REQ_GAME_GUARD_CHECK *serialize()\n', '\t{\n', '\t\treturn this;\n', '\t}\n', '\n', '\tvirtual void deserialize(PacketBuffer &/*buf*/) { }\n', '\n', '\tvirtual CA_REQ_GAME_GUARD_CHECK *operator << (PacketBuffer &right)\n', '\t{\n', '\t\tdeserialize(right);\n', '\t\treturn this;\n', '\t}\n', '\n', '\tvirtual CA_REQ_GAME_GUARD_CHECK *operator >> (PacketBuffer &right)\n', '\t{\n', '\t\treturn right = serialize();\n', '\t}\n', '\n', 'protected:\n', '\t/* Structure Goes Here */\n', '};\n', '}\n', '}\n', '}\n', '#endif /* HORIZON_AUTH_CA_REQ_GAME_GUARD_CHECK_HPP */']
39
enum {

enum {

40
};

['/***************************************************\n', ' *       _   _            _                        *\n', ' *      | | | |          (_)                       *\n', ' *      | |_| | ___  _ __ _ _______  _ __          *\n', " *      |  _  |/ _ \\| '__| |_  / _ \\| '_  \\        *\n", ' *      | | | | (_) | |  | |/ / (_) | | | |        *\n', ' *      \\_| |_/\\___/|_|  |_/___\\___/|_| |_|        *\n', ' ***************************************************\n', ' * This file is part of Horizon (c).\n', ' * Copyright (c) 2019 Horizon Dev Team.\n', ' *\n', ' * Base Author - Sagun Khosla. (sagunxp@gmail.com)\n', ' *\n', ' * This library is free software; you can redistribute it and/or modify\n', ' * it under the terms of the GNU General Public License as published by\n', ' * the Free Software Foundation, either version 3 of the License, or\n', ' * (at your option) any later version.\n', ' *\n', ' * This library is distributed in the hope that it will be useful,\n', ' * but WITHOUT ANY WARRANTY; without even the implied warranty of\n', ' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n', ' * GNU General Public License for more details.\n', ' *\n', ' * You should have received a copy of the GNU General Public License\n', ' * along with this library.  If not, see <http://www.gnu.org/licenses/>.\n', ' **************************************************/\n', '\n', '#ifndef HORIZON_AUTH_CA_REQ_GAME_GUARD_CHECK_HPP\n', '#define HORIZON_AUTH_CA_REQ_GAME_GUARD_CHECK_HPP\n', '\n', '#include "Server/Common/PacketBuffer.hpp"\n', '\n', 'namespace Horizon\n', '{\n', 'namespace Auth\n', '{\n', 'namespace Packet\n', '{\n', '\n', '\tID_CA_REQ_GAME_GUARD_CHECK = 0x0258\n', '};\n', '/**\n', ' * @brief Main object for the aegis packet: CA_REQ_GAME_GUARD_CHECK\n', ' * Size : 2 @ 0\n', ' *\n', ' */ \n', 'class CA_REQ_GAME_GUARD_CHECK : public PacketBuffer\n', '{\n', 'public:\n', '\tCA_REQ_GAME_GUARD_CHECK() : Packet(ID_CA_REQ_GAME_GUARD_CHECK) { }\n', '\t~CA_REQ_GAME_GUARD_CHECK() { }\n', '\n', '\tvirtual CA_REQ_GAME_GUARD_CHECK *serialize()\n', '\t{\n', '\t\treturn this;\n', '\t}\n', '\n', '\tvirtual void deserialize(PacketBuffer &/*buf*/) { }\n', '\n', '\tvirtual CA_REQ_GAME_GUARD_CHECK *operator << (PacketBuffer &right)\n', '\t{\n', '\t\tdeserialize(right);\n', '\t\treturn this;\n', '\t}\n', '\n', '\tvirtual CA_REQ_GAME_GUARD_CHECK *operator >> (PacketBuffer &right)\n', '\t{\n', '\t\treturn right = serialize();\n', '\t}\n', '\n', 'protected:\n', '\t/* Structure Goes Here */\n', '};\n', '}\n', '}\n', '}\n', '#endif /* HORIZON_AUTH_CA_REQ_GAME_GUARD_CHECK_HPP */']

标签: pythonarraysalgorithm

解决方案


使用 print(linesa[i]) 代替 print(linesa.pop(i))。如果要打印最后一个元素,请使用linesa[-1]

Pop 会减少linesa 的长度,并会导致循环内的尴尬行为。


推荐阅读