I have two lists which are following.
characteristic = ['length', 'width', 'height', 'Thread length', 'space']
value = ['length 34','width ab23','Thread length 8ah','space','height 099']
I have written a loop.
temp_str = {}
for x in characteristic:
for z in value:
if x in z:
temp_str = z.replace(x,'')
temp_str = ','
print(temp_str)
I am getting output:
34,
Thread 8ah,
ab23,
099,
8ah,
,
but in real I want output like following
34,ab23,099,8ah,
CodePudding user response:
Try the following:
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)
Output:
34,ab23,099,8ah,
Note: space
was ignored because it was not included in your required output
CodePudding user response:
You could use a nested list comprehension, checking if the values in value
start with the characteristics from characteristic
:
result = ','.join(v.replace(c, '') for c in characteristic for v in value if v.startswith(c))
Output:
34, ab23, 099, 8ah,
Note that space
appears in value
with no value attached; hence the empty field in the string.