# 数字字符串转换为int并添加到列表中（优化问题）

##### 问题描述投票：2回答：2

`'b'`的唯一限制是与`'a'`至少有1个索引。

``````lineOfNums = input('Line of numbers: ') # User input: example - 4 2 6 8
arrNums = lineOfNums.split()
conMet = False # Is the condition met at least once

for a in range(0, len(arrNums)):
for b in range(a + 1, len(arrNums)):
for c in range(0, len(arrNums)):
if int(arrNums[a]) + int(arrNums[b]) == int(arrNums[c]):
print(f'{arrNums[a]} + {arrNums[b]} == {arrNums[c]}')
conMet = True

if conMet == False: print('No')
``````
python arrays
##### 2个回答
1

``````arrNum = [int(x) for x in lineOfNums.split()]
``````

``````for a in range(0, len(arrNums)-1):
for b in range(a + 1, len(arrNums)):
for c in range(0, len(arrNums)):
if arrNums[a] + arrNums[b] == arrNums[c]:
print(f'{arrNums[a]} + {arrNums[b]} == {arrNums[c]}')
conMet = True
``````

0

`from itertools import combinations`

``````# Convert to int
arr= [int(i) for i in arrNums]

# Get all the combinations
psums = {sum(i): i for i in combinations(arr, 2)}

# Then loop once
for i, v in enumerate(arr):
if v in psums:
print(f'{psums[v][0]} + {psums[v][1]} == {v}')
``````