Reputation: 23
I'm having a problem in replacing my data. I have a large dataset. lets say I have 15 attributes, the 15th attributes is Label
. I want to change if the data contains botnet
, it will change the whole data into "1". for example the data botnet are here
or we are looking for botnet
, both data will replace to "1"
I've already try using replace
x = "botnet is here and im looking for botnet"
tags = ['botnet']
for tag in tags:
x = x.replace(tag,'')
print(x)
This code only replce word "botnet" But what I want is if the data contain botnet
it will change the whole sentences to "1"
Upvotes: 2
Views: 162
Reputation: 3601
Try this:
label = ['botnet is here' , 'im looking for botnet', 'just a test']
tags='botnet'
for x in label:
if tags in x:
label[label.index(x)]='1'
print(label)
output: only sentences contain 'botnet' are replaced
['1', '1', 'just a test']
Upvotes: 1
Reputation: 357
You should assign string value to tags variable instead of list.
Iterate x not tags.
Try this code.
x = "botnet is here and im looking for botnet"
tags='botnet'
for tag in x.split():
if tag==tags:
x=tag.replace(tag,'1')
print(x)
output of this code is.
1
Upvotes: 0