Zipping together unicode strings in Python

后端 未结 4 1744
感动是毒
感动是毒 2021-01-24 18:18

I have the string:

a = \"ÀÁÂÃÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜ\" b = \"àáâãäèéçêëìíîïòóôõöùúûüÿ\"

and I want to create the string

\"ÀàÁáÂâ...\"         


        
4条回答
  •  走了就别回头了
    2021-01-24 18:55

    Maybe not beautiful, but working one.

    >>> a_longer = len(a) > len(b)
    >>> new_string = ""
    >>> for i in range((min(len(a), len(b)))):
    ...     new_string += a[i] + b[i]
    ... 
    >>> if a_longer:
    ...     new_string += a[i:]
    ... else:
    ...     new_string += b[i:]
    ... 
    >>> print new_string
    ÀàÁáÂâÃãÈäÉèÊéËçÌêÍëÎìÏíÒîÓïÔòÕóÖôÙõÚöÛùÜúúûüÿ
    

    Or, with using zip:

    >>> a = u'ÀÁÂÃÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜ'
    >>> b = u'àáâãäèéçêëìíîïòóôõöùúûüÿ'
    >>> c = zip(a, b)
    >>> new_string = "".join([a + b for a, b in c])
    >>> print new_string
    ÀàÁáÂâÃãÈäÉèÊéËçÌêÍëÎìÏíÒîÓïÔòÕóÖôÙõÚöÛùÜú
    

    But watch out, that a zip method will not give you the rest of the 'b' string as it does not have a pair in 'a' string.

提交回复
热议问题