镜像一个句子中的单个单词,但不是句子本身

时间:2017-11-12 19:16:05

标签: python

有没有人对如何创建 private void btnSave_Click(object sender, EventArgs e) { string imageFilePath = null; string dataFilePath = null; ImageFormat format = System.Drawing.Imaging.ImageFormat.Png; try { // Image data imageFilePath = promptForFilePath("Save Chart image", "PNG Files (*.png)|*.png|JPEG Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|BMP Files (*.bmp)|*.bmp|All Files (*.*)|*.*"); if (!String.IsNullOrWhiteSpace(imageFilePath)) { switch (Path.GetExtension(imageFilePath).Replace(".", "").ToLower()) { case "jpeg": case "jpg": format = ImageFormat.Jpeg; break; case "png": format = ImageFormat.Png; break; case "bmp": format = ImageFormat.Bmp; break; } this.graph.SaveImage(imageFilePath, format); } // XML data dataFilePath = promptForFilePath("Save Chart data","XML Files (*.xml)|*.xml|All Files (*.*)|*.*"); if (!String.IsNullOrWhiteSpace(dataFilePath)) this.graph.Serializer.Save(dataFilePath); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message, "btnSave_Click Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { } } string promptForFilePath(string title = "Save File", string fileFilter = "All Files (*.*)|*.*") { string result = null; SaveFileDialog saveFileDialog = null; try { saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = title; saveFileDialog.Filter = fileFilter; if(saveFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { result = saveFileDialog.FileName; } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message, "saveChartData Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (saveFileDialog != null) { saveFileDialog.Dispose(); saveFileDialog = null; } } return result; } 有任何想法,function接受来自标准inputprints他们mirrored的文字?

例如:

输入:

day time you is  

输出:

yad emit ouy si   

2 个答案:

答案 0 :(得分:0)

您可以使用generator循环浏览word中的每个string并将其反转。要反转它,我们使用slicing高级-1作为step值。然后我们想要在每个单词之间加入所有反转的单词和space。我们可以使用str.join执行此操作。

将上述内容放在一起,我们得到function

def mirror(s):
    return ' '.join(w[::-1] for w in s.split())

的示例:

>>> mirror("day time you is")
'yad emit uoy si'
>>> mirror("hello how are you")
'olleh woh era uoy'

答案 1 :(得分:0)

def mirrow(stringToMirror) :
     stringToMirrorList = StringToMirror.split(" ")
     for x in stringToMirrorList :
         print x(::-1)  + " "