打印二维列表 [英] print two dimensional list

查看:70
本文介绍了打印二维列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中有另一个列表,我想doc.write(a)

a = [[1, 2, "你好"],[3, 5, 你好"],[5,7,我不知道"]]doc.write(''.join(a))类型错误:序列项 0:预期的 str 实例,找到列表

我该如何处理,我是否必须创建一个 for 循环来加入并添加所有子列表?

真正的目标是让它以某种方式对人类可读,但我不想从你那里得到一个完整的解决方案.

解决方案

你可以做不同的合法事情,任何人在不知道你想要哪一个的情况下都无法说出哪一个是对的.

<小时>

首先,你可以只写astrrepr:

<预><代码>>>>a=[[1, 2, "你好"],[3, 5, "你好"],[5,7,"我不知道"]]>>>代表(一)'[[1, 2, \'hello\'], [3, 5, \'hi There\'], [5, 7, "我不知道"]]'

请注意,这就是 print 所做的(它打印您提供的任何内容的 str——尽管使用列表,str 是与 repr 相同;它们都是有效的 '[' + ', '.join(map(repr, self)) + ']').

<小时>

其次,您可以使用专为数据持久化设计的格式,例如 JSON:

<预><代码>>>>json.dumps(a)'[[1, 2, "你好"], [3, 5, "hi There"], [5, 7, "我不知道"]]'

<小时>

第三,你可以用你选择的某种方式将a的每个元素的repr连接在一起,这对于一个map或一个理解来说是微不足道的.例如:

<预><代码>>>>'[' + ', '.join(map(repr, a)) + ']''[[1, 2, \'hello\'], [3, 5, \'hi There\'], [5, 7, "我不知道"]]'

……或……

<预><代码>>>>'我的东西包括:' + ','.join(map(repr, a)) + '\n''我的东西包括:[1, 2, \'hello\'],[3, 5, \'hi There\'],[5, 7, "我不知道"]\n'

<小时>

或者你可以递归地做同样的事情.

或者您可以将列表展平(例如,使用 itertools.chain 将其展平一步,或者使用 itertools 文档中的配方或使用 more-itertools 包),然后根据需要将这些部分串起来,然后将它们连接起来.

或者你可以只写LIST这个词.

所有这些都是传递给 write 的完全有效的东西.

I have a list, in which is another list and I want to doc.write(a)

a = [[1, 2, "hello"],
     [3, 5, "hi There"],
     [5,7,"I don't know"]]
doc.write(''.join(a))



TypeError: sequence item 0: expected str instance, list found

How can I handle this, do I have to make a for-loop in which I join and add all the sublists?

The real goal was to make it somehow readable for human beeing, but I didn't wanted a finished solution from you.

解决方案

There are different legal things you can do, and no way for anyone to say which one is right without knowing which one you want.


First, you can just write the str or repr of a:

>>> a=[[1, 2, "hello"],[3, 5, "hi There"],[5,7,"I don't know"]]
>>> repr(a)
'[[1, 2, \'hello\'], [3, 5, \'hi There\'], [5, 7, "I don\'t know"]]'

Note that this is what print does (it prints the str of whatever you give it—although with a list, the str is identical to the repr; they're both effectively '[' + ', '.join(map(repr, self)) + ']').


Second, you could use a format that's designed for data persistent, like JSON:

>>> json.dumps(a)
'[[1, 2, "hello"], [3, 5, "hi There"], [5, 7, "I don\'t know"]]'


Third, you can join together the repr of each element of a in some way of your choosing, which is trivial with a map or a comprehension. For example:

>>> '[' + ', '.join(map(repr, a)) + ']'
'[[1, 2, \'hello\'], [3, 5, \'hi There\'], [5, 7, "I don\'t know"]]'

… or …

>>> 'My stuff includes: ' + ','.join(map(repr, a)) + '\n'
'My stuff includes: [1, 2, \'hello\'],[3, 5, \'hi There\'],[5, 7, "I don\'t know"]\n'


Or you can do the same thing recursively.

Or you can flatten the list (e.g., flatten it one step with itertools.chain, or recursively with the recipes from the itertools docs or with the more-itertools package) and then stringify the pieces however you want and then join them up.

Or you can just write the word LIST.

All of those are perfectly valid things to pass to write.

这篇关于打印二维列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆