小编Cra*_*aig的帖子

如何使用枚举向后计数?

letters = ['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)

假设这是我的清单.哪里for i, letter in enumerate(letters)会:

0, a
1, b
2, c
Run Code Online (Sandbox Code Playgroud)

我怎样才能使它向后枚举,如:

2, a
1, b
0, c
Run Code Online (Sandbox Code Playgroud)

python python-3.x

6
推荐指数
2
解决办法
2124
查看次数

在Python 2.7中使用加号(+)连接大括号({})和`format`是否合适?

我正在尝试编写干净简洁的代码,并且在很多代码中,我看到有时候我看到人们的代码不一致.我问的是,有没有这样的实例

print("Cars on the road: " + cars)

比这更合适

print("Cars on the road: {}".format(cars))

或者只是一个偏好的问题?

python string concatenation string-formatting python-2.7

5
推荐指数
1
解决办法
96
查看次数

在 BS4 中使用 find_all 获取文本作为列表

首先我要说的是我对 Python 很陌生。我一直在使用 discord.py 和 Beautiful Soup 4 构建一个 Discord 机器人。这就是我现在的位置:

@commands.command(hidden=True)
async def roster(self):
    """Gets a list of CD's members"""
    url = "http://www.clandestine.pw/roster.html"
    async with aiohttp.get(url) as response:
        soupObject = BeautifulSoup(await response.text(), "html.parser")
    try:
        text = soupObject.find_all("font", attrs={'size': '4'})
        await self.bot.say(text)
    except:
        await self.bot.say("Not found!")
Run Code Online (Sandbox Code Playgroud)

这是输出: http://puu.sh/uycBF/1efe173437.png

现在,我尝试使用get_text()多种不同的方式从这段代码中删除括号和 HTML 标签,但每次都会抛出错误。我如何才能实现这一目标或将这些数据输出到数组或列表中,然后只打印纯文本?

python beautifulsoup discord

4
推荐指数
1
解决办法
1万
查看次数

是否有更多Pythonic方法来迭代通过字典键查找值而不是这个?

这是一个示例字典,colors:

{
    "Red" : {
        "members" : {
            "153950039532134112" : {
                "rank" : 1,
                "score" : 43,
                "time" : 1530513303
            }
        },
        "rank" : 2,
        "score" : 43
    },
    "Blue" : {
        "members" : {
            "273493248051539968" : {
                "rank" : 1,
                "score" : 849,
                "time" : 1530514923
            },
            "277645262486011904" : {
                "rank" : 2,
                "score" : 312,
                "time" : 1530513964
            },
            "281784064714487810" : {
                "rank" : 3,
                "score" : 235,
                "time" : 1530514147
            }
        },
        "rank" : 1, …
Run Code Online (Sandbox Code Playgroud)

python dictionary python-2.7

3
推荐指数
1
解决办法
180
查看次数