简体   繁体   English

检查一个列表中的元素是否与另一个列表中的另一个元素具有相同的索引

[英]Check if an element in one list has the same index as another element in another list

I am making the mastermind game on python with numbers. 我正在用数字在python上制作策划游戏。 I have a problem with checking the elements if they are in an incorrect place. 我在检查元素是否放置在错误位置时遇到问题。

So basically, if the code generated by the computer has no duplicates but the user used some in their input like (COMP: 1 2 3 4 & USER: 1 2 2 3). 因此,基本上,如果计算机生成的代码没有重复项,但用户在输入中使用了一些重复项(例如:COMP:1 2 3 4和USER:1 2 2 3)。 I can't check the positions of only one '2' and not both of them. 我不能只检查一个“ 2”的位置,而不能同时检查两个位置。 Please help. 请帮忙。

Code: 码:

def incorrect_place():
        incorrect_place = 0

        if len(set(user_code)) != 4: #There are duplicates in the user's input
            for index, num in enumerate(set(user_code)):
                #I don't know how to check it now
                incorrect_place += 1

       return incorrect_place

I would like it if someone also finds a solution where the computer has chosen a duplicate and so has the user like (COMP: 1, 2, 2, 3 & USER: 2, 3, 4, 4) 如果有人也找到了计算机选择了副本的解决方案,而用户也喜欢(COMP:1、2、2、3和USER:2、3、4、4),我会很喜欢

Based on a little bit of research into what Mastermind is and the fact that the function in your OP returns an integer, my guess is that you want to compare a computer-generated list to a user-generated list of the same size and see how many positions have different values. 基于对Mastermind是什么的一点研究,以及OP中的函数返回整数的事实,我的猜测是,您希望将计算机生成的列表与相同大小的用户生成的列表进行比较,并了解如何许多职位都有不同的价值。

Example: 例:

Computer: 1 2 3 4
User:     1 2 2 3
          S S D D   # S=Same, D=Different

So the answer above would be 2. 所以上面的答案是2。

I propose the following: 我提出以下建议:

def incorrect_places(comp_list, user_list):
    return sum(1 if a != b else 0 for a, b in zip(comp_list, user_list))

How it works: 这个怎么运作:

  • zip(comp_list, user_list) gives us a list of 2-tuples, pairing the corresponding values in each list by the position in which they occur. zip(comp_list, user_list)给我们一个2元组的列表,将每个列表中的对应值与它们出现的位置配对。 For the above example, we'd get: [(1, 1), (2, 2), (3, 2), (4, 3)] 对于上面的示例,我们将得到: [(1, 1), (2, 2), (3, 2), (4, 3)]
  • 1 if a != b else 0 for a, b in that_list -- returns a 1 if each number in the pair is the same, otherwise a 0. Thus, we get [0, 0, 1, 1] 1 if a != b else 0 for a, b in that_list 1,否则1 if a != b else 0 for a, b in that_list -如果该对中的每个数字相同,则返回1,否则返回0。因此,我们得到[0, 0, 1, 1] 1 if a != b else 0 for a, b in that_list [0, 0, 1, 1]

Finally we sum it all to get our answer. 最后,我们将所有这些sum起来以获得答案。 Hope this helps. 希望这可以帮助。

Edit: Commenters have pointed out that this is the same as doing sum(a != b for a, b in zip(comp_list, user_list)) . 编辑:评论者指出,这与执行sum(a != b for a, b in zip(comp_list, user_list)) I prefer the longer and more readable version, but they work the same way. 我更喜欢更长和更易读的版本,但是它们的工作方式相同。 This difference is purely stylistic. 这种差异纯粹是风格上的。 Choose whichever version you like. 选择您喜欢的版本。

I understood your question after reading Two-Bit Alchemist's answer. 阅读“两位炼金术士”的答案后,我理解了您的问题。 Here's my try: 这是我的尝试:

def list_differences(list_a, list_b):
    # Subtract list_b from list_a, element by element. The subtraction 
    # will result in non-zero values for elements that were different.
    return [bool(list_a[i]-list_b[i]) for i in range(len(list_a))].count(True)

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

相关问题 如何检查列表元素是否在另一个列表中,但也在同一索引中 - how to check whether a list element is in another list but also at the same index 有没有办法确定列表的元素是否与另一个列表(具有相同索引)的元素匹配? - Is there a way of determining if an element of a list matches the element of another list (of the same index)? 如何在包含相同元素的另一个列表中找到一个列表的元素的索引? - How to find index of element of one list in another list which includes the same elements? 检查元素是否为列表或其他对象 - Check if element is list or another object 检查一个列表的每个元素是否是另一个列表的所有元素的倍数 - Check if each element of one list is a multiple of all the elements of another list 在另一个列表中查找列表的第一个元素的索引 - Finding the index of the first element of a list in another list 如何检查列表中的元素是否存在于另一个列表中 - How to check if an element in the list exists in another list 检查列表元素是否存在于另一个列表的元素中 - check if element of list is present in elements of another list 检查列表数组是否包含另一个列表中的元素 - Check if list array contains element in another list 一个列表的返回元素基于另一个列表的真实索引 - Return element of one list based on true index of another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM