要了解:从PHP数组到Python? [英] To understand: From PHP Array to Python?

查看:102
本文介绍了要了解:从PHP数组到Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是常见的任务在PHP和其他编程languages​​.I从PHP开发人员感动。
我想确保这个集合。
任何人都有谁在良好的python,请帮助我了解清楚。
这是从PHP code我的收藏品。

This is Common task In PHP and other programming languages.I moved from PHP developer. I want to make sure with this collections. Anyone have who good in python please help me to understand clearly . This is my collections from PHP code.

<?php
$php = array(1,2,3,4,5,6,7,8,9,10);
for ($i = 0; $i < 10 ; $i ++)
echo $php[$i]."<br>";
?>

=>什么是在Python?

=>What is in Python?

<?php

for ($i = 0; $i < 10 ; $i ++)
echo $php[$i] = $i +1 ;
?>

=>什么是在Python?

=>What is in Python?

<?php
$php = array(1,2,3,4,5,6,7,8,9,10);
foreach ($php as $value)
echo $value."<br>";
?>

=>什么是在Python?

=>What is in Python?

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
?>

=>什么是在Python?

=>What is in Python?

<?php
$arr = array("mot"=>"one", "hai"=>"two","ba"=> "three");
foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}
?>

=>什么是在Python?

=>What is in Python?

<?php
$arr = array("one", "two","three");
while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
}
?>

=>什么是在Python?

=>What is in Python?

<?php
$arr = array("one", "two","three");
while ($element = each($arr)) {
    echo "Key: $element['key']; Value: $element['value']<br />\n";
}
?>

=>什么是在Python?

=>What is in Python?

<?php
$products = array( array("ITL","INTEL","HARD"),
                        array("MIR", "MICROSOFT","SOFT"),
                        array("Py4C", "pythonkhmer.wordpress.com","TUTORIAL")
                         );
for ($row = 0; $row < 3; $row++)
{
    for ($col = 0; $col <3; $col++)
        {
         echo "|".$products[$row][$col];
         }
echo "<br>";
}
?>

=>什么是在Python?

=>What is in Python?

推荐答案

所有这些都是很明显的真。我只列出的 Python化的方法做这些事情。

All of these are quite obvious really. I'm only listing the Pythonic ways to do these things

PHP

$php = array(1,2,3,4,5,6,7,8,9,10);
for ($i = 0; $i < 10 ; $i ++)
echo $php[$i]."<br>";

的Python(一般你的迭代的比在Python列表,而不是通过索引访问):

Python (generally you iterate over lists in Python, instead of accessing by index):

lst = [1,2,3,4,5,6,7,8,9,10]
for item in lst:
    print str(item) + "<br>"

例2

for ($i = 0; $i < 10 ; $i ++)
echo $php[$i] = $i +1 ;

的Python:

Python:

lst = range(1, 11)
for item in lst:
    print item

也许

lst = []
for i in xrange(10):
    lst.append(i + 1)
    print lst[-1]     # prints out last element

例3

$php = array(1,2,3,4,5,6,7,8,9,10);
foreach ($php as $value)
echo $value."<br>";

同1

$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}

的Python:

Python:

lst = [1, 2, 3, 4]
lst = [val*2 for val in lst]

例5

$arr = array("mot"=>"one", "hai"=>"two","ba"=> "three");
foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}

的Python(注意, {...} 创建一个字典 [字典]在Python,而不是一个列表/):

Python (note that {...} creates a dict [dictionary] in Python, not a list/):

dct = {'mot': 'one', 'hai': 'two', 'ba': 'three'}
for key, value in dct.iteritems():
    print "Key: %s; Value: %s<br />" % (key, value)

例6

$arr = array("one", "two","three");
while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
}

的Python:

Python:

lst = ['one', 'two', 'three']
for key, value in enumerate(lst):
    print "Key: %d; Value: %s<br />" % (key, value)

例7

$arr = array("one", "two","three");
while ($element = each($arr)) {
    echo "Key: $element['key']; Value: $element['value']<br />\n";
}

有没有类似这种直接的Python。

There is no direct Python equivalent to this.

$products = array( array("ITL","INTEL","HARD"),
                        array("MIR", "MICROSOFT","SOFT"),
                        array("Py4C", "pythonkhmer.wordpress.com","TUTORIAL")
                         );
for ($row = 0; $row < 3; $row++)
{
    for ($col = 0; $col <3; $col++)
    {
        echo "|".$products[$row][$col];
    }
    echo "<br>";
}

的Python:

Python:

products = [['ITL', 'INTEL', 'HARD'],
    ['MIR', 'MICROSOFT', 'SOFT'],
    ['Py4C', 'pythonkhmer.wordpress.com', 'TUTORIAL']]

for product in products:
    for item in product:
        print '|' + item
    print '<br>'

也许一个更Python版本:

Or maybe a more Pythonic version:

for product in products:
    print '|%s<br>' % ('|'.join(product))

这篇关于要了解:从PHP数组到Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆