不太确定正确的标题是什么。
我有一个具有2个输入def color_matching(color_old, color_new)
的函数。此函数应检查两个参数中的字符串,如果有命中,则分配一个新字符串。
def color_matching(color_old, color_new):
if ('<color: none' in color_old):
color_old = "NoHighlightColor"
elif ('<color: none' in color_new):
color_new = "NoHighlightColor"
等等。问题是每个参数都可以与14个不同类别中的1个进行匹配(“ NoHighlightColor”是其中之一)。我敢肯定,比对每个映射重复执行if语句28次更好的方法是这样做的,但是我正在绘制空白。
答案 0 :(得分:1)
您可以首先解析您的输入参数,例如,例如:
old_color='<color: none attr:ham>'
您可以解析它以仅获取所需的相关属性的值:
_old_color=old_color.split(':')[1].split()[0]
这样_old_color='none'
然后,您可以使用{'none':'NoHighlightColor'}
字典,将其命名为colors_dict
old_color=colors_dict.get(_old_color, old_color)
这样,如果_old_color
作为字典中的键存在,old_color
将获得该键的值,否则,old_color
将保持不变
因此,您的最终代码应类似于以下内容:
def color_matching(color_old, color_new):
""" Assuming you've predefined colros_dict """
# Parsing to get both colors
_old_color=old_color.split(':')[1].split()[0]
_new_color=new_color.split(':')[1].split()[0]
# Checking if the first one is a hit
_result_color = colors_dict.get(_old_color, None)
# If it was a hit (not None) then assign it to the first argument
if _result_color:
color_old = _result_color
else:
color_new = colors_dict.get(_color_new, color_new)
答案 1 :(得分:0)
您可以将条件替换为数据结构:
databuild <-function() {
allonew <<- data.frame(Contract.Number=character(), Contract.Receipt.Point=character(),Contract.Delivery.Point=character(),Contract.Volumes.Mdthd=numeric())
allorow <- data.frame(Contract.Number=character(), Contract.Receipt.Point=character(),Contract.Delivery.Point=character(),Contract.Volumes.Mdthd=numeric())
deliveryct <<- 1
receipt.vols <- numeric()
receipt.vols <- c(200,310,100,40)
receipt.name <<- c("rtest1", "rtest2", "rtest3", "rtest4")
delivery.name <<- c("dtest1","dtest2")
contract <<- 1260
r.count <-length(receipt.vols)
contract.receipt.vols <<- receipt.vols[!is.na(receipt.vols)]
rec.sum <<- sum(receipt.vols)
contract.vols.sum <-rec.sum*2
delivery.vols <- c(500,150)
d.count <-length(delivery.vols)
contract.delivery.vols <<- delivery.vols[!is.na(delivery.vols)]
receipt.vols <- receipt.vols[receiptct]
delivery.vols <- delivery.vols[deliveryct]
browser()
while(contract.vols.sum/rec.sum !=1) {
if(receipt.vols == delivery.vols) {
while(receipt.vols == delivery.vols) {
contract.vols <-min(receipt.vols,delivery.vols)
contract.vols.sum <- contract.vols.sum - contract.vols
allorow <- as.data.frame(t(c(contract[1],receipt.name[receiptct],delivery.name[deliveryct],contract.vols)))
colnames(allorow) <- colnames(allonew)
allonew <-rbind(allonew,allorow)
deliveryct <-deliveryct + 1
receiptct <- receiptct + 1
if(receiptct > r.count) {
break
}
delivery.vols <- contract.delivery.vols[deliveryct]
receipt.vols <- contract.receipt.vols[receiptct]
}
} else if(receipt.vols < delivery.vols) {
while(receipt.vols < delivery.vols) {
contract.vols <- min(receipt.vols,delivery.vols)
delivery.vols <- delivery.vols - contract.vols
contract.vols.sum <- contract.vols.sum - contract.vols
allorow <- as.data.frame(t(c(contract[1],receipt.name[receiptct],delivery.name[deliveryct],contract.vols)))
colnames(allorow) <- colnames(allonew)
allonew <-rbind(allonew,allorow)
receiptct <- receiptct + 1
receipt.vols <- contract.receipt.vols[receiptct]
}
} else if(receipt.vols > delivery.vols) {
while(receipt.vols > delivery.vols) {
contract.vols <-min(receipt.vols,delivery.vols)
receipt.vols <- receipt.vols- contract.vols
contract.vols.sum <- contract.vols.sum - contract.vols
allorow <- as.data.frame(t(c(contract[1],receipt.name[receiptct],delivery.name[deliveryct],contract.vols)),stringsAsFactors = FALSE)
colnames(allorow) <- colnames(allonew)
allonew <-rbind(allonew,allorow)
deliveryct <- deliveryct + 1
delivery.vols <- contract.delivery.vols[deliveryct]
}
} else next
}
allonew <<- allonew
但是您似乎遇到了一个问题,要求您尝试识别的格式具有正确的解析器。
您可以通过简单的字符串操作(例如def match(color):
matches = {'<color: none': 'NoHighlightColor', ... }
for substring, ret in matches.iteritems():
if substring in color:
return ret
您也许可以使用大量的正则表达式来破解它。
或者使用由this one之类的库生成的功能强大的解析器