1

我正在尝试制作一个程序来检查两个数字是否具有相同的数字但顺序不同。例如,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"
4

5 回答 5

6

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

#!/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

于 2013-10-26T14:07:47.927 回答
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 内置类型来获取指纹:

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

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

于 2013-10-26T14:17:18.897 回答
0

i variable in your snippet is string, not integer, that's why you have TypeError. Change

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

to

if i == list_b[j]:

If you wan to use i like index, change for loop like so:

for i in range(len(list_a)):

Also, indices in python start from 0, not from 1, so change

j = 1

to

j = 0

One more correction:

while j <= d:

to

while j < d:

So you won't have index-out-of-range.

Also this one:

list_a = list(str(a))
list_b = list(str(b))
于 2013-10-26T14:23:55.817 回答
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))
于 2019-01-07T18:24:48.923 回答
-1

我认为它有帮助

    a = 323
    b = 233

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

    print sorted(al) == sorted(bl)
于 2013-10-26T14:08:46.463 回答