嵌套列表中的Python元素不会改变

时间:2016-11-28 22:50:01

标签: python list

当我打印$user->save(); //attach security group $security_group=$input['security_group_id']; if(is_array($security_group)){ foreach($security_group as $key=>$val){ $user->SecurityGroup()->attach($val,['company_id' => Session::get('selected_company_id')]); } } 时,它会将元素更改为"< - 您在这里"。

当我调用gb0函数时,它不会改变任何内容。

如何更改代码以更改内容?

board

2 个答案:

答案 0 :(得分:0)

def board():

place = 0


gb = [['Planet Number', 'CivLevel', 'Success%', 'Fuel', 'Rocks', '']]
for row in gb:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

gb0 = [['Planet 0', '4', '80%', '10', '10', '']]
if place == 0:
    gb0[0][5] = ("<-- You are here")
for row in gb0:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

gb1 = [['Planet 1', '4', '80%', '10', '10', '']]
for row in gb1:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

gb2 = [['Planet 2', '4', '80%', '10', '10', '']]
for row in gb2:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

gb3 = [['Planet 3', '4', '80%', '10', '10','']]
for row in gb3:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

gb4 = [['Planet 4', '4', '80%', '10', '10', '']]
for row in gb4:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

gb5 = [['Planet 5', '4', '80%', '10', '10', '']]
for row in gb5:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

gb6 = [['Planet 6', '4', '80%', '10', '10', '']]
for row in gb6:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

gb7 = [['Planet 7', '4', '80%', '10', '10', '']]
for row in gb7:
    print("{: >20} {: >20} {: >20} {: >20} {: >20} {: >20}".format(*row))

print("")

print ("")

答案 1 :(得分:0)

您期望在哪里做出改变?您发布的功能之外没有代码。函数中的所有变量都在函数内完全声明,初始化,打印和操作。他们都没有提到外面的任何东西。您唯一的通信是返回值&#34;&lt; - 您在这里&#34;,无论董事会名单中是否有任何数据。

另请注意,退出该功能后,将立即回收 gb 列表。也许你需要更像这样的东西:

def board_init(place):

    gb = [ 
           ['Planet Number', 'CivLevel', 'Success%', 'Fuel', 'Rocks', ''],
           ['Planet 1', '4', '80%', '10', '10', ' '],
           ['Planet 2', '4', '80%', '10', '10', ' '],
           ['Planet 3', '4', '80%', '10', '10', ' '],
           ['Planet 4', '4', '80%', '10', '10', ' '],
           ['Planet 5', '4', '80%', '10', '10', ' '],
           ['Planet 6', '4', '80%', '10', '10', ' '],
           ['Planet 7', '4', '80%', '10', '10', ' ']
         ]

    if place > 0 and place < len(gb):
        gb[place][-1] = "<-- You are here"

    print ("")
    for row in gb:
        print("{: >15} {: >15} {: >15} {: >15} {: >15} {: >15}".format(*row))

    return gb

# Main program
game_display = board_init(3)

输出:

Planet Number        CivLevel        Success%            Fuel           Rocks                
       Planet 1               4             80%              10              10                
       Planet 2               4             80%              10              10                
       Planet 3               4             80%              10              10 <-- You are here
       Planet 4               4             80%              10              10                
       Planet 5               4             80%              10              10                
       Planet 6               4             80%              10              10                
       Planet 7               4             80%              10              10