给定这样的代码,我如何在运行选项中实际设置文件?
我正在使用Spyder并将其-h -s -p -o作为参数,但我不确定如何为该-o选项指定命名文件.
class CommandLine:
def __init__(self):
opts, args = getopt.getopt(sys.argv[1:],'hspw:o:')
opts = dict(opts)
if '-o' in opts:
self.outfile = opts['-o']
else:
self.outfile = None
Run Code Online (Sandbox Code Playgroud) 假设我有这样的字典:
d = {27: 32, 28: 41, 29: 29, 30: 29, 31: 67, 32: 65}
Run Code Online (Sandbox Code Playgroud)
还有一个清单:
l = [27, 30, 31]
for n in l:
d2 = {k:v for k,v in d.items() if k in n}
Run Code Online (Sandbox Code Playgroud)
为什么不允许这样做?我该怎么做?
给定一个字典,d类型{key: (v1,v2)},我想v1,v2对所有键进行分割以产生分数,v3所以我的字典是类型的{key: (v1,v2,v3)}.
我明白我能做到:
for key,v1,v2 in d.items():
score = v1/v2
Run Code Online (Sandbox Code Playgroud)
但无法弄清楚如何将其存储在字典中.