将列表压缩为字符串:['z','y','x'...] -> 'zyx...' ? Python (2.7.1)

发布于 2024-11-02 13:22:48 字数 435 浏览 8 评论 0原文

如果我有 list='abcdedcba'

我想要: a=z、b=y、c=x、d=w、e=v 所以它会翻译成:

translate='zyxwvwxya'

我该怎么做? 如果我构造一个字典

>>> d=dict(zip(('a','b','c','d','e'),('z','y','x','w','v')))

并输入

>>> example= d[x] for x in list
>>> print translate
['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']

如何将其恢复为

translate='zyxwvwxyz'

形式?

If I have
list='abcdedcba'

and i want:
a=z, b=y, c=x, d=w, e=v
so it would translate to:

translate='zyxwvwxya'

How would I do this?
If I construct a dictionary

>>> d=dict(zip(('a','b','c','d','e'),('z','y','x','w','v')))

and type

>>> example= d[x] for x in list
>>> print translate
['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']

How do I get it back into the form

translate='zyxwvwxyz'

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜心 2024-11-09 13:22:48
the_list = ['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']
print "".join(the_list)
the_list = ['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']
print "".join(the_list)
新人笑 2024-11-09 13:22:48

对于单字母替换,请使用 maketrans从 string 模块翻译。它们的操作类似于 unix tr 命令。使用空分隔符连接是最后一步的正确答案,但对于此确切任务来说不是必需的。

For monoalphabetic substitution, use maketrans and translate from the string module. They operate like the unix tr command. Joining with an empty separator is the correct answer for that last step, but not necessary for this exact task.

千紇 2024-11-09 13:22:48
''.join(translate)

我不确定这是你想要的吗?

''.join(translate)

I'm not sure this is what you want?

始终不够爱げ你 2024-11-09 13:22:48

使用 maketrans 和 translate 的示例:

>>> import string
>>> table = string.maketrans('abcdef', 'zyxwvu')
>>> 'abdedddfdffdabe'.translate(table)
'zywvwwwuwuuwzyv'

假设您要替换 ASCII 字母表中的所有字母:

import string
reversed_ascii_letters = string.ascii_letters[::-1]
# reorder lowercase and uppercase
reversed_ascii_letters = reversed_ascii_letters[26:] + reversed_ascii_letters[:26]
table = string.maketrans(string.ascii_letters, reversed_ascii_letters)
data = 'The Quick Brown Fox Jumped Over the Lazy Dog'
print data.translate(table)

an example of using maketrans and translate:

>>> import string
>>> table = string.maketrans('abcdef', 'zyxwvu')
>>> 'abdedddfdffdabe'.translate(table)
'zywvwwwuwuuwzyv'

Assuming you want to substitute all letters in the ASCII alphabet:

import string
reversed_ascii_letters = string.ascii_letters[::-1]
# reorder lowercase and uppercase
reversed_ascii_letters = reversed_ascii_letters[26:] + reversed_ascii_letters[:26]
table = string.maketrans(string.ascii_letters, reversed_ascii_letters)
data = 'The Quick Brown Fox Jumped Over the Lazy Dog'
print data.translate(table)
記柔刀 2024-11-09 13:22:48
>>> import string
>>> table = string.maketrans(string.lowercase, string.lowercase[::-1])
>>> 'abcdedcba'.translate(table)
'zyxwvwxyz'
>>> import string
>>> table = string.maketrans(string.lowercase, string.lowercase[::-1])
>>> 'abcdedcba'.translate(table)
'zyxwvwxyz'
国产ˉ祖宗 2024-11-09 13:22:48
>>> import string
>>> letters = string.lowercase
>>> letters
'abcdefghijklmnopqrstuvwxyz'
>>> def revert_string(s):
    s_rev = ''
    for c in s:
        s_rev += letters[len(letters) - 1 - letters.find(c)]
    return s_rev

>>> s = 'zearoizuetlkzjetkl'
>>> revert_string(s)
'avzilrafvgopaqvgpo'
>>> import string
>>> letters = string.lowercase
>>> letters
'abcdefghijklmnopqrstuvwxyz'
>>> def revert_string(s):
    s_rev = ''
    for c in s:
        s_rev += letters[len(letters) - 1 - letters.find(c)]
    return s_rev

>>> s = 'zearoizuetlkzjetkl'
>>> revert_string(s)
'avzilrafvgopaqvgpo'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文