python - Python用值填充列表
问题描述
我想用python中的值填充一个列表,但不确定如何去做。在 Java 中我会使用array.fills
,但我认为 Python 没有类似的功能。
到目前为止,这是我的代码:
import numpy as np
# inserts string
def insert_sequence(str1, str2, int):
str1_split1 = str1[:int]
str1_split2 = str1[int:]
new_string = str1_split1 + str2 + str1_split2
return new_string
#converts to hexadecimal formatting
def convertAddressToHex(address):
if address < 0:
address = address
address = (1 << 20) + address
else:
if (address & (1 << (20 - 1))) != 0:
address = address - (1 << 20)
address = hex(address) # converts to hexadecimal format after calculating 2's complement
if len(address) == 3:
return insert_sequence(address, '00', 2)
if len(address) == 4:
return insert_sequence(address, '0', 2)
if len(address) == 5:
return address
if len(address) > 5:
return address[0:5]
memArray = [[None for _ in range(17)] for _ in range(256)]
for i in range(0, 4095, 16):
for j, row in enumerate(memArray):
for i, _ in enumerate(row):
memArray[j][i//16] = "0000" + convertAddressToHex(i)
print(np.matrix(memArray))
不过,这并没有得到我想要的输出。我只希望列表的第一个“列”具有这样的十六进制值:
[ 000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 010, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 020, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 030, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 040, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 050, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 060, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 070, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 080, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 090, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 0A0, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 0B0, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 0C0, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 0D0, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 0E0, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
[ 0F0, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000]
解决方案
这是一个应该做你正在寻找的代码块:
cols = 17
rows = 16
arr = []
for row_index in range(rows):
row = ["0000"] * cols
row[0] = f"{row_index*16:04X}"
arr.append(row)
print(arr)
这部分:["0000"] * cols
产生一个长度列表,cols
其中每个元素是"0000"
。
推荐阅读
- cmake - CMakeList.txt:142 (find_package) 处的 CMake 错误
- sql-server - SQL Server 连接错误 - 服务器不存在或访问被拒绝
- ios - iOS:UINavigationController的交互弹出手势期间导航栏的全宽自定义titleView闪烁
- roku - 他们在 Roku 的 onkeyEvent() 中调用 init() 是否正确
- android - Android 如何从 firebase-realtime-database 获取单个记录并显示到 FirebaseRecyclerAdapter
- c# - 访问打印机“PrinterName”的设置无效
- asp.net-mvc - Service Worker - 使用动态参数缓存页面
- reactjs - 将 Ionic Native / Cordova 插件与 Ionic (React) & Capacitor 一起使用的正确方法是什么?
- c++ - 什么是地图的*迭代器
? 这不是对 - php - 在 HTML 中包含 PHP 文件时,表格排序不起作用