我知道"%03d"可以很容易地做到这一点,但是我正在尝试解决一个稍微不同的问题。在脚本的一部分中,我需要找出一个范围(例如0-999)中有多少个数字,其中至少有一个3(或任何有问题的数字)。因此,我想到了这个lambda函数:
fx = lambda z,y=999: [ "%03d" % (x, ) for x in range(y) if str(z) in str(x) ]
Run Code Online (Sandbox Code Playgroud)
这是很好的工作,但我想根据范围自动填充填充的“前导零”位,例如003时为999或88时为09等等。知道我该怎么做吗?
阅读时如何忽略文件中的注释或空白/空行?我以为/^[\s#]*$/会做这个工作,但事实并非如此:
irb(main):180:0> open(inFile, 'r').each { |ln| puts ln if ln !~ /^[\s#]*$/ }
....
....
# and ..... ThIs Is A cOmMeNt .....
....
....
=> #<File:/tmp/running-instances.txt>
irb(main):181:0>
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?任何帮助将受到高度赞赏.干杯!!
PS.
我可以分两步单独完成:
open(inFile, 'r').each { |ln| next if ln =~ /^\s*$/; puts ln if ln !~ /#[^#]*$/ }
Run Code Online (Sandbox Code Playgroud) 如何在另一个变量的名称中使用变量?说,我有很多像这样的数组:
g_OS = ['Mac', 'Linux', 'Win']
g_Mac = ['Lion', 'Tiger', 'Jaguar']
g_Linux = ['Slackware', 'RedHat', 'Caldera']
g_Win = [ .... ]
Run Code Online (Sandbox Code Playgroud)
如果我这样做:g_OS.each {|OS| puts "g_#{OS}[0]"},它将打印'g_Mac[0]'或'g_Linux'作为文字字符串.但我真正想要的是获得数组的第一个元素:g_Mac.我怎样才能做到这一点?
如何输出这样的结果:
user I R H
=================
atl001 2 1 0
cms017 1 2 1
lhc003 0 1 2
Run Code Online (Sandbox Code Playgroud)
从这样的列表:
atl001 I
atl001 I
cms017 H
atl001 R
lhc003 H
cms017 R
cms017 I
lhc003 H
lhc003 R
cms017 R
Run Code Online (Sandbox Code Playgroud)
即我想计算的数量I,H以及R每个用户.刚一说明,我无法使用groupby从itertools在这种特殊情况下.在此先感谢您的帮助.干杯!!
我正在研究这个小脚本:基本上它将列表元素(包含特殊字符)映射到其索引以创建字典.
#!/usr/bin/env python
#-*- coding: latin-1 -*-
ln1 = '?0>9<8~7|65"4:3}2{1+_)'
ln2 = "(*&^%$£@!/`'\][=-#¢"
refStr = ln2+ln1
keyDict = {}
for i in range(0,len(refStr)):
keyDict[refStr[i]] = i
print "-" * 32
print "Originl: ",refStr
print "KeyDict: ", keyDict
# added just to test a few special characters
tsChr = ['£','%','\\','¢']
for k in tsChr:
if k in keyDict:
print k, "\t", keyDict[k]
else: print k, "\t", "not in the dic."
Run Code Online (Sandbox Code Playgroud)
它返回如下结果:
Originl: (*&^%$£@!/`'\][=-#¢?0>9<8~7|65"4:3}2{1+_)
KeyDict: {'!': 9, '\xa3': 7, '\xa2': 20, '%': …Run Code Online (Sandbox Code Playgroud) 我正在处理date validation(MM/DD/YYYY)并收到此错误:
错误:左值作为赋值的左操作数需要左值
在行:9(if ( Y%4=0 ) { return true; })运行这段代码?
bool valDate( int M, int D, int Y )
{
if (! (1<=M and M<=12) ) return false;
if (! (1<=D and D<=31) ) return false;
if ( (D==31) and (M==2 or M==4 or M==6 or M==9 or M==11) )
return false;
if ( (D==30) and (M==2) ) return false;
if ( (M==2) and (D==29) ) {
if ( Y%4=0 ) { return true; } …Run Code Online (Sandbox Code Playgroud) 我需要生成一个这样的字典:
{
'newEnv': {
'newProj': {
'newComp': {
'instances': [],
'n_thing': 'newThing'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
从一个元组,像这样:('newEnv','newProj','newComp','newThing')但只有当它不存在时.所以,我试过这个:
myDict = {}
(env,proj,comp,thing) = ('newEnv','newProj','newComp','newThing')
if env not in myDict:
myDict[env] = {}
if proj not in myDict[env]:
myDict[env][proj] = {}
if comp not in myDict[env][proj]:
myDict[env][proj][comp] = {'n_thing': thing, 'instances': []}
Run Code Online (Sandbox Code Playgroud)
这几乎是有效但不确定效率如何,或者我应该这样做.有什么建议)??
python ×4
dictionary ×2
erb ×2
ruby ×2
c++ ×1
date ×1
linux ×1
python-2.3 ×1
python-2.7 ×1
regex ×1
tuples ×1
validation ×1