小编use*_*230的帖子

如何使用循环python构建列表

如何在python循环中获取项目以构建列表.在PHP中我会使用这样的东西:

$ar1 = array("Bobs","Sams","Jacks"); 
foreach ($ar1 as $ar2){ 
$ar3[] = "$ar2 array item"; }

print_r($ar3);
Run Code Online (Sandbox Code Playgroud)

哪个产生

Array ( [0] => Bobs array item [1] => Sams array item [2] => Jacks array item )
Run Code Online (Sandbox Code Playgroud)

$ar3[] 将项目存储在数组中的foreach中.

在Python中我试过:

list1 = ("Bobs","Sams","Jacks"); 
foreach list2 in list1:
    list3 = list2 + " list item"

print list3
Run Code Online (Sandbox Code Playgroud)

哪个产生

Jacks list item
Run Code Online (Sandbox Code Playgroud)

但它不会list3作为列表返回,list3[]也不起作用.如何让循环进入列表?

python loops store list

-6
推荐指数
1
解决办法
4万
查看次数

标签 统计

list ×1

loops ×1

python ×1

store ×1