2005年07月01日 星期五 11:06
我用的是python2.4.1 >>> TypeDict=dict(zip(AllTypes,[[]]*len(AllTypes))) >>> TypeDict {'IntType': [], 'TypeType': [], 'CodeType': [], 'BooleanType': [], 'UnboundMethodType': [], 'StringType': [], 'BuiltinMethodTy pe': [], 'FloatType': [], 'DictionaryType': [], 'NotImplementedType': [], 'BuiltinFunctionType': [], 'DictProxyType': [], 'Gen eratorType': [], 'InstanceType': [], 'ObjectType': [], 'DictType': [], 'FileType': [], 'EllipsisType': [], 'ListType': [], 'Me thodType': [], 'TupleType': [], 'ModuleType': [], 'FrameType': [], 'LongType': [], 'BufferType': [], 'TracebackType': [], 'Cla ssType': [], 'UnicodeType': [], 'SliceType': [], 'ComplexType': [], 'LambdaType': [], 'FunctionType': [], 'XRangeType': [], 'N oneType': []} #这里先定义一个词典,每一个value都是空的list #-------------------------------------------------------------- >>> TypeDict['TypeType']=TypeDict['TypeType'].__add__(['a','b']) >>> TypeDict {'IntType': [], 'TypeType': ['a', 'b'], 'CodeType': [], 'BooleanType': [], 'UnboundMethodType': [], 'StringType': [], 'Builtin MethodType': [], 'FloatType': [], 'DictionaryType': [], 'NotImplementedType': [], 'BuiltinFunctionType': [], 'DictProxyType': [], 'GeneratorType': [], 'InstanceType': [], 'ObjectType': [], 'DictType': [], 'FileType': [], 'EllipsisType': [], 'ListType': [], 'MethodType': [], 'TupleType': [], 'ModuleType': [], 'FrameType': [], 'LongType': [], 'BufferType': [], 'TracebackType': [], 'ClassType': [], 'UnicodeType': [], 'SliceType': [], 'ComplexType': [], 'LambdaType': [], 'FunctionType': [], 'XRangeType' : [], 'NoneType': []} #给TypeDict['TypeType']加一些元素,效果如预期 #-------------------------------------------------------------- >>> TypeDict['IntType'].append('k') >>> TypeDict {'IntType': ['k'], 'TypeType': ['a', 'b'], 'CodeType': ['k'], 'BooleanType': ['k'], 'UnboundMethodType': ['k'], 'StringType': ['k'], 'BuiltinMethodType': ['k'], 'FloatType': ['k'], 'DictionaryType': ['k'], 'NotImplementedType': ['k'], 'BuiltinFunctionT ype': ['k'], 'DictProxyType': ['k'], 'GeneratorType': ['k'], 'InstanceType': ['k'], 'ObjectType': ['k'], 'DictType': ['k'], 'F ileType': ['k'], 'EllipsisType': ['k'], 'ListType': ['k'], 'MethodType': ['k'], 'TupleType': ['k'], 'ModuleType': ['k'], 'Fram eType': ['k'], 'LongType': ['k'], 'BufferType': ['k'], 'TracebackType': ['k'], 'ClassType': ['k'], 'UnicodeType': ['k'], 'Slic eType': ['k'], 'ComplexType': ['k'], 'LambdaType': ['k'], 'FunctionType': ['k'], 'XRangeType': ['k'], 'NoneType': ['k']} #但这里就不明白了,怎么每一个都加了呢 #-------------------------------------------------------------- >>> TypeDict['TypeType'].append('l') >>> TypeDict {'IntType': ['k'], 'TypeType': ['a', 'b', 'l'], 'CodeType': ['k'], 'BooleanType': ['k'], 'UnboundMethodType': ['k'], 'StringTy pe': ['k'], 'BuiltinMethodType': ['k'], 'FloatType': ['k'], 'DictionaryType': ['k'], 'NotImplementedType': ['k'], 'BuiltinFunc tionType': ['k'], 'DictProxyType': ['k'], 'GeneratorType': ['k'], 'InstanceType': ['k'], 'ObjectType': ['k'], 'DictType': ['k' ], 'FileType': ['k'], 'EllipsisType': ['k'], 'ListType': ['k'], 'MethodType': ['k'], 'TupleType': ['k'], 'ModuleType': ['k'], 'FrameType': ['k'], 'LongType': ['k'], 'BufferType': ['k'], 'TracebackType': ['k'], 'ClassType': ['k'], 'UnicodeType': ['k'], 'SliceType': ['k'], 'ComplexType': ['k'], 'LambdaType': ['k'], 'FunctionType': ['k'], 'XRangeType': ['k'], 'NoneType': ['k']} #如果在已经非空的list后追加的话又没有问题 #-------------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050701/b9de905f/attachment.htm
2005年07月01日 星期五 12:06
kassarar wrote: > 我用的是python2.4.1 > >>>> TypeDict=dict(zip(AllTypes,[[]]*len(AllTypes))) 把这句改成 TypeDict = dict(zip(AllTypes, [[] for t in AllTypes])) 不然你这里创建的列表指向的都是同一个[] BTW, 我觉得写成 TypeDict = dict((t, []) for t in AllTypes) 更pythonic些 [...] > >>>> TypeDict['IntType'].append('k') >>>> TypeDict > {'IntType': ['k'], 'TypeType': ['a', 'b'], 'CodeType': ['k'], > 'BooleanType': ['k'], 'UnboundMethodType': ['k'], 'StringType': > ['k'], 'BuiltinMethodType': ['k'], 'FloatType': ['k'], 'DictionaryType': > ['k'], 'NotImplementedType': ['k'], 'BuiltinFunctionT > ype': ['k'], 'DictProxyType': ['k'], 'GeneratorType': ['k'], > 'InstanceType': ['k'], 'ObjectType': ['k'], 'DictType': ['k'], 'F > ileType': ['k'], 'EllipsisType': ['k'], 'ListType': ['k'], 'MethodType': > ['k'], 'TupleType': ['k'], 'ModuleType': ['k'], 'Fram > eType': ['k'], 'LongType': ['k'], 'BufferType': ['k'], 'TracebackType': > ['k'], 'ClassType': ['k'], 'UnicodeType': ['k'], 'Slic > eType': ['k'], 'ComplexType': ['k'], 'LambdaType': ['k'], > 'FunctionType': ['k'], 'XRangeType': ['k'], 'NoneType': ['k']} > > #但这里就不明白了,怎么每一个都加了呢 [...] -- Qiangning Hong ___________________________________ ( It's like deja vu all over again. ) ( ) ( -- Yogi Berra ) ----------------------------------- o o ("`-' '-/") .___..--' ' "`-._ ` *_ * ) `-. ( ) .`-.__. `) (_Y_.) ' ._ ) `._` ; `` -. .-' _.. `--'_..-_/ /--' _ .' ,4 ( i l ),-'' ( l i),' ( ( ! .-'
Zeuux © 2025
京ICP备05028076号