用Python编写程序以打印给定序列中具有排序后的不同值的数字索引数组
假设您有一个序列,并且具有排序后的不同值的数字索引为-
Sorted distict values - numeric array index [2 3 0 3 2 1 4] ['apple' 'kiwi' 'mango' 'orange' 'pomegranate']
为了解决这个问题,我们将遵循以下步骤-
解决方案
在非唯一元素列表中应用函数,并将其另存为index,index_value。pd.factorize()
index,unique_value = pd.factorize(['mango','orange','apple','orange','mango','kiwi','pomegranate'])
打印索引和元素。在不区分不同值及其索引的情况下显示结果
在列表元素内部应用并设置sort=True,然后将其另存为sorted_index,unique_valuepd.factorize()
sorted_index,unique_value = pd.factorize(['mango','orange','apple','orange','mango','kiwi','pomegranate'],sort=True)
最后打印数字索引和不同的值
例子
让我们看下面的代码以获得更好的理解-
import pandas as pd
index,unique_value =
pd.factorize(['mango','orange','apple','orange','mango','kiwi','pomegranate'])
print("Without sorting of distict values-numeric array index")
print(index)
print(unique_value)
print("Sorted distict values - numeric array index")
sorted_index,unique_value =
pd.factorize(['mango','orange','apple','orange','mango','kiwi','pomegranate'],sort=True)
print(sorted_index)
print(unique_value)输出
Without sorting of distict values-numeric array index [0 1 2 1 0 3 4] ['mango' 'orange' 'apple' 'kiwi' 'pomegranate'] Sorted distict values - numeric array index [2 3 0 3 2 1 4] ['apple' 'kiwi' 'mango' 'orange' 'pomegranate']
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短