Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查数组中的两个值是否相同。python_Python_Arrays - Fatal编程技术网

检查数组中的两个值是否相同。python

检查数组中的两个值是否相同。python,python,arrays,Python,Arrays,这是我的密码 a = [1, "A", 2, "B", 1, "C"] empty = [] if a[0] == a[2]: empty.append(a[1]) empty.append(a[3]) elif a[0] == a[4]: empty.append(a[1]) empty.append(a[5]) elif a[2] == a[4]: empty.append(a[3]) empty.append(a[5]) 我正在寻找一种更有

这是我的密码

a = [1, "A", 2, "B", 1, "C"]
empty = []
if a[0] == a[2]:
    empty.append(a[1])
    empty.append(a[3])
elif a[0] == a[4]:
    empty.append(a[1])
    empty.append(a[5])
elif a[2] == a[4]:
    empty.append(a[3])
    empty.append(a[5])
我正在寻找一种更有效的方法来执行这个过程。如果该数组中的两个元素[integers]相同,我希望它将数组[index+1]追加到“empty”中。如果数组中有2个相同的值,则后面将有2个字母。
在这个例子中,它将是[“A”,“C”],因为它们都有1。我更愿意找到重复项所在的索引位置

如果只想找到一个匹配项

a = [1, "A", 2, "B", 1, "C"]
empty = []
match = False

for i in range(0, len(a), 2):
    for j in range(i + 2, len(a) - 1, 2):
        if a[i] == a[j]:
            empty.append(a[i])
            empty.append(a[j])
            match = True
            break
    if match:
        break
如果你想要所有的火柴

a = [1, "A", 2, "B", 1, "C"]
empty = []

for i in range(0, len(a), 2):
    for j in range(i + 2, len(a) - 1, 2):
        if a[i] == a[j]:
            empty.append(a[i])
            empty.append(a[j])
如果您想更改索引而不是项目

empty.append(a[i])
empty.append(a[j])

编辑

正如@Aaron指出的,带有match变量的代码可以使用
for
循环的
else
子句来编写

a = [1, "A", 2, "B", 1, "C"]
empty = []

for i in range(0, len(a), 2):
    for j in range(i + 2, len(a) - 1, 2):
        if a[i] == a[j]:
            empty.append(a[i])
            empty.append(a[j])
            break
    else:
        continue
    break

您可以使用
itertools.combines
从列表(在本例中为偶数索引列表)中获取两个项目的每个唯一组合。项目将按照输入的字典顺序生成,因此如果输入被排序,输出也将被排序

from itertools import combinations
a = []
empty = [1, "A", 2, "B", 1, "C"]
for x,y in combinations(range(0,len(a),2),2): #take combinations in sets of 2 from the even indices
    if a[x] == a[y]:
        empty += (a[x+1], a[y+1]) #this could fail if you have an odd number of items in the list a

如果要在匹配单个对后停止,只需在“If”语句中的
中添加项后添加
中断

尝试检查重复项

>>> def checkDuplicate(List):
    duplicate={}
    for i in List:
            ## checking whether the item is already present in dictionary or not
            ## increasing count if present
            ## initializing count to 1 if not present

        duplicate[i]=duplicate.get(i,0)+1

    return [k for k,v in duplicate.items() if v>1]

>>> checkDuplicate([1,2,3,"s",1,2,3])
[1, 2, 3]

你说的“由于缩进错误而不让我发布”是什么意思?据我所知,SO从未做过任何缩进测试(不幸的是,有很多用户发布完全没有缩进的代码)。不要指望其他人从屏幕截图中转录您的代码来解决您的问题。您有代码:将其粘贴到您的问题中。@WillemVanOnsem在图像中查看代码。我张贴了完全相同的东西,不断地被扔到红色的error@khelwood如果真有效果的话,我会的!,它不允许我事后阅读你能找到数组中相同数字的索引吗,所以在a=[1,“a”,2,“B”,1,“C”],它将是索引[0]和索引[4]@非白色现在检查我的答案以消除一个无关变量
match
在您的第一个示例中,您可以使用
否则:在内部循环之后继续\n中断
。@Aaron没有想到这一点。我将把它添加到我的答案中,因为:else:
是很多人经常忘记的事情,有时它非常有用
>>> def checkDuplicate(List):
    duplicate={}
    for i in List:
            ## checking whether the item is already present in dictionary or not
            ## increasing count if present
            ## initializing count to 1 if not present

        duplicate[i]=duplicate.get(i,0)+1

    return [k for k,v in duplicate.items() if v>1]

>>> checkDuplicate([1,2,3,"s",1,2,3])
[1, 2, 3]