我需要从字符串中读取一些格式化数据并将其存储在两个变量中。字符串具有以下格式:
data = "(1234),(-567)"
数字的长度和符号各不相同。我觉得这应该很简单。在C中很容易:
scanf(data, "(%d),(%d)", num1, num2)
但是在斯威夫特,我想把头发拉出来试图找到一个简单的方法来做到这一点。正如其他答案中所建议的那样,我试过了:
data.components(separatedBy: CharacterSet.decimalDigits.inverted)
然而,这忽略了减号。非常感谢任何帮助!
答案 0 :(得分:0)
当您需要scanf
时,您可以使用Scanner
- 就像行为一样:
let data = "(1234),(-567)"
var num1: CInt = 0
var num2: CInt = 0
let scanner = Scanner(string: data)
if
scanner.scanString("(", into: nil),
scanner.scanInt32(&num1),
scanner.scanString("),(", into: nil),
scanner.scanInt32(&num2),
scanner.scanString(")", into: nil)
{
print(num1, num2)
} else {
print("failed")
}
答案 1 :(得分:0)
我喜欢正则表达式:
train_X = (train_X - numpy.mean(train_X)) / (numpy.amax(train_X) - numpy.amin(train_X))
train_Y = (train_Y - numpy.mean(train_Y)) / (numpy.amax(train_Y) - numpy.amin(train_Y))`