比较两个数字

时间:2013-10-26 13:33:20

标签: python string-comparison

我正在尝试制作一个程序来检查两个数字是否具有相同的数字但是顺序不同。例如,232和223将打印“true”,但123和223将打印“false”。 现在我没有错,但答案应该是“真实的”,而不是:

我的代码:

a=322 
b=223

list_a = list(str(a))
list_b = list(str(b))
c=len(str(a))
d=len(str(b))

j=0

if c != d:
    print "false"
else:
    for i in range(len(list_a)):
        while j<d:
           if i == list_b[j]:
            list_b.remove(list_b[j])
            break
           j=j+1
        j=0



if list_b==[]:
    print "true"

5 个答案:

答案 0 :(得分:4)

这样的事情似乎是一种显而易见的方式:

#!/usr/bin/python

def same_digits(a, b):
    if sorted(str(a)) == sorted(str(b)):
        print "{0} and {1} contain the same digits".format(a, b)
    else:
        print "{0} and {1} do not contain the same digits".format(a, b)

same_digits(232, 232)
same_digits(232, 223)
same_digits(232, 233)

输出:

paul@local:~/src/python/scratch$ ./testnum.py
232 and 232 contain the same digits
232 and 223 contain the same digits
232 and 233 do not contain the same digits
paul@local:~/src/python/scratch$

如果您想要匹配true,无论每个数字的数字如何,请使用set来消除重复项:

#!/usr/bin/python

def same_digits(a, b):
    if sorted(set(str(a))) == sorted(set(str(b))):
        print "{0} and {1} contain the same digits".format(a, b)
    else:
        print "{0} and {1} do not contain the same digits".format(a, b)

same_digits(232, 232)
same_digits(232, 223)
same_digits(232, 233)
same_digits(232, 2333332232)
same_digits(232, 2)
same_digits(232, 234)

输出:

paul@local:~/src/python/scratch$ ./testnum2.py
232 and 232 contain the same digits
232 and 223 contain the same digits
232 and 233 contain the same digits
232 and 2333332232 contain the same digits
232 and 2 do not contain the same digits
232 and 234 do not contain the same digits
paul@local:~/src/python/scratch$

如果你真的必须这么做,那么这会复制第一个例子而不使用sorted()

#!/usr/bin/python

def same_digits_loop(a, b):
    a_alpha = str(a)
    b_alpha = str(b)

    if len(a_alpha) != len(b_alpha):
        return False

    for c in a_alpha:
        b_alpha = b_alpha.replace(c, "", 1)

    return False if len(b_alpha) else True


def same_digits(a, b):
    if same_digits_loop(a, b):
        print "{0} and {1} contain the same digits".format(a, b)
    else:
        print "{0} and {1} do not contain the same digits".format(a, b)


same_digits(232, 23)
same_digits(232, 232)
same_digits(232, 223)
same_digits(232, 233)
same_digits(232, 2333)

和输出:

paul@local:~/src/python/scratch$ ./testnum3.py
232 and 23 do not contain the same digits
232 and 232 contain the same digits
232 and 223 contain the same digits
232 and 233 do not contain the same digits
232 and 2333 do not contain the same digits
paul@local:~/src/python/scratch$

对于您最近编辑中的问题代码,只需更改:

if i == list_b[j]:

为:

if list_a[i] == list_b[j]:

它会起作用。话虽如此,它不会始终工作,因为当你这样做时:

while j<d:

每次从list_b删除元素时,list_b的长度都会发生变化,但d不会。除非您每次都将d更新为等于新长度,否则在数字不相同时您将超出界限,并在您到达结束之前检查list_b是否已变空list_a

答案 1 :(得分:1)

您可以使用Counter对象获取每个数字字符串的指纹,然后只比较两个指纹,即所有这些指纹:

In [1]: n1 = 123

In [2]: n2 = 312

In [3]: n1, n2 = map(str, [n1, n2])

In [4]: n1,n2
Out[4]: ('123', '312')   

In [5]: from collections import Counter

In [6]: c1 = Counter(n1)

In [7]: c2 = Counter(n2)

In [8]: c1 == c2
Out[8]: True

In [9]: c1
Out[9]: Counter({'1': 1, '3': 1, '2': 1})

In [10]: c2
Out[10]: Counter({'1': 1, '3': 1, '2': 1})

如果您不关心字符串中的位数,可以使用set builtin类型来获取指纹:

In [13]: n1 = 112

In [14]: n2 = 212

In [15]: n1, n2 = map(str, [n1, n2])

In [16]: s1 = set(n1)

In [17]: s2 = set(n2)

In [18]: s1
Out[18]: set(['1', '2'])

In [19]: s2
Out[19]: set(['1', '2'])

In [20]: s1 == s2
Out[20]: True

你应该做的唯一工作就是找到某种指纹!

答案 2 :(得分:0)

我认为这有帮助

    a = 323
    b = 233

    al = [c for c in str(a)]
    bl = [c for c in str(b)]

    print sorted(al) == sorted(bl)

答案 3 :(得分:0)

您的代码段中的

i变量是字符串,而不是整数,这就是您拥有TypeError的原因。改变

if list_a[i] == list_b[j]:

if i == list_b[j]:

如果你想使用i之类的索引,请像这样更改for循环:

for i in range(len(list_a)):

此外,python中的索引从0开始,而不是从1开始,因此更改

j = 1

j = 0

还有一个更正:

while j <= d:

while j < d:

所以你不会有索引超出范围。

还有这个:

list_a = list(str(a))
list_b = list(str(b))

答案 4 :(得分:0)

使用sort()比较两个具有不同数字的数字:

 def check_different_numbers_having_same_digits(num1,num2):
            num1=[int(i) for i in str(num1)] #converting to int to list
            num1.sort()                     #sorting the list
            num2=[int(i) for i in str(num2)]
            num2.sort()
            if(num1==num2):
                return True
            else:
                return False

        print(check_different_numbers_having_same_digits(234,324))
相关问题