我在更新数据方面有问题。我有一个大数据集。假设我有15个属性,第15个属性是Label。如果数据包含botnet,我想要更改,它将将整个数据更改为"1“。例如,数据botnet are here或we are looking for botnet,这两个数据都将替换为"1“
我已经试过用替换
x = "botnet is here and im looking for botnet"
tags = ['botnet']
for tag in tags:
x = x.replace(tag,'')
print(x)这段代码只复制单词“僵尸网络”,但我想要的是,如果数据包含botnet,则整个句子将改为"1“。
发布于 2018-11-01 05:57:03
试试这个:
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)输出:只包含“僵尸网络”的句子被替换
['1', '1', 'just a test']发布于 2018-11-01 05:13:58
for tag in tags:
if tag in x:
x = "1"
break发布于 2018-11-01 05:34:57
应该将字符串值赋值给标记变量,而不是list。
迭代x不标记。
试试这段代码。
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)此代码的输出是。
1https://stackoverflow.com/questions/53095477
复制相似问题