String 为字符串中的元素指定不同的值
我用的是R。 假设我有一个城市向量,我想单独使用这些城市名称 一串String 为字符串中的元素指定不同的值,string,r,String,R,我用的是R。 假设我有一个城市向量,我想单独使用这些城市名称 一串 city = c("Dallas", "Houston", "El Paso", "Waco") phrase = c("Hey {city}, what's the meaning of life?") 所以我想以四个独立的短语结束 "Hey Dallas, what's the meaning of life?" "Hey Houston, what's the meaning of life?" ... Python
city = c("Dallas", "Houston", "El Paso", "Waco")
phrase = c("Hey {city}, what's the meaning of life?")
所以我想以四个独立的短语结束
"Hey Dallas, what's the meaning of life?"
"Hey Houston, what's the meaning of life?"
...
Python中是否有类似于format()的函数允许
让我以简单/高效的方式执行此任务
希望避免下面这样的事情
for( i in city){
phrase = c("Hey ", i, "what's the meaning of life?")
}
sprintf怎么样
> city = c("Dallas", "Houston", "El Paso", "Waco")
> phrase = c("Hey %s, what's the meaning of life?")
> sprintf(phrase, city)
[1] "Hey Dallas, what's the meaning of life?" "Hey Houston, what's the meaning of life?"
[3] "Hey El Paso, what's the meaning of life?" "Hey Waco, what's the meaning of life?"
根据需要的复杂程度,简单的粘贴即可完成以下工作:
paste("Hey ", city, ", what's the meaning of life", sep="")
做你想做的事
@Zach的回答是,sprintf有很多优点,比如正确的双打格式等。没有printf,但是你可以使用
printf来实现自己的功能