Joining a list of python objects with __str__ method

前端 未结 5 702
孤城傲影
孤城傲影 2021-01-02 09:44

I\'ve already looked at this question on representing strings in Python but my question is slightly different.

Here\'s the code:

>>> class W         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 09:58

    You technically aren't joining the list of python objects, just their string representation.

    >>> reduce(lambda x,y: "%s\n%s" % (x,y), weird_list)
    '1302226564.83\n1302226564.83\n1302226564.83'
    >>> 
    

    This works as well but doesn't look any nicer:

    >>> a = ""
    >>> for x in weird_list:
    ...     a+="%s\n" % x
    ... 
    >>> print a
    1302226564.83
    1302226564.83
    1302226564.83
    
    >>>
    

提交回复
热议问题