首页 > 解决方案 > 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]

标签: pythonarrayslist

解决方案


这是一个应该做你正在寻找的代码块:

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"


推荐阅读