Home > Backend Development > PHP Tutorial > 会php和python的大神进来帮忙转换一段代码

会php和python的大神进来帮忙转换一段代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:09:06
Original
1374 people have browsed it

求把这段php代码转成python的代码,谢谢!

<code>//倒序排序
function my_sort($a,$b)
{
    if ($a==$b) return 0;
   return ($a5,'bbb'=>3,'ccc'=>4);

usort($arr,"my_sort");

echo json_encode($arr);
</code>
Copy after login
Copy after login

简单说就是数组倒序排序,然后转成json格式。

回复内容:

求把这段php代码转成python的代码,谢谢!

<code>//倒序排序
function my_sort($a,$b)
{
    if ($a==$b) return 0;
   return ($a5,'bbb'=>3,'ccc'=>4);

usort($arr,"my_sort");

echo json_encode($arr);
</code>
Copy after login
Copy after login

简单说就是数组倒序排序,然后转成json格式。

PHP 中的 associative array 是一種 ordered mapping (有序映射).
這代表了 Python 中的 dictionary 並非完全相等於 associative array.

其次, json 據我所知並不支援 ordered mapping,所以如果你想要完成這項任務可能要:

  1. 使用 Python 中的有序映射對象: OrderedDict (請參考OrderedDict)

  2. OrderedDict 轉為 list 再轉為 json

  3. 到時候要使用該項資料時,必須從 json 中 load 進 list 再轉回 OrderedDict


以下是 Python3 的代碼讓你參考:

代碼:

<code>import json
from collections import OrderedDict

# using OrderedDict
arr = {"aaa":5,"bbb":3,"ccc":4, "ddd":7}
arr = OrderedDict(sorted(arr.items(), key=lambda item: item[1], reverse=True))
# or you can create an OrderedDict directly:
# arr = OrderedDict([('aaa', 5), ('bbb', 3), ('ccc', 4), ('ddd', 7)])
print(arr)

# list
arr = list(arr.items())
print(arr)

# json dump
json_arr = json.dumps(arr)
print(json_arr)

# json load
arr = OrderedDict(json.loads(json_arr))
print(arr)
</code>
Copy after login

結果:

<code>OrderedDict([('ddd', 7), ('aaa', 5), ('ccc', 4), ('bbb', 3)])
[('ddd', 7), ('aaa', 5), ('ccc', 4), ('bbb', 3)]
[["ddd", 7], ["aaa", 5], ["ccc", 4], ["bbb", 3]]
OrderedDict([('ddd', 7), ('aaa', 5), ('ccc', 4), ('bbb', 3)])
</code>
Copy after login

P.S. 任何不清楚的地方都歡迎用評論告訴我,我們可以再討論

<code class="php">import json
arr={"aaa":5,"bbb":3,"ccc":4}
print json.dumps(sorted(arr.values(),reverse=True))#'[5, 4, 3]'</code>
Copy after login

python代码(改造后)

<code>#!/usr/bin/env python
#encoding:utf-8

import json

if __name__ == '__main__':
    myDict = {'aaa':5,'bbb':6,'ccc':777}
    outDic = sorted(myDict.iteritems(), key=lambda asd: asd[1], reverse=True)
    print '排序前的字典,类似于php的array'
    print myDict
    print '排序后json输出:'
    print json.dumps(outDic)
    
</code>
Copy after login

输出:

<code>/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/luyh/www/python/lesson1/pysort.py
排序前的字典,类似于php的array
{'aaa': 5, 'bbb': 6, 'ccc': 777}
排序后json输出:
[["ccc", 777], ["bbb", 6], ["aaa", 5]]

Process finished with exit code 0
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template