How to print a list of tuples with no brackets in Python

前端 未结 4 1365
[愿得一人]
[愿得一人] 2020-12-14 02:41

I\'m looking for a way to print elements from a tuple with no brackets.

Here is my tuple:

mytuple = [(1.0,),(25.34,),(2.4,),(7.4,)]
<
4条回答
  •  醉酒成梦
    2020-12-14 03:34

    mytuple is already a list (a list of tuples), so calling list() on it does nothing.

    (1.0,) is a tuple with one item. You can't call string functions on it (like you tried). They're for string types.

    To print each item in your list of tuples, just do:

    for item in mytuple:
        print str(item[0]) + ','
    

    Or:

    print ', ,'.join([str(i[0]) for i in mytuple])
    # 1.0, ,25.34, ,2.4, ,7.4
    

提交回复
热议问题