Python列表理解是非常简单的: >>> l = [x ** 2 for x in range(10) if x % 2 == 0]
>>> [0, 4, 16, 36, 64]
不生锈具有等同的语法,如: let vector = vec![x for x in (1..10) if x % 2 == 0]
// [2, 4, 6, 8]
试图编写一个列表理解,该列表理解可以从input()中迭代一个字符串,并创建一个具有自己索引的字符串的每个字符的列表。 从本质上说,我希望有一个功能,这是否: x = ["00.00"[h] for h in range(len("00.00"))]
print(x)
> ['0', '0', '.', '0', '0']
在那里把一个实际字符串(“00.00”)时,蟒蛇也正是我想要它做的
有人可以简化这段代码背后的逻辑: 我试图推行它像这样 for others in prefs:
if others!=person:
scores=[similarity(prefs,person, others),others]
但只挑选别人的最后一个元素。 Btw prefs是一个2D字典,分数应该是元组列表。
我想知道是否有可能取代这个: results = [x for x in os.listdir(dir_path) if os.path.splitext(x)[1] == ".tif" and "abc" in os.path.splitext(x)[0]]
的东西,如: results = [x for x in os.listdir(dir_path) if ext == ".tif"