以单词格式打印python列表,就像是一个字符串

时间:2017-04-20 14:35:49

标签: python python-2.7

只需要打印:

0123456789

而不是这个,与列表大小无关:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

以下是代码示例:

c = []

for x in range(0, 10):
    c.append(x)

print c

2 个答案:

答案 0 :(得分:3)

您可以使用join将元素连接在一起,但它们必须是字符串。

如果您有整数,请先使用str函数和map

map

如果没有>>> ''.join(str(i) for i in range(10)) '0123456789' ,您还可以写:

public class Main {
    public static void main(String[] args) {
        Thread t = new Thread() {
            public void run() {
                while (true) {
                    if (Thread.interrupted()) {
                        break;
                    }
                }
                System.out.println("[thread] exiting...");
            }
        };

        System.out.println("[main] starting the thread");
        t.start();
        System.out.println("[main] interrupting the secondary thread");
        t.interrupt();

        try {
            t.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
        }
        System.out.println("[main] exiting");
    }
}

答案 1 :(得分:0)

试试这样:

c = []

for x in range(0, 10):
    c.append(str(x))

print ''.join(c)