Python 两个数字的比较

Python 两个数字的比较,python,string-comparison,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(l

我正在尝试制作一个程序来检查两个数字是否有相同的数字,但顺序不同。例如,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"
a=322
b=223
列表a=列表(str(a))
列表_b=列表(str(b))
c=长度(str(a))
d=长度(str(b))
j=0
如果c!=d:
打印“假”
其他:
对于范围内的i(len(列表_a)):

而像这样的事情似乎是显而易见的:

#!/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$
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$
如果您希望在不考虑每个数字的数量的情况下匹配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$ ./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$
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()

和产出:

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]:
致:

它会起作用的。也就是说,这并不总是有效的,因为当你这样做时:

while j<d:

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

#!/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$
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$
如果您希望在不考虑每个数字的数量的情况下匹配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$ ./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$
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()

和产出:

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]:
致:

它会起作用的。也就是说,这并不总是有效的,因为当你这样做时:

while j<d:
而j我认为它有帮助

    a = 323
    b = 233

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

    print sorted(al) == sorted(bl)
我认为这有帮助

    a = 323
    b = 233

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

    print sorted(al) == sorted(bl)

您可以使用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})
如果不关心字符串中的位数,可以使用设置的内置类型来获取指纹:

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

你唯一要做的就是找到某种指纹

您可以使用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})
如果不关心字符串中的位数,可以使用设置的内置类型来获取指纹:

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

你唯一要做的就是找到某种指纹

i
代码段中的变量是字符串,而不是整数,这就是为什么会出现
TypeError
。改变

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

如果要使用
i
类索引,请将
更改为
类循环:

for i in range(len(list_a)):
此外,python中的索引从0开始,而不是从1开始,因此更改

j = 1

还有一项更正:

while j <= d:

代码段中的
i
变量是字符串,而不是整数,这就是为什么会出现
TypeError
。改变

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

如果要使用
i
类索引,请将
更改为
类循环:

for i in range(len(list_a)):
此外,python中的索引从0开始,而不是从1开始,因此更改

j = 1

还有一项更正:

while j <= d:

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


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



提示-在比较数字时,不取决于长度。你看1=0001。此外,您不需要循环。去检查a=b。问题是数字不相等。322不等于223,但它应该让我回到“真”的状态,所以当它们不相等时,你想怎么做?问题是什么?我需要找出它们是否有相同的数字,但顺序不同。在没有循环的情况下我怎么做?这是针对Project Euler的吗?提示-在比较数字时不依赖于长度。你看1=0001。此外,您不需要循环。去检查a=b。问题是数字不相等。322不等于223,但它应该让我回到“真”的状态,所以当它们不相等时,你想怎么做?问题是什么?我需要找出它们是否有相同的数字,但顺序不同。没有循环我怎么做?这是针对Project Euler的吗?有一种方法可以不用排序来做吗?任务只是循环,不需要列表比较,只需要时间和内存
sorted()
也可以对字符串进行排序,如果不想保存副本,则将
sorted()
与列表一起使用是个坏主意;您可以
list.sort()
就地排序。有没有一种不用排序的方法?任务只是循环,不需要列表比较,只需要时间和内存
sorted()
也可以对字符串进行排序,如果不想保存副本,则将
sorted()
与列表一起使用是个坏主意;您可以
list.sort()
就地排序。有没有一种不用排序的方法?该任务只有循环,你可以使用
return not len(b_alpha)
,而不是
return False if len(b_alpha)else True
@Vik2015:True,尽管这不太清楚和明确。简练在Python中通常不是一个设计目标。您能检查一下我做错了什么吗?我编辑了原稿post@user2923032当前位置查看我的最新编辑,您只有一个小错误,这会使您感到不适。有没有一种方法可以在不进行排序的情况下执行此操作?该任务只有循环,你可以使用
return not len(b_alpha)
,而不是
return False if len(b_alpha)else True
@Vik2015:True,尽管这不太清楚和明确。简练在Python中通常不是一个设计目标。您能检查一下我做错了什么吗?我编辑了原稿post@user2923032:查看我的最新编辑,您只遇到了一个小错误。AttributeError:'str'对象没有属性'remove',我仍然存在“remove”@user2923032的问题,这是因为您的
列表\u b
是字符串,而不是列表。看看我上次的更正。现在我没有错误,但答案应该是正确的,不是。我编辑了我原来的帖子。怎么了?谢谢大家!`AttributeError:'str'对象没有属性'remove',我仍然存在“remove”@user2923032的问题,这是因为您的
列表是字符串,而不是列表。