Joining elements of a list

前端 未结 4 1986
旧巷少年郎
旧巷少年郎 2020-12-20 08:21

I have a list of tuples like:

data = [(\'a1\', \'a2\'), (\'b1\', \'b2\')]

And I want to generate a string like this: \"(\'a1\', \'a2\

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 09:09

    Use a generator to cast the tuples to strings and then use join().

    >>> ', '.join(str(d) for d in data)
    "('a1', 'a2'), ('b1', 'b2')"
    

提交回复
热议问题