首页 > 解决方案 > 将元素放入 for lob Python 3 的新数组中

问题描述

我正在开发一个 Python 3 项目。我的代码更长,但我为您准备了我的代码的基本示例,它可以工作。

arr = []
number = (["01", "02", "03", "06", "08"])
arr = number
pos1 = int(arr[1],16)
pos2 = 2 + int(arr[1],16)

for s in range(pos1,pos2):
  print(arr[s])

说明:
您看到的数字,它们是十六进制数字,我将其转换pos1int2在其上加上pos2. 过了一会儿,我得到了03 06. 我想在不同的数组中使用这些数字。有没有办法放入03 06一个新的数组?

标签: pythonarrayspython-3.x

解决方案


如果我理解正确,那么试试这个:

arr2 = []
number = (["01", "02", "03", "06", "08"])
arr = number[:]
pos1 = int(arr[1],16)
pos2 = 2 + int(arr[1],16)

for s in range(pos1,pos2):
    arr2.append(arr[s]) # add numbers to arr2
print(arr2[0])

推荐阅读