Lists in Python

前端 未结 7 2133
太阳男子
太阳男子 2021-01-07 05:20

Is the List in python homogeneous or heterogeneous?

相关标签:
7条回答
  • 2021-01-07 05:42

    The List in Python is heterogeneous - the same list can accept objects of various different types.

    There is a snippet here which gives you a homogeneous list in Python. No idea how that piece of code would perform however.

    0 讨论(0)
  • 2021-01-07 05:46

    Please try a simple

    a=[1, "a"]
    

    and see if it throws an error before asking a question.

    Btw, it does not.

    0 讨论(0)
  • 2021-01-07 05:50

    It depends if you talk about the direct elements of a list or its indirect elements.

    I believe that a list is in reality a list of references, not of objects, and that when the interpeter has to use a list, it knows that in fact it must manipulate the objects to which the references are pointing, not the references themselves: that's the "execution model" of Python, I think.

    • What I call indirect element of a list are the objects that constitute the value of the list.

    • What I call direct elements are the references to these objects that constitutes the implementation of the list.

    So:

    • considered as a list of references, a list is homogenous: all the elements are variables, in the plain sense of variable = chunk of memory whose value can change (while the value of an object never changes)

    • considered as a list of the objects pointed by the references, a list is heterogenous. The innuendo being that their TYPES are heterogenous.

    That's my understanding. I don't know if I am fully right.

    0 讨论(0)
  • 2021-01-07 05:51
    >>> def a(): pass
    >>> lst=[1,'one',{1:'one'},a,[1,1],(1,),True,set((1,))]
    >>> for each in lst:
    ...    print type(each), str(each)
    ... 
    <type 'int'> 1
    <type 'str'> one
    <type 'dict'> {1: 'one'}
    <type 'function'> <function a at 0x100496938>
    <type 'list'> [1, 1]
    <type 'tuple'> (1,)
    <type 'bool'> True
    <type 'set'> set([1])
    

    Any questions?

    0 讨论(0)
  • 2021-01-07 05:58

    Lists in Python can be heterogeneous, but by the general convention it is preferable that they only contain homogeneous elements. Python tuples are the natural data structure for heterogeneous sequences.

    Of course you can argue that both tuples and lists are just homogeneous sequences of Python objects, but that is a misleading oversimplification and doesn't add any value.

    Since tuples are immutable and lists are mutable you could also argue that mutability is the real distinction between them. However, that is not how they were intended. More on this can be found in this question.

    0 讨论(0)
  • 2021-01-07 05:59

    One more argument for homogeneity of lists: the PEP 484 and the new typing module. The items of tuple can be individually typed with Tuple:

    Tuple, used by listing the element types, for example Tuple[int, int, str]. The empty tuple can be typed as Tuple[()]. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example Tuple[int, ...]. (The ... here are part of the syntax, a literal ellipsis.) [reference]

    >>> import typing
    >>> typing.Tuple[str, int, float]
    typing.Tuple[str, int, float]
    

    However a list, via typing.List only accepts a single type parameter:

    List, used as List[element_type]

    >>> typing.List[str, int, float]
    Traceback (most recent call last):
      ...
    TypeError: Too many parameters for typing.List<~T>; actual 3, expected 1
    
    0 讨论(0)
提交回复
热议问题