美文网首页
python list 乘号 踩坑纪实

python list 乘号 踩坑纪实

作者: 铁佛爷 | 来源:发表于2018-11-08 10:01 被阅读0次
    # -*- coding: utf-8 -*-
    """
    Created on Thu Nov  8 09:55:02 2018
    
    @author: BigFly
    """
    
    num_list=[1000]*10
    list_list =[[]]*10
    set_list =[set()]*10
    
    
    print(num_list)
    print(list_list )
    print(set_list )
    
    print("对 0 号元素操作")
    num_list[0]+=6
    print(num_list)
    
    list_list[0].append(6)
    print(list_list )
    
    set_list[0].add(6)
    print(set_list )
    
    
    print("正确写法")
    list_list =[[] for i in range(10)]
    list_list[0].append(6)
    print(list_list )
    

    运行结果:

    [1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]
    [[], [], [], [], [], [], [], [], [], []]
    [set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
    对 0 号元素操作
    [1006, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]
    [[6], [6], [6], [6], [6], [6], [6], [6], [6], [6]]
    [{6}, {6}, {6}, {6}, {6}, {6}, {6}, {6}, {6}, {6}]
    正确写法
    [[6], [], [], [], [], [], [], [], [], []]
    

    相关文章

      网友评论

          本文标题:python list 乘号 踩坑纪实

          本文链接:https://www.haomeiwen.com/subject/knpixqtx.html