在python中,如何使for循环与内部for循环在数组上同时迭代?

在python中,如何使for循环与内部for循环在数组上同时迭代?,python,for-loop,iteration,counting,Python,For Loop,Iteration,Counting,我正试图做一个程序,可以通过逐字计数来增加。但为此,我有两个for循环,它们需要一起工作。例如,如果我输入3和2,则外部for循环在数组中迭代到“3”,然后另一个for循环在数组中迭代到“2”,这样外部for循环应该(但不)与它一起迭代,并最终打印出它所在的位置(应该是5)。我怎样才能做到这一点?因为现在内部循环将完成其迭代并中断 arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] #print(arr[0]) a = str(inp

我正试图做一个程序,可以通过逐字计数来增加。但为此,我有两个
for
循环,它们需要一起工作。例如,如果我输入3和2,则外部
for
循环在数组中迭代到“3”,然后另一个for循环在数组中迭代到“2”,这样外部for循环应该(但不)与它一起迭代,并最终打印出它所在的位置(应该是5)。我怎样才能做到这一点?因为现在内部循环将完成其迭代并中断

arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
#print(arr[0])
a = str(input()) #first number
b = str(input()) #second number

for i in arr:
    if i == a:
        for j in arr:
            if j == b:
                print(i)
                break

该程序为输入3和2输出3,但我想要5,您可以使用另一个变量跟踪计数,如下所示:

arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

a = str(input())  # first number
b = str(input())  # second number

counter = 0
for i in arr:
    if i == a:
        for j in arr:
            if j == b:
                print(counter)
                break
            counter += 1
    counter += 1
我们可以编写一个程序来实现类似的行为,非常简单:

a = int(input())  # first number
b = int(input())  # second number

counter = 0

for _ in range(a):
    counter += 1
for _ in range(b):
    counter += 1
print(counter)

这样做的好处是,我们不局限于
arr

中的输入,您可以使用另一个变量来跟踪计数,如下所示:

arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

a = str(input())  # first number
b = str(input())  # second number

counter = 0
for i in arr:
    if i == a:
        for j in arr:
            if j == b:
                print(counter)
                break
            counter += 1
    counter += 1
我们可以编写一个程序来实现类似的行为,非常简单:

a = int(input())  # first number
b = int(input())  # second number

counter = 0

for _ in range(a):
    counter += 1
for _ in range(b):
    counter += 1
print(counter)
这样做的好处是,我们不局限于
arr

中的输入。以下是我的解决方案:

arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
num_list = list(range(int(arr[-1])*2+1))

new_dict = dict()
for index, value in enumerate(num_list):
    new_dict[value] = set()
    for item1 in arr:
        for item2 in arr:
            if int(value) == int(item1) + int(item2):
                new_dict[value].add((item1, item2))

a = str(input())  # first number
b = str(input())  # second number

target = (a, b)

for key, value in new_dict.items():
    if target in value:
        print(int(key))
希望能有帮助。我感兴趣的是,你是否可以不使用任何“+”来实现目标。如果你找到了,请分享。提前谢谢

以下是我的解决方案:

arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
num_list = list(range(int(arr[-1])*2+1))

new_dict = dict()
for index, value in enumerate(num_list):
    new_dict[value] = set()
    for item1 in arr:
        for item2 in arr:
            if int(value) == int(item1) + int(item2):
                new_dict[value].add((item1, item2))

a = str(input())  # first number
b = str(input())  # second number

target = (a, b)

for key, value in new_dict.items():
    if target in value:
        print(int(key))

希望能有帮助。我感兴趣的是,你是否可以不使用任何“+”来实现目标。如果你找到了,请分享。提前谢谢

你能发布一个示例输出吗???@abheet22我想要的输出或者这个代码给出的输出?我希望它为3和2输出5,但这会为所提到的输入输出3。最简单的方法是使用一个变量,该变量随着第一个和第二个循环的每次迭代而递增。
input
已返回一个
str
值;没有必要在上面调用
str
。你能发布一个示例输出吗???@abheet22我想要的输出或者这个代码给出的输出?我希望它为3和2输出5,但这会为所提到的输入输出3。最简单的方法是使用一个变量,该变量随着第一个和第二个循环的每次迭代而递增。
input
已返回一个
str
值;没有必要在上面调用
str
。我想制作一个可以直接添加的程序,但是如果我简单地使用内置的add,它将无法达到目的,因为这样我就可以简单地添加
print(3+2)
。我希望它只迭代数组来给出答案。请你找出一种不使用+运算符的方法,因为使用它来实现加法是自动的,而且我还想为它添加+本身可以实现的携带功能。我想制作一个可以直接添加的程序,但如果我简单地使用内置的add,它将失败因为这样我就可以简单地添加
print(3+2)
。我希望它只迭代数组来给出答案。请您找出一种不使用+运算符的方法,因为使用它来实现加法是自动的,而且我还希望向它添加进位功能,而+本身就是这样做的。