Pythonic conversion to singleton iterable if not already an iterable

前端 未结 1 975
死守一世寂寞
死守一世寂寞 2021-01-18 17:00

Suppose I have

def distance2(vector1, vector2):
    zipped = zip(vector1, vector2)
    difference2 = [(vector2 - vector1) ** 2 for (vector1, vector2) in zipp         


        
相关标签:
1条回答
  • 2021-01-18 17:56

    You are describing the basic usage of always_iterable.

    >>> from more_itertools.more import always_iterable
    >>> for val in always_iterable(1):
    ...     print(val)
    ...     
    1
    

    This helper is thoroughly battle-tested in OSS.

    0 讨论(0)
提交回复
热议问题