Python将“\"转换为“\\" [英] Python converting '\' to '\\'
问题描述
我正在编写一个程序来对输入字符串(歌曲名称)的列表进行排序.这些歌曲名包含像 $\lambda$ 这样的乳胶字符,我想要而是像 'lambda' 一样排序,så 我正在使用应用的能力排序期间每个元素的函数.像这样:
I am writing a program to sort a list fo input strings (song names). Those songnames contains latex chars like $\lambda$, which i want to get sorted like 'lambda' instead, så i'm using a the ability to apply a function to each element during sort. like this:
# -*- coding: UTF8 -*-
def conv( inp ):
if inp == '$\lambda$':
return 'lambda'
else:
return inp
mlist = []
mlist.append('martin')
mlist.append('jenny')
mlist.append('åse')
mlist.append('$\lambda$')
mlist.append('lambda')
mlist.append('\her')
print (mlist)
mlist = sorted(mlist, key=conv(str.lower))
print (mlist)
但出于某种原因,当我附加 lambda
符号或 \her
时,它会将其转换为 \\her
或 $\\lambda$
,我可以防止这种情况发生吗?
But for some reason, when i append the lambda
sign or \her
it converts it to \\her
or $\\lambda$
, can i prevent this?
推荐答案
但出于某种原因,当我附加
lambda
符号或\her
时,它会将其转换为\\her
或$\\lambda$
...
But for some reason, when i append the
lambda
sign or\her
it converts it to\\her
or$\\lambda$
...
不,不是.您看到的是表示,为了清晰起见,它总是将单独的反斜杠加倍.如果你打印实际的字符串,你会发现它们没问题.
No it doesn't. What you're seeing is the representation, which always doubles solo backslashes for clarity. If you print the actual strings instead you'll see that they're fine.
这篇关于Python将“\"转换为“\\"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!