在列表理解中设置项目索引
Setting index of item in list comprehension
我有以下代码将 IP_TABLE
反转为 FP_TABLE
。
IP_TABLE = [58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7]
FP_TABLE = [0]*64
for n in range(64):
FP_TABLE[IP_TABLE[n]-1] = n+1
这很好用,但是可以使用列表理解吗?我不确定该怎么做,因为索引取决于项目的价值。
这样就可以了,虽然看起来效率很低:
[IP_TABLE.index(n+1)+1 for n in range(64)]
如果你想更有效地做到这一点,你应该求助于 numpy:
import numpy as np
IP_TABLE = np.array(IP_TABLE)
FP_TABLE = np.arange(1,65)[np.argsort(IP_TABLE)]
与@Scott 的回答相比:
#%%timeit
[IP_TABLE.index(n+1)+1 for n in range(64)]
#> 54.2 µs ± 17 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#%%timeit
np.arange(1,65)[np.argsort(IP_TABLE)]
#> 17 µs ± 4.37 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
快3倍!
它的写法可能略有不同,但您的解决方案已经比按顺序搜索每个元素的列表理解更有效。
IP_TABLE = [58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7]
FP_TABLE = [0]*len(IP_TABLE)
for i,p in enumerate(IP_TABLE,1):
FP_TABLE[p-1] = i
print(FP_TABLE)
[40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25]
我有以下代码将 IP_TABLE
反转为 FP_TABLE
。
IP_TABLE = [58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7]
FP_TABLE = [0]*64
for n in range(64):
FP_TABLE[IP_TABLE[n]-1] = n+1
这很好用,但是可以使用列表理解吗?我不确定该怎么做,因为索引取决于项目的价值。
这样就可以了,虽然看起来效率很低:
[IP_TABLE.index(n+1)+1 for n in range(64)]
如果你想更有效地做到这一点,你应该求助于 numpy:
import numpy as np
IP_TABLE = np.array(IP_TABLE)
FP_TABLE = np.arange(1,65)[np.argsort(IP_TABLE)]
与@Scott 的回答相比:
#%%timeit
[IP_TABLE.index(n+1)+1 for n in range(64)]
#> 54.2 µs ± 17 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#%%timeit
np.arange(1,65)[np.argsort(IP_TABLE)]
#> 17 µs ± 4.37 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
快3倍!
它的写法可能略有不同,但您的解决方案已经比按顺序搜索每个元素的列表理解更有效。
IP_TABLE = [58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7]
FP_TABLE = [0]*len(IP_TABLE)
for i,p in enumerate(IP_TABLE,1):
FP_TABLE[p-1] = i
print(FP_TABLE)
[40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25]