Python论坛  - 讨论区

标题:[python-chinese] 问一个问题

2005年07月01日 星期五 11:06

kassarar kassarar at 126.com
Fri Jul 1 11:06:45 HKT 2005

我用的是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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月01日 星期五 12:06

Qiangning Hong hongqn at gmail.com
Fri Jul 1 12:06:51 HKT 2005

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),'  ( ( ! .-'

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号