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?
The following code is equivalent of Python's argsort in DolphinDB. It is the step-by-step implementation with ordered dictionaries
// 1. Create ordered dictionary (preserves insertion order)
// NOTE: DolphinDB dictionaries are unordered by default
cityDict = dict(
["New York", "Tokyo", "London"], // Dictionary keys (city names)
[8419000, 13960000, 8900000], // Dictionary values (population numbers)
ordered=true // Enable insertion order tracking
)
// 2. Find cities with population over 10 million (Boolean masking)
// Vectorized operation similar to NumPy/Pandas
mask = cityDict.values() > 10000000 // Create boolean mask
largeCities = dict(
cityDict.keys()[mask], // Filter keys using mask
cityDict.values()[mask] // Filter values using same mask
)
print("Cities with population > 10M: " + string(largeCities))
// 3. Sort by population descending using table operations
// Convert dictionary to table for sorting functionality
t = table(
cityDict.keys() as city, // Create 'city' column from keys
cityDict.values() as population // Create 'population' column from values
)
t.sortBy!(`population, false) // In-place descending sort by population
// Rebuild ordered dictionary from sorted table
// Maintains sort order through ordered=true
sortedDict = dict(
t.city, // Sorted city names
t.population, // Sorted population numbers
ordered=true // Preserve new order
)
print("Cities sorted by population: " + string(sortedDict))
/* Sample Output:
Cities with population > 10M: Tokyo->13960000
Cities sorted by population: Tokyo->13960000
London->8900000
New York->8419000
*/