Python 比较字符串中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37958360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing characters in strings
提问by Denis Moura
I'm trying to create a function that compares characters in the same position of two strings of same length and returns the count of their differences.
我正在尝试创建一个函数,该函数比较两个长度相同的字符串的相同位置的字符并返回它们的差异计数。
For instance,
例如,
a = "HORSE"
b = "TIGER"
And it would return 5 (as all characters in the same position are different)
它会返回 5(因为同一位置的所有字符都不同)
Here's what I've been working on.
这就是我一直在做的事情。
def Differences(one, two):
difference = []
for i in list(one):
if list(one)[i] != list(two)[i]:
difference = difference+1
return difference
That gives an error "List indices must be integers not strings"
这给出了一个错误“列表索引必须是整数而不是字符串”
And so I've tried turning it to int by using int(ord(
所以我尝试使用 int(ord(
def Differences(one, two):
difference = 0
for i in list(one):
if int(ord(list(one)[i])) != int(ord(list(two)[i])):
difference = difference+1
return difference
Which also returns the same error.
这也返回相同的错误。
When I print list(one)[1] != list(two)[1] it eithers returns True or False, as the comparison is correctly made.
当我打印 list(one)[1] != list(two)[1] 时,它要么返回 True 要么返回 False,因为比较是正确进行的。
Can you tell me how to correct my code for this purpose?
你能告诉我如何为此目的更正我的代码吗?
回答by Reid Ballard
I would probably just iterate over both of them at the same time with zip and a list comprehension and then take length of the list:
我可能只是使用 zip 和列表理解同时迭代它们,然后获取列表的长度:
a='HORSE'
b='TIGER'
words=zip(a,b)
incorrect=len([c for c,d in words if c!=d])
print(incorrect)
Zipping pairs lists together index-for-index, stopping when one runs out. List comprehensions are generators that are basicallycompact for-statements that you can add logic to. So it basically reads: for each zipped pair of letters (c,d) if c!=d then put a into the list (so if the letters are different, we increase the list length by 1). Then we just take the length of the list which is all the letters that are positionally different.
压缩对列出索引对索引,当一个用完时停止。列表推导式生成器基本上是紧凑的 for 语句,您可以向其中添加逻辑。所以它基本上是:对于每个压缩的字母对 (c,d) 如果 c!=d 然后将 a 放入列表中(因此如果字母不同,我们将列表长度增加 1)。然后我们只取列表的长度,即位置不同的所有字母。
If we consider missing letters to be different, then we can use itertools.zip_longest to fill out the rest of the word:
如果我们认为缺失的字母不同,那么我们可以使用 itertools.zip_longest 来填写单词的其余部分:
import itertools
a='HORSES'
b='TIG'
words=itertools.zip_longest(a,b,fillvalue=None)
incorrect=len([c for c,d in words if c!=d]) ## No changes here
print(incorrect)
Obviously, None will never equal a character, so the difference in length will be registered.
显然, None 永远不会等于一个字符,因此长度的差异将被记录。
EDIT: This hasn't been mentioned, but if we want case-insensitivity, then you just run .lower() or .casefold() on the strings beforehand.
编辑:这还没有提到,但是如果我们想要不区分大小写,那么您只需事先在字符串上运行 .lower() 或 .casefold() 。
回答by user3404344
sum([int(i!=j) for i,j in zip(a,b)])
would do the trick
sum([int(i!=j) for i,j in zip(a,b)])
会做的伎俩
回答by Jordan Bonitatis
use zip
to iterate over both strings consecutively
用于zip
连续迭代两个字符串
>>> def get_difference(str_a, str_b):
... """
... Traverse two strings of the same length and determine the number of
... indexes in which the characters differ
... """
...
... # confirm inputs are strings
... if not all(isinstance(x, str) for x in (str_a, str_b)):
... raise Exception("`difference` requires str inputs")
... # confirm string lengths match
... if len(str_a) != len(str_b):
... raise Exception("`difference` requires both input strings to be of equal length")
...
... # count the differences; this is the important bit
... ret = 0
... for i, j in zip(str_a, str_b):
... if i != j:
... ret += 1
... return ret
...
>>> difference('HORSE', 'TIGER')
5
also, the general style is to lower case function names (which are often verbs) and title case class names (which are often nouns) :)
此外,一般风格是小写函数名称(通常是动词)和标题案例类名称(通常是名词):)
回答by Eduardo Cuesta
Find out for yourself
自己去了解一下
>>> a = "HORSE"
>>> list(a)
['H', 'O', 'R', 'S', 'E']
>>> list(a)[2]
'R'
>>> for i in list(a):
... i
...
'H'
'O'
'R'
'S'
'E'
>>> list(a)['R']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
Good luck!
祝你好运!
回答by Nick Davies
a = "HORSE"
b = "TIGER"
a_list=[]
b_list=[]
for l in a_list:
a_list.append(l)
for k in b_list:
b_list.append(k)
difference = len(a)
for i in a_list:
for x in b_list:
if a_list[i] == b_list[x]:
difference = difference - 1
print(difference)
See if this works :)
看看这是否有效:)
回答by Ahmad
That's too simple:
这太简单了:
def Differences(one, two):
difference = 0
for char1, char2 in zip(one, two):
if char1 != char2:
difference += difference
return difference
回答by bhagyashree bhaya
a = ['a' , 'b' , 'c' , 'd' , 'd' , 'c']
b = ['a' , 'b' , 'c' ]
index = []
if len(a) == len(b):
for i in range(len(a)):
if a[i]!=b[i]:
index.append(a[i])
if len(index) > 0:
print("The array is not same")
else:
print("The array is same")
else:
print("The array is not same")
回答by John-Paul Ensign
You could do something like this:
你可以这样做:
def getDifferences(a,b):
count = 0
for i in range(0, len(a)):
if a[i] is not b[i]:
count += 1
return count
The only thing that you will have to implement yourself here is checking for the size of the strings. In my example, if a
is larger than b
, then there will be an IndexError
.
您在这里唯一需要自己实现的是检查字符串的大小。在我的示例中,如果a
大于b
,则将有一个IndexError
.
回答by Jon McClung
try this:
尝试这个:
def Differences(one, two):
if len(two) < len(one):
one, two = two, one
res = len(two) - len(one)
for i, chr in enumerate(one):
res += two[i] != chr
return res
it's important to make the first check of their size in case the second string is shorter than the first, so you don't get an IndexError
如果第二个字符串比第一个短,首先检查它们的大小很重要,这样你就不会得到 IndexError
回答by Aviad
As matters for complexity and runtime, calling list() each iteration is not efficient, since it splits the strings, allocates memory and on... The correct way to do it, is to iterate the indexof the lists, than compare them by it, something like:
至于复杂性和运行时的问题,每次迭代调用 list() 效率不高,因为它拆分字符串,分配内存等等......正确的方法是迭代列表的索引,而不是比较它们它,类似于:
def str_compare(l1, l2):
assert len(l1) == len(l2) , 'Lists must have the same size'
differ_cnt = 0
for i in xrange(len(l1)):
if l1[i] != l2[i]:
differ_cnt += 1
return differ_cnt