duanhe4155 2009-10-31 20:37
浏览 80
已采纳

将Python代码转换为PHP

What is the following Python code in PHP?

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);

Wanted output for 777

1   258
2   258
3   258
4   258
5   258
6   258
7   231
8   147
9   147
  • 写回答

1条回答 默认 最新

  • doumanni3501 2009-10-31 20:45
    关注

    It's been a while since I did any Python but I think this should do it. If you could clarify what str(li) looks like it would help.

    <?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), "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动