小编wim*_*wim的帖子

排列数字列表,允许联系

可以说我有一个这样的列表:

newIndexVertList = [0, 1, 2, 2, 1, 20, 21, 21, 20, 3, 23, 22]
Run Code Online (Sandbox Code Playgroud)

我想将其转换为:

newIndexVertList = [0, 1, 2, 2, 1, 4, 5, 5, 4, 3, 7, 6]
Run Code Online (Sandbox Code Playgroud)

这里,变换基于原始列表中按升序排列的数字位置.因此,在新的列表中,数字会根据逻辑被替换:

0   -->  0    0th position in sorted list
1   -->  1    1st position in sorted list
2   -->  2    2nd position in sorted list
3   -->  3    3rd position in sorted list
20  -->  4    4th position in sorted list
21  -->  5    5th position in sorted list
22 …
Run Code Online (Sandbox Code Playgroud)

python list

0
推荐指数
1
解决办法
1084
查看次数

日期格式化为月大写

我设法得到日期

import datetime
getDate = datetime.date.today()
print(getDate.strftime("%Y-%B-%d"))
Run Code Online (Sandbox Code Playgroud)

输出是 2018-June-23

但我想这样格式化输出:(2018-JUNE-23月份为大写)

python strftime python-3.x

0
推荐指数
2
解决办法
2512
查看次数

Pythonic方法检查哪个变量等于零?

找到哪个给定变量等于零的最佳方法是什么?我可以做到这一点,但这不会告诉我哪个变量等于零.

if 0 in (a, b, c, d, e):
    do something
Run Code Online (Sandbox Code Playgroud)

python

-1
推荐指数
1
解决办法
145
查看次数

如何使用strftime小写月份名称

strftime 中 %b是月份名称

>>> '{:%b}'.format(datetime.now())
'Apr'
Run Code Online (Sandbox Code Playgroud)

我可以使用chinese-hat修饰符carrot thing将它大写

>>> '{:%^b}'.format(datetime.now())
'APR'
Run Code Online (Sandbox Code Playgroud)

我如何小写它?即获得结果'{:%b}'.format(dt).lower(),但使用模板而不是后处理步骤。

c python strftime

-2
推荐指数
1
解决办法
3092
查看次数

为什么我不能在收益率上使用yield?

我希望您考虑以下代码:

def func(alist):
    if len(alist) == 1:
        return arg * 2
    for item in alist:
        yield item * 2
Run Code Online (Sandbox Code Playgroud)

运行它时,出现以下错误:

SyntaxError: 'return' with argument inside generator
Run Code Online (Sandbox Code Playgroud)

现在,我意识到我无法做到这一点。但是,我想知道为什么。到底是什么导致Python抛出SyntaxError

python yield return function python-2.x

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

标签 统计

python ×5

strftime ×2

c ×1

function ×1

list ×1

python-2.x ×1

python-3.x ×1

return ×1

yield ×1