我已经能够像这样连接字符串了
def masks_to_onehots_tf(tag_masks, tag_class_indices, num_classes):
def onehotify(pixel_tag_masks):
tag_mask_sizes_nozeroidx = tf.where(tf.not_equal(tag_mask_sizes, 0))
tag_mask_sizes_suppressed = tf.gather_nd(tag_mask_sizes, tag_mask_sizes_nozeroidx)
smallest_mask_index = tag_mask_sizes_nozeroidx[tf.argmin(tag_mask_sizes_suppressed)]
onehot = tf.one_hot(smallest_mask_index[0], depth=num_classes, dtype=tf.uint8)
return onehot
tag_mask_sizes = tf.reduce_sum(tag_masks, axis=[1, 2])
image_masks = tf.transpose(tag_masks, perm=[1, 2, 0])
onehots = tf.map_fn(lambda x: tf.map_fn(onehotify, x), image_masks)
return onehots
有没有更有效的方法?我想避免for循环。 我在考虑zip或枚举之类的东西,但它们似乎创建元组而不是连接字符串。
结果应如下所示:
string_right = list(range(1,100))
string_left = ['AAA','BBB', "CCC"]
combination = []
for i in headers_right:
merged1 = string_left[0] + ' ' + str(i)
merged2 = string_left[1] + ' ' + str(i)
merged3 = string_left[2] + ' ' + str(i)
combination.append(merged1)
combination.append(merged2)
combination.append(merged3)
非常感谢!
答案 0 :(得分:3)
您可以为此使用itertools.product,这实际上给出了两个列表的笛卡尔积,然后我们从该乘积中获取每个元素,然后将这两个元素结合在一起。
例如
In [2]: from itertools import product
In [1]: string_right = range(1,3)
...: string_left = ['AAA','BBB', "CCC"]
In [3]: list(product(string_left, string_right))
Out[3]: [('AAA', 1), ('AAA', 2), ('BBB', 1), ('BBB', 2), ('CCC', 1), ('CCC', 2)]
如果使用这种方法,也不需要将range
转换为列表,因为itertools.product
将迭代器作为输入
from itertools import product
string_right = range(1,100)
string_left = ['AAA','BBB', "CCC"]
#Take cartesian product of the two lists, and join elements together of each tuple
print([ '{}{}'.format(j,i) for i,j in product(string_right,string_left)])
输出将为
['AAA1', 'BBB1', 'CCC1', 'AAA2', 'BBB2', 'CCC2', 'AAA3', 'BBB3', 'CCC3', 'AAA4', 'BBB4', 'CCC4', 'AAA5', 'BBB5', 'CCC5', 'AAA6', 'BBB6', 'CCC6', 'AAA7', 'BBB7', 'CCC7', 'AAA8', 'BBB8', 'CCC8', 'AAA9', 'BBB9', 'CCC9', 'AAA10', 'BBB10', 'CCC10', 'AAA11', 'BBB11', 'CCC11', 'AAA12', 'BBB12', 'CCC12', 'AAA13', 'BBB13', 'CCC13', 'AAA14', 'BBB14', 'CCC14', 'AAA15', 'BBB15', 'CCC15', 'AAA16', 'BBB16', 'CCC16', 'AAA17', 'BBB17', 'CCC17', 'AAA18', 'BBB18', 'CCC18', 'AAA19', 'BBB19', 'CCC19', 'AAA20', 'BBB20', 'CCC20', 'AAA21', 'BBB21', 'CCC21', 'AAA22', 'BBB22', 'CCC22', 'AAA23', 'BBB23', 'CCC23', 'AAA24', 'BBB24', 'CCC24', 'AAA25', 'BBB25', 'CCC25', 'AAA26', 'BBB26', 'CCC26', 'AAA27', 'BBB27', 'CCC27', 'AAA28', 'BBB28', 'CCC28', 'AAA29', 'BBB29', 'CCC29', 'AAA30', 'BBB30', 'CCC30', 'AAA31', 'BBB31', 'CCC31', 'AAA32', 'BBB32', 'CCC32', 'AAA33', 'BBB33', 'CCC33', 'AAA34', 'BBB34', 'CCC34', 'AAA35', 'BBB35', 'CCC35', 'AAA36', 'BBB36', 'CCC36', 'AAA37', 'BBB37', 'CCC37', 'AAA38', 'BBB38', 'CCC38', 'AAA39', 'BBB39', 'CCC39', 'AAA40', 'BBB40', 'CCC40', 'AAA41', 'BBB41', 'CCC41', 'AAA42', 'BBB42', 'CCC42', 'AAA43', 'BBB43', 'CCC43', 'AAA44', 'BBB44', 'CCC44', 'AAA45', 'BBB45', 'CCC45', 'AAA46', 'BBB46', 'CCC46', 'AAA47', 'BBB47', 'CCC47', 'AAA48', 'BBB48', 'CCC48', 'AAA49', 'BBB49', 'CCC49', 'AAA50', 'BBB50', 'CCC50', 'AAA51', 'BBB51', 'CCC51', 'AAA52', 'BBB52', 'CCC52', 'AAA53', 'BBB53', 'CCC53', 'AAA54', 'BBB54', 'CCC54', 'AAA55', 'BBB55', 'CCC55', 'AAA56', 'BBB56', 'CCC56', 'AAA57', 'BBB57', 'CCC57', 'AAA58', 'BBB58', 'CCC58', 'AAA59', 'BBB59', 'CCC59', 'AAA60', 'BBB60', 'CCC60', 'AAA61', 'BBB61', 'CCC61', 'AAA62', 'BBB62', 'CCC62', 'AAA63', 'BBB63', 'CCC63', 'AAA64', 'BBB64', 'CCC64', 'AAA65', 'BBB65', 'CCC65', 'AAA66', 'BBB66', 'CCC66', 'AAA67', 'BBB67', 'CCC67', 'AAA68', 'BBB68', 'CCC68', 'AAA69', 'BBB69', 'CCC69', 'AAA70', 'BBB70', 'CCC70', 'AAA71', 'BBB71', 'CCC71', 'AAA72', 'BBB72', 'CCC72', 'AAA73', 'BBB73', 'CCC73', 'AAA74', 'BBB74', 'CCC74', 'AAA75', 'BBB75', 'CCC75', 'AAA76', 'BBB76', 'CCC76', 'AAA77', 'BBB77', 'CCC77', 'AAA78', 'BBB78', 'CCC78', 'AAA79', 'BBB79', 'CCC79', 'AAA80', 'BBB80', 'CCC80', 'AAA81', 'BBB81', 'CCC81', 'AAA82', 'BBB82', 'CCC82', 'AAA83', 'BBB83', 'CCC83', 'AAA84', 'BBB84', 'CCC84', 'AAA85', 'BBB85', 'CCC85', 'AAA86', 'BBB86', 'CCC86', 'AAA87', 'BBB87', 'CCC87', 'AAA88', 'BBB88', 'CCC88', 'AAA89', 'BBB89', 'CCC89', 'AAA90', 'BBB90', 'CCC90', 'AAA91', 'BBB91', 'CCC91', 'AAA92', 'BBB92', 'CCC92', 'AAA93', 'BBB93', 'CCC93', 'AAA94', 'BBB94', 'CCC94', 'AAA95', 'BBB95', 'CCC95', 'AAA96', 'BBB96', 'CCC96', 'AAA97', 'BBB97', 'CCC97', 'AAA98', 'BBB98', 'CCC98', 'AAA99', 'BBB99', 'CCC99']