Python中的排序DICT
我正在将数据加载到一个python dict中,该dict来自coinmarketcap的api,然后我希望能够按照排名对其进行排序。 我在网上浏览了一下,虽然我看到了一些示例或订购,但我无法让它正常工作,任何帮助都将不胜感激。 有人能帮忙吗Python中的排序DICT,python,sorting,Python,Sorting,我正在将数据加载到一个python dict中,该dict来自coinmarketcap的api,然后我希望能够按照排名对其进行排序。 我在网上浏览了一下,虽然我看到了一些示例或订购,但我无法让它正常工作,任何帮助都将不胜感激。 有人能帮忙吗 { 'data': { 1: { u'last_updated': 1527886475, u'name': u'Bitcoin', u'symbol': u'BTC', u'rank': 1,
{
'data': {
1: {
u'last_updated': 1527886475,
u'name': u'Bitcoin',
u'symbol': u'BTC',
u'rank': 1,
u'total_supply': '17068825.0',
u'quotes': {
u'USD': {
u'market_cap': '127372692798.0',
u'percent_change_7d': '0.0',
u'price': '7462.3',
u'percent_change_24h': '-1.29',
u'volume_24h': '4962840000.0',
u'percent_change_1h': '0.03'
}
},
u'max_supply': '21000000.0',
u'circulating_supply': '17068825.0',
u'website_slug': u'bitcoin',
u'id': 1
},
825: {
u'last_updated': 1527886449,
u'name': u'Tether',
u'symbol': u'USDT',
u'rank': 13,
u'total_supply': '2830109970.0',
u'quotes': {
u'USD': {
u'market_cap': '2508920883.0',
u'percent_change_7d': '-0.03',
u'price': '1.00071',
u'percent_change_24h': '0.01',
u'volume_24h': '2542980000.0',
u'percent_change_1h': '0.1'
}
},
u'max_supply': None,
u'circulating_supply': '2507140814.0',
u'website_slug': u'tether',
u'id': 825
},
1027: {
u'last_updated': 1527886459,
u'name': u'Ethereum',
u'symbol': u'ETH',
u'rank': 2,
u'total_supply': '99807496.0',
u'quotes': {
u'USD': {
u'market_cap': '56961236045.0',
u'percent_change_7d': '-2.88',
u'price': '570.711',
u'percent_change_24h': '-1.62',
u'volume_24h': '1986760000.0',
u'percent_change_1h': '-0.39'
}
},
u'max_supply': None,
u'circulating_supply': '99807496.0',
u'website_slug': u'ethereum',
u'id': 1027
}
}
}
您不能订购
目录
,但可以创建项目列表
,并对其进行排序:
sorted(data.iteritems(), key=lambda x:x[1][u'rank'])
您不能订购
目录
,但可以创建项目列表
,并对其进行排序:
sorted(data.iteritems(), key=lambda x:x[1][u'rank'])
使用
collections.OrderedDict
演示:
from collections import OrderedDict
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'])) )
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'], reverse=True)) ) #Descending order
OrderedDict([(1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'})])
OrderedDict([(825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'})])
输出:
from collections import OrderedDict
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'])) )
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'], reverse=True)) ) #Descending order
OrderedDict([(1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'})])
OrderedDict([(825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'})])
使用
collections.OrderedDict
演示:
from collections import OrderedDict
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'])) )
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'], reverse=True)) ) #Descending order
OrderedDict([(1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'})])
OrderedDict([(825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'})])
输出:
from collections import OrderedDict
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'])) )
print( OrderedDict(sorted(data['data'].items(), key=lambda (x, y): y['rank'], reverse=True)) ) #Descending order
OrderedDict([(1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'})])
OrderedDict([(825, {u'total_supply': '2830109970.0', u'quotes': {u'USD': {u'market_cap': '2508920883.0', u'percent_change_7d': '-0.03', u'price': '1.00071', u'percent_change_1h': '0.1', u'volume_24h': '2542980000.0', u'percent_change_24h': '0.01'}}, u'max_supply': None, u'last_updated': 1527886449, u'name': u'Tether', u'website_slug': u'tether', u'symbol': u'USDT', u'id': 825, u'rank': 13, u'circulating_supply': '2507140814.0'}), (1027, {u'total_supply': '99807496.0', u'quotes': {u'USD': {u'market_cap': '56961236045.0', u'percent_change_7d': '-2.88', u'price': '570.711', u'percent_change_1h': '-0.39', u'volume_24h': '1986760000.0', u'percent_change_24h': '-1.62'}}, u'max_supply': None, u'last_updated': 1527886459, u'name': u'Ethereum', u'website_slug': u'ethereum', u'symbol': u'ETH', u'id': 1027, u'rank': 2, u'circulating_supply': '99807496.0'}), (1, {u'total_supply': '17068825.0', u'quotes': {u'USD': {u'market_cap': '127372692798.0', u'percent_change_7d': '0.0', u'price': '7462.3', u'percent_change_1h': '0.03', u'volume_24h': '4962840000.0', u'percent_change_24h': '-1.29'}}, u'max_supply': '21000000.0', u'last_updated': 1527886475, u'name': u'Bitcoin', u'website_slug': u'bitcoin', u'symbol': u'BTC', u'id': 1, u'rank': 1, u'circulating_supply': '17068825.0'})])
您可能需要查看
enumerate()
或dict.iteritems()
方法。您可能需要查看enumerate()
或dict.iteritems()
方法。