我想从以下列表中获取唯一值:
['nowplaying', 'PBS', 'PBS', 'nowplaying', 'job', 'debate', 'thenandnow']
Run Code Online (Sandbox Code Playgroud)
我需要的输出是:
['nowplaying', 'PBS', 'job', 'debate', 'thenandnow']
Run Code Online (Sandbox Code Playgroud)
此代码有效:
output = []
for x in trends:
if x not in output:
output.append(x)
print(output)
Run Code Online (Sandbox Code Playgroud)
我应该使用更好的解决方案吗?
['b','b','b','a','a','c','c']
Run Code Online (Sandbox Code Playgroud)
numpy.unique给出
['a','b','c']
Run Code Online (Sandbox Code Playgroud)
如何保留原始订单
['b','a','c']
Run Code Online (Sandbox Code Playgroud)
很棒的答案.奖金问题.为什么这些方法都不适用于此数据集?http://www.uploadmb.com/dw.php?id=1364341573这是numpy排序奇怪行为的问题
我只需要删除在数组中重复但保留其中一行的行,我不能使用unique,因为我需要维护顺序.例
1 a234 125
1 a123 265
1 a234 125
1 a145 167
1 a234 125
2 a189 547
2 a189 547
3 a678 567
3 a357 569
Run Code Online (Sandbox Code Playgroud)
我需要这个输出
1 a234 125
1 a123 265
1 a145 167
2 a189 547
3 a678 567
3 a357 569
Run Code Online (Sandbox Code Playgroud) 在Python中,我们可以使用列表中的唯一项set(list).但是,这样做会破坏值在原始列表中出现的顺序.是否有一种优雅的方式来获取列表中显示的顺序中的唯一项目.
我希望确定 sklearn LabelEncoder 的标签(即 0,1,2,3,...)以适应分类变量可能值的特定顺序(例如 ['b', 'a', 'c', 'd'])。LabelEncoder 选择按字典序拟合标签,我想可以在这个例子中看到:
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
le.fit(['b', 'a', 'c', 'd' ])
le.classes_
array(['a', 'b', 'c', 'd'], dtype='<U1')
le.transform(['a', 'b'])
array([0, 1])
Run Code Online (Sandbox Code Playgroud)
我怎样才能强制编码器坚持在 .fit 方法中第一次遇到的数据顺序(即,将“b”编码为 0,“a”编码为 1,“c”编码为 2,“d”编码为3)?