我有兩個串列如下。
characteristic = ['length', 'width', 'height', 'Thread length', 'space']
value = ['length 34','width ab23','Thread length 8ah','space','height 099']
我寫了一個回圈。
temp_str = {}
for x in characteristic:
for z in value:
if x in z:
temp_str = z.replace(x,'')
temp_str = ','
print(temp_str)
我得到輸出:
34,
Thread 8ah,
ab23,
099,
8ah,
,
但實際上我想要如下輸出
34,ab23,099,8ah,
uj5u.com熱心網友回復:
嘗試以下操作:
characteristic = ['length', 'width', 'height', 'Thread length', 'space']
value = ['length 34','width ab23','Thread length 8ah','space','height 099']
temp_str = ''
for x in characteristic:
for z in value:
if z.startswith(x) and z!=x:
temp_str = z.split(' ')[-1]
temp_str = ','
print(temp_str)
輸出:
34,ab23,099,8ah,
注意:space被忽略,因為它未包含在您所需的輸出中
uj5u.com熱心網友回復:
您可以使用嵌套串列推導,檢查中的值是否value以以下特征開頭characteristic:
result = ','.join(v.replace(c, '') for c in characteristic for v in value if v.startswith(c))
輸出:
34, ab23, 099, 8ah,
請注意,space出現在value沒有附加值的情況下;因此字串中的空欄位。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/466089.html
下一篇:我不知道如何制作這3列布局
