为什么我不能xor字符串? [英] Why can't I xor strings?

查看:89
本文介绍了为什么我不能xor字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数来比较两个字符串是否相似。因为

我正在使用python制作一个小型文本冒险引擎而我想要它b / b
能够对轻微的错误拼写做出反应,比如inevtory等等。为了节省时间我的函数做的第一件事是检查我是否正在比较一个空的字符串与非空的字符串,因为它们永远不会被考虑

类似。现在我必须写出这样的支票:


如果str1和str2:

如果不是str1或不str2:

返回0


因为python不会让我做str1 ^ str2。为什么这只适用于

数字?只需将空字符串视为0,将空字符视为1.


无论这是否是检测两个字符串是否类似的最佳实现,我都不会不知道为什么xor for strings不应该支持
。我错过了什么吗?特别是,我认为

有xor是很酷的。而不是^。胡萝卜会返回结果

值,而xor值则返回会表现得像和/或做并返回那个

true(如果有的话)。

I wrote a function to compare whether two strings are "similar" because
I''m using python to make a small text adventure engine and I want to it
to be responsive to slight mispellings, like "inevtory" and the like. To
save time the first thing my function does is check if I''m comparing an
empty vs. a non-empty string, because they are to be never considered
similar. Right now I have to write out the check like this:

if str1 and str2:
if not str1 or not str2:
return 0

Because python won''t let me do str1 ^ str2. Why does this only work for
numbers? Just treat empty strings as 0 and nonempty as 1.

Regardless of whether this is the best implementation for detecting if
two strings are similar, I don''t see why xor for strings shouldn''t be
supported. Am I missing something? Inparticular, I think it''d be cool to
have "xor" as opposed to "^". The carrot would return the resulting
value, while "xor" would act like and/or do and return the one that was
true (if any).

推荐答案

dataangel< ; K0 ***** @ kzoo.edu>写道:
dataangel <k0*****@kzoo.edu> writes:
为了节省时间,我的函数做的第一件事是检查
我是在比较一个空字符串还是一个非空字符串,因为它们是
从未考虑类似。
现在我必须写出这样的支票:

如果str1和str2:
如果不是str1或不str2:
返回0
To save time the first thing my function does is check if
I''m comparing an empty vs. a non-empty string, because they are to be
never considered similar. Right now I have to write out the check like this:

if str1 and str2:
if not str1 or not str2:
return 0




怎么样


def非空(

return( len(s)> 0)


然后


如果非空(str1)!=非空(str2):

返回0


当然还有其他方法可以写同样的东西。 Python 2.3有b $ b和bool。函数当调用字符串时,如果

字符串是非空的,则返回true。



How about:

def nonempty(s):
return (len(s) > 0)

and then

if nonempty(str1) != nonempty(str2):
return 0

Of course there''s other ways to write the same thing. Python 2.3 has
a "bool" function which when called on a string, returns true iff the
string is nonempty.


您好DataAngel,


欢迎来到Python社区!


从阅读你的帖子,听起来你想要使用XOR

加密,因为还有其他方法快速轻松地比较

字符串以确定它们是否相等。


要比较字符串,请使用以下内容:


name =" Steven"

name2 =" Steven"

打印名称== name2


结果将是真实的。但是,如果我最初的猜测是正确的,那么你想要对字符串使用XOR,那可能是因为你想要快速加密数据以便基本用户无法查看信息。
查看信息。如果这是你想要的东西,我可能会为你点上一些东西......


让我知道,


Byron

---

dataangel写道:
Hi DataAngel,

Welcome to the Python community!

From reading your post, it sounds like you are wanting to use XOR
encryption because there are other ways to quickly and easily compare
strings to see if they are equal.

To compare a string, use the following:

name = "Steven"
name2 = "Steven"
print name == name2

The result will be "True." However, if my initial guess is correct that
you are wanting to use XOR for strings, it probably because you are
wanting a quick way of encrypting data so that basic users won''t be able
to view the information. If this is what you are wanting, I might have
something for you...

Let me know,

Byron
---
dataangel wrote:
我写了一个函数来比较两个字符串是否相似。因为
我正在使用python制作一个小型的文本冒险引擎,而我希望它能够对轻微的错误拼写做出反应,比如inevtory。等等。为了节省时间我的功能所做的第一件事就是检查我是否正在比较一个空的字符串和一个非空的字符串,因为它们永远不会被认为是相似的。现在我必须写出这样的支票:

如果str1和str2:
如果不是str1或不str2:
返回0

因为python不会让我做str1 ^ str2。为什么这仅适用于
数字?只需将空字符串视为0,将空字符视为1.

无论这是否是检测两个字符串是否相似的最佳实现,我都不明白为什么xor for strings shouldn不受支持。我错过了什么吗?特别是,我认为
拥有xor会很酷。而不是^。胡萝卜将返回所得的值,而xor值则返回。会表现得像和/或做并返回那个真实的(如果有的话)。
I wrote a function to compare whether two strings are "similar" because
I''m using python to make a small text adventure engine and I want to it
to be responsive to slight mispellings, like "inevtory" and the like. To
save time the first thing my function does is check if I''m comparing an
empty vs. a non-empty string, because they are to be never considered
similar. Right now I have to write out the check like this:

if str1 and str2:
if not str1 or not str2:
return 0

Because python won''t let me do str1 ^ str2. Why does this only work for
numbers? Just treat empty strings as 0 and nonempty as 1.

Regardless of whether this is the best implementation for detecting if
two strings are similar, I don''t see why xor for strings shouldn''t be
supported. Am I missing something? Inparticular, I think it''d be cool to
have "xor" as opposed to "^". The carrot would return the resulting
value, while "xor" would act like and/or do and return the one that was
true (if any).



你好DataAngel,


欢迎来到Python社区!


从阅读你的帖子,听起来你想要使用XOR

加密。我之所以这么说是因为还有很多其他方法可以比较两个字符串的内容,看看它们是否完全相同。下面列出了如何执行此类操作的示例:


#如何比较两个字符串以查看它们是否完全匹配。

name =" ;史蒂文"

name2 ="史蒂文"

打印名称== name2

结果将为True。


但是,如果他或她想要使用基于XOR的加密来保存数据,那么人们可能希望对字符串使用XOR的唯一原因是

br />
半私人。


让我知道,


Byron

- -


dataangel写道:
Hi DataAngel,

Welcome to the Python community!

From reading your post, it sounds like you are wanting to use XOR for
"encryption." The reason why I say this is because there are many other
ways of comparing the content of two strings to see if they are exactly
the same. An example of how to do such a thing is listed below:

# How to compare two strings to see if they are exact matches.
name = "Steven"
name2 = "Steven"
print name == name2
The result will be "True."

However, the only reason that one might want to use XOR for a string is
if he or she wanted to use XOR based encryption in order to keep data
semi-private.

Let me know,

Byron
---

dataangel wrote:
我写了一个函数来比较两个字符串是否相似。因为
我正在使用python制作一个小型的文本冒险引擎,而我希望它能够对轻微的错误拼写做出反应,比如inevtory。等等。为了节省时间我的功能所做的第一件事就是检查我是否正在比较一个空的字符串和一个非空的字符串,因为它们永远不会被认为是相似的。现在我必须写出这样的支票:

如果str1和str2:
如果不是str1或不str2:
返回0

因为python不会让我做str1 ^ str2。为什么这仅适用于
数字?只需将空字符串视为0,将空字符视为1.

无论这是否是检测两个字符串是否相似的最佳实现,我都不明白为什么xor for strings shouldn不受支持。我错过了什么吗?特别是,我认为
拥有xor会很酷。而不是^。胡萝卜将返回所得的值,而xor值则返回。会表现得像和/或做并返回那个真实的(如果有的话)。
I wrote a function to compare whether two strings are "similar" because
I''m using python to make a small text adventure engine and I want to it
to be responsive to slight mispellings, like "inevtory" and the like. To
save time the first thing my function does is check if I''m comparing an
empty vs. a non-empty string, because they are to be never considered
similar. Right now I have to write out the check like this:

if str1 and str2:
if not str1 or not str2:
return 0

Because python won''t let me do str1 ^ str2. Why does this only work for
numbers? Just treat empty strings as 0 and nonempty as 1.

Regardless of whether this is the best implementation for detecting if
two strings are similar, I don''t see why xor for strings shouldn''t be
supported. Am I missing something? Inparticular, I think it''d be cool to
have "xor" as opposed to "^". The carrot would return the resulting
value, while "xor" would act like and/or do and return the one that was
true (if any).



这篇关于为什么我不能xor字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆