包含字符串和整数的二维列表

时间:2019-03-01 19:17:10

标签: python python-3.x

我有一个二维列表;即一个包含整数和字符串的嵌套列表。

mylist = [[92, "james"], [88, "john"], [75, "robert"], [80, "michael"], [25, "william"], [54, "david"], [59, "richard"], [74, "charles"], [46, "joseph"], [90, "thomas"], [80, "christopher"]]

我只想找到一种方法来检查字符串,检查它们是否以特定字母开头;说j,将其删除,然后忽略其余不符合此条件的元素。

所需的结果将如下所示:

mylist = [[92, "ames"], [88, "ohn"], [75, "robert"], [80, "michael"], [25, "william"], [54, "david"], [59, "richard"], [74, "charles"], [46, "oseph"], [90, "thomas"], [80, "christo, her"]]

2 个答案:

答案 0 :(得分:1)

如果我对您的理解正确,这是使用list comprehension的解决方案:

<body>
    <h1>Life in The Wild.</h1>
    <div class="container">
        <img src="https://static.pexels.com/photos/52500/horse-herd-fog-nature-52500.jpeg">
        <img src="https://static.pexels.com/photos/66898/elephant-cub-tsavo-kenya-66898.jpeg">
        <img src="https://static.pexels.com/photos/213399/pexels-photo-213399.jpeg">
        <img src="https://static.pexels.com/photos/158471/ibis-bird-red-animals-158471.jpeg">
        <img src="https://static.pexels.com/photos/133459/pexels-photo-133459.jpeg">
        <img src="https://static.pexels.com/photos/50988/ape-berber-monkeys-mammal-affchen-50988.jpeg">
    </div>

</body>

答案 1 :(得分:0)

您可以使用函数lstrip()删除前导字符:

[[i, j.lstrip('j')] for i, j in mylist]