Python多列表理解

时间:2018-04-25 02:02:43

标签: python list-comprehension

<form action="EmployeeControllerServlet"> <!-- only use one form -->
    <table>
        <tbody>
            <tr>
                <td><label>Branch </label></td>
                <td>
                        <select name="uBranch">
                            <option value="${loadedEmployee.branch}"></option>
                            <option value="IT">IT</option>
                            <option value="ACC">ACC</option>
                            <option value="HR">HR</option>
                        </select>
                </td>
            </tr>
            <tr>
                <td><label>Privilege </label></td>
                <td>
                        <select name="userType">
                            <option value="${loadedEmployee.userType}"></option>
                            <option value="regular">REGULAR</option>
                            <option value="admin">ADMIN</option>
                        </select>
                </td>
            </tr>
        </table>
    </tbody>
</form>

如何使用列表理解获得def test(): return 1,2 a, b = [test() for _ in range(5)] a=[1,1,1,1,1]

1 个答案:

答案 0 :(得分:0)

zip()就是关键所在:

代码:

a, b = [list(x) for x in zip(*[test() for _ in range(5)])]

测试代码:

def test():
    return 1, 2

a, b = [list(x) for x in zip(*[test() for _ in range(5)])]
print(a, b)

结果:

[1, 1, 1, 1, 1] [2, 2, 2, 2, 2]