将Python代码转换为PHP

时间:2009-10-31 20:38:00

标签: php python

PHP中的以下Python代码是什么?

import sys

li = range(1,777);

def countFigure(li, n):
        m = str(n);
        return str(li).count(m);

# counting figures
for substr in range(1,10):
        print substr, " ", countFigure(li, substr);

通缉输出777

1   258
2   258
3   258
4   258
5   258
6   258
7   231
8   147
9   147

1 个答案:

答案 0 :(得分:1)

我做了任何Python已经有一段时间了,但我认为应该这样做。 如果你能澄清str(li)看起来会有什么帮助。

<?php

$li = implode('', range(1, 776));

function countFigure($li, $n)
{
    return substr_count($li, $n);
}

// counting figures

foreach (range(1, 9) as $substr)
    echo $substr, " ", countFigure($li, $substr), "\n";