简体   繁体   English

如何将4位数字变量与4位数字列表进行比较,并确定它们是否具有完全相同的数字,而不管顺序如何?

[英]How Can I Compare A 4 Digit Variable To A List Of 4 Digit Numbers And Determine If They Have The Exact Same Numbers, Regardless Of Order?

I have been trying to create a program that will break a 4 digit code by listing all possible codes. 我一直在尝试创建一个程序,该程序将通过列出所有可能的代码来破坏4位代码。 The code, 1234 and 4321 will both work as they have the same numbers. 代码1234和4321都可以使用,因为它们具有相同的数字。 Just as 7125 and 2157 will work. 正如7125和2157将起作用。

I have tried comparing the variable with the numbers in the list, but have only been able to do it with, for example, 1234 as the variable and 1234 as the number in the list. 我尝试过将变量与列表中的数字进行比较,但是只能用例如1234作为变量和1234作为列表中的数字来做到这一点。

codes1example = [1240, 1241, 0214]
variable1example = 4120

I am not on my normal computer so I cannot post the comparing code, but it is probably easy to make. 我不在普通计算机上,所以我无法发布比较代码,但是可能很容易做到。

With my original code, it would disregard any numbers in the list that is the same as the variable. 使用我的原始代码,它将忽略列表中与变量相同的任何数字。 But I cannot get past this, I cannot make it so it does not matter the order. 但是我无法超越,我无法做到,所以顺序无关紧要。

One approach is to convert the int to str and sort them and then do the comparison. 一种方法是将int转换为str并对其进行排序,然后进行比较。

Ex: 例如:

codes1example = [1240, 1241, 2014]
variable1example = 4120

temp = map(sorted, (map(str, codes1example)))
if sorted(str(variable1example)) in temp:
    print("Ok!")
else:
    print("No!!!")

#-->Ok!

WUse the set structure: W使用集合结构:

for n in codes1example:
   if not set(str(variable1example)).symmetric_difference(set(str(n))):
      print(variable1example, n)

This should work: 这应该工作:

codes1example = [1240, 1241, 214]
variable1example = 4120

def matchable(code):
     return sorted(str(code).zfill(4))

[c for c in codes1example if matchable(c) == matchable(variable1example)]

# [1240, 214]

This converts each item into a strong with left zero padding, then sorts the digits in the string to standardize them, then compares those. 这会将每个项目转换为带有左零填充的强项,然后对字符串中的数字进行排序以使其标准化,然后进行比较。 After sorting, these will actually be lists of digits as strings, but the comparison should work fine. 排序后,这些实际上将是作为字符串的数字列表,但是比较应该可以正常工作。

Note that I had to drop the leading zero from 214 when entering it as an integer, because the leading 0 causes it to be interpreted as an octal literal (0214 == decimal 140). 请注意,当我将其作为整数输入时,必须从214中删除前导零,因为前导0会将其解释为八进制文字(0214 ==十进制140)。 I assume your real code has proper bits that should be interpreted as 4-digit decimal numbers. 我假设您的真实代码具有正确的位,应将其解释为4位十进制数字。 If you are storing your numbers as strings instead, you can skip the str() step. 如果您将数字存储为字符串,则可以跳过str()步骤。

Edit: updated code to correct the catch mentioned in Justin comment 编辑:更新了代码以纠正贾斯汀评论中提到的问题

you will get invalid token error if you start a number with zero like in your list 0214 , but you can store it as a text, below is an example of how to achieve this: 如果以0开头的数字(如列表0214 ,则会出现无效的令牌错误,但是您可以将其存储为文本,以下是实现此目的的示例:

codes1example = ['1240', '1241', '0214', '5678']
variable1example = '4120'

for code in codes1example:
    match = [c for c in variable1example if c in code ]
    if ''.join(match) == variable1example: 
        print (code)
        # rest of your code

output: 输出:

1240
0214

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM