如何将数字字符串替换为数组中相应的字母字符。
virtualenv
答案 0 :(得分:3)
您可以使用Dictionary
替换规则:
let array = ["1", "2", "3", "3", "2", "3"]
let replacementRules = ["1": "A", "2": "B", "3": "C"]
let result = array.map{replacementRules[$0] ?? $0}
print(result)
//["A", "B", "C", "C", "B", "C"]
答案 1 :(得分:1)
如果您的数组包含字符串值直到1-26,您可以尝试这样
public class HueWheel : Control
{
static HueWheel()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HueWheel), new FrameworkPropertyMetadata(typeof(HueWheel)));
}
private bool _isPressed = false;
//private Canvas _templateCanvas = null;
private Canvas _PART_FirstCanvas;
private Canvas _PART_SecondCanvas;
private Slider _PART_Slider;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_PART_FirstCanvas = (Canvas)GetTemplateChild("PART_FirstCanvas");
_PART_SecondCanvas = (Canvas)GetTemplateChild("PART_SecondCanvas");
_PART_Slider = (Slider)GetTemplateChild("PART_Slider");
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (_isPressed)
{
const double RADIUS = 150;
Point newPos = e.GetPosition(_PART_SecondCanvas);
double angle = MyHelper.GetAngleR(newPos, RADIUS);
_PART_Slider.Value = (_PART_Slider.Maximum - _PART_Slider.Minimum) * angle / (2 * Math.PI);
}
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
_isPressed = true;
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
_isPressed = false;
}
}
答案 2 :(得分:1)
var arr = ["1","2","3","3","2","3"]
for i in 0..<arr.count {
switch(arr[i]) {
case "1":
arr[i] = "A"
break
case "2":
arr[i] = "B"
break
case "3":
arr[i] = "C"
break
default: break
}
}