本文介绍了在字符串上调用upper(),不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我这里发生了什么。这是我的代码:


PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)


def filter_letters(original):

return''''。join(set(original) - PUNCT_SPACE_SET)


''original''是一个字符串。上面的工作正如预期的那样,但是当我改变

时它将返回''''。$($($。)'。'set(original.upper()) - PUNCT_SPACE_SET )


它似乎无法奏效。如果它有助于理解,完整的代码如下。


导入字符串

导入随机

import itertools


PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def convert_quote(quote):

return encrypt_quote(quote) .split(''|'')


def encrypt_quote(原文):

original_letters = filter_letters(original)

new_letters = list(string.ascii_uppercase)

而True:

random.shuffle(new_letters)

trans_letters =''''。join(new_letters )[:len(original_letters)]

如果test_code(original_letters,trans_letters):

trans_table = string.maketrans(original_letters,trans_letters)

休息

返回original.translate(trans_table)


def filter_letters(original):

return''''。join( set(original) - PUNCT_SPACE_SET)

#return''''。join(set(original.upper()) - PUN CT_SPACE_SET)

def test_code(original_letters,trans_letters):

for itertools.izip(original_letters,trans_letters):

if pair [0] == pair [1]:

返回False

返回True


if __name__ =='' __main__'':

print convert_quote("过去没有死。事实上,它甚至都没有过去。| William Faulkner")

Can someone tell me what''s happening here. This is my code:

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def filter_letters(original):
return ''''.join(set(original) - PUNCT_SPACE_SET)

''original'' is a string. The above works as expected, but when I change
it to

return ''''.join(set(original.upper()) - PUNCT_SPACE_SET)

it doesn''t seem to work. The full code is below if it helps to understand.

import string
import random
import itertools

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def convert_quote(quote):
return encrypt_quote(quote).split(''|'')

def encrypt_quote(original):
original_letters = filter_letters(original)
new_letters = list(string.ascii_uppercase)
while True:
random.shuffle(new_letters)
trans_letters = ''''.join(new_letters)[:len(original_letters)]
if test_code(original_letters, trans_letters):
trans_table = string.maketrans(original_letters, trans_letters)
break
return original.translate(trans_table)

def filter_letters(original):
return ''''.join(set(original) - PUNCT_SPACE_SET)
#return ''''.join(set(original.upper()) - PUNCT_SPACE_SET)

def test_code(original_letters, trans_letters):
for pair in itertools.izip(original_letters, trans_letters):
if pair[0] == pair[1]:
return False
return True

if __name__ == ''__main__'':
print convert_quote("The past is not dead. In fact, it''s not even
past.|William Faulkner")

推荐答案




为什么它不起作用?它做了什么,你期望它做什么?



Why doesn''t it work? What does it do, what did you expect it to do?



''IH''

-

Felipe。


''IH''
--
Felipe.




为什么它不起作用?它做了什么,你期望它做什么?



Why doesn''t it work? What does it do, what did you expect it to do?




如果你用第一行(没有评论的那一行)运行整个脚本,

你得到类似的输出,这是正确的:



If you run the whole script with the first line (the one not commented),
you get output similar to this, which is correct:




现在,我知道实际的upper()函数有效,但我无法理解是否

*当*被调用时,或者正在做什么时会出现问题

用以获得上面的第二个结果。



Now, I know the actual upper() function works, but I can''t understand if
there''s a problem with *when* it''s being called, or what''s being done
with it to get the second result above.





这里你正在翻译''original''(其中包含很多

小写字母),使用trans_table只映射大写的

字符。这个回报应该是:


返回original.upper()。翻译(trans_table)


mk

- -

。 o。 >> <<

。 。 o因为错误而得到宽恕更容易获得宽恕。
$ b $ o o o o而不是原谅是正确的。



And here you''re translating ''original'' (which contains a lot of
lowercase letters) with use of trans_table that maps only uppercase
characters. This return should be:

return original.upper().translate(trans_table)

mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It''s easier to get forgiveness for being wrong
o o o than forgiveness for being right.


这篇关于在字符串上调用upper(),不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 07:01