可以说我有一个这样的列表:
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) 我设法得到日期
import datetime
getDate = datetime.date.today()
print(getDate.strftime("%Y-%B-%d"))
Run Code Online (Sandbox Code Playgroud)
输出是 2018-June-23
但我想这样格式化输出:(2018-JUNE-23月份为大写)
找到哪个给定变量等于零的最佳方法是什么?我可以做到这一点,但这不会告诉我哪个变量等于零.
if 0 in (a, b, c, d, e):
do something
Run Code Online (Sandbox Code Playgroud) 在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(),但使用模板而不是后处理步骤。
我希望您考虑以下代码:
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?