2017-08-01 96 views

回答

3

您可以調用tf.unique後使用tf.pad墊零點。

x = tf.placeholder(tf.int32, shape=(None)) 
y, idx = tf.unique(x) 
y = tf.pad(y,[[0,(tf.shape(x) - tf.shape(y))[0]]]) 

sess = tf.InteractiveSession() 
print(sess.run(y, {x:np.random.randint(0,10, (10), dtype=np.int32)})) 
1

你需要單獨處理的問題。

y = [1, 2, 4, 7, 8] 
idx = [0, 0, 1, 2, 2, 2, 3, 4, 4] 

for ii in range(0, len(idx) - len(y)): 
    y.append(0) 

print(y) 

輸出是

[1, 2, 4, 7, 8, 0, 0, 0, 0] 
0

使用設置,找到獨特的..diff LEN和追加尾隨0

x = [1, 1, 2, 4, 4, 4, 7, 8, 8] 
length = len(x) 

print(length) 
y = list(set(x)) 

print(y) 
pad = length - len(list(set(x))) 

for index in range(0,pad): 
    y.append(0) 


print(y) 

輸出

9 
[1, 2, 4, 7, 8] 
[1, 2, 4, 7, 8, 0, 0, 0, 0] 
相關問題