我的代码是:
for datum in data :
sick.append(datum[2])
,结果是
sick=['J02', 'J02', 'J20', 'J20', 'J20', 'J20', 'J20', 'A08', 'J45','A08']
我想要的是
if sick[i] == sick[i+1], don't appned value
所以我想要的结果是
sick=['J02', 'J20', 'A08', 'J45','A08'] <-- this isn't removing duplicates. I just don't want to append "if sick[i] == sick[i+1]"
我该怎么办?
答案 0 :(得分:1)
我的建议:
for datum in data :
if not datum in sick:
sick.append(datum)
答案 1 :(得分:1)
您可以使用以下
data = ['J02', 'J02', 'J20', 'J20', 'J20', 'J20', 'J20', 'A08', 'J45']
sick=[]
for datum in data:
if datum not in data:
sick.append(datum)
这将给出输出:
['J02', 'J20', 'A08', 'J45']
答案 2 :(得分:1)
如果应该允许重复,那么这将起作用:
for datum in data:
if len(sick) == 0 or sick[-1] != datum:
sick.append(datum)