[Python] Is there an equivalent of Python's argsort function in DolphinDB?

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to implement the following functionality in DolphinDB:

    Create a dictionary with city names as keys and population counts as values

    Find cities with population over 10 million

    Sort cities by population in descending order

    In Python, this can be easily achieved with:

    # Create dictionary
    city_population = {"New York": 8419000, "Tokyo": 13960000, "London": 8900000}

    # Find cities with population > 10 million
    large_cities = {city: pop for city, pop in city_population.items() if pop > 10000000}
    print("Cities with population > 10M:", large_cities)

    # Sort by population
    sorted_cities = dict(sorted(city_population.items(), key=lambda x: x[1], reverse=True))
    print("Cities sorted by population:", sorted_cities)`


    In DolphinDB, I've attempted:

    // 1. Create dictionary
    cityDict = dict(["New York", "Tokyo", "London"], [8419000, 13960000, 8900000])

    // 2. Find cities with population > 10M
    mask = cityDict.values() > 10000000
    largeCities = dict(cityDict.keys()[mask], cityDict.values()[mask])
    print("Cities with population > 10M: " + string(largeCities))

    // 3. Sort by population (descending)
    sortedValues = cityDict.values().sort(false) // Get sorted values
    sortedKeys = cityDict.keys()[cityDict.values().argsort().reverse()] // Get corresponding sorted keys
    sortedDict = dict(sortedKeys, sortedValues)
    print("Cities sorted by population: " + sortedDict)


    However, I'm getting an error that argsort doesn't exist. How should I properly implement this sorting functionality in DolphinDB?

    Continue reading...

Compartilhe esta Página