目录
Print a list of lists on separate lines in Python
在 Python 中的不同行上打印列表列表
要在单独的行上打印列表列表:
- 使用
for
循环遍历列表。 - 使用可迭代的解包操作符来解包每个子列表的项目。
- 使用
print()
函数打印结果。
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] for sublist in list_of_lists: # 1 alice # 2 bobbyhadz # 3 carl print(*sublist)
*可迭代解包运算符
使我们能够在函数调用、推导式和生成器表达式中解包可迭代对象。
如果您只需要打印每个子列表的特定项目,请在索引处访问它们。
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] for sublist in list_of_lists: # alice # bobbyhadz # carl print(sublist[1])
通过在 Python 中格式化每一行来打印列表列表
如果需要以特定方式格式化每一行,则可以使用格式化字符串文字。
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] for sublist in list_of_lists: # id: 1 | name: alice # id: 2 | name: bobbyhadz # id: 3 | name: carl print(f'id: {sublist[0]} | name: {sublist[1]}')
f
.var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # 👉️ bobbyhadz
确保将表达式括在大括号 –{expression}
中。
在 Python 中以表格格式打印列表列表
如果您需要以
表格格式打印列表列表,您可以使用相同的方法。
headers = [ 'ID', 'Name', 'Country' ] list_of_lists = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in list_of_lists: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')
Formatted string literals also enable us to use the
format-specific mini-language
in expression blocks.
my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
The space between the colon and the less-than sign is the fill character.
The less-than or greater-than sign is the alignment.
We first format and print the headers and then iterate over the list and print
each row.
Print a List of Lists by joining the elements of each sublist with a delimiter #
You can use the str.join()
method if you need to join the elements of each
sublist with a delimiter.
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] # 1, alice # 2, bobbyhadz # 3, carl for sublist in list_of_lists: result = ', '.join(str(item) for item in sublist) print(result)
The str.join method
takes an iterable as an argument and returns a string which is the concatenation
of the strings in the iterable.
TypeError
if there are any non-string values in the iterable.If your iterable contains numbers or other types, convert all of the values to
strings before calling join()
.
The string the method is called on is used as the separator between the
elements.
We used a comma and a space (,
) in the example, but you can use any other
delimiter.
If you need to join the list of lists into a string that contains newline (\n
)
characters, use two calls to the str.join()
method.
list_of_lists = [ [1, 'alice'], [2, 'bobbyhadz'], [3, 'carl'], ] result = '\n'.join(' '.join(str(item) for item in sublist) for sublist in list_of_lists) # 1 alice # 2 bobbyhadz # 3 carl print(result)
join()
method joins the elements of the sublist
of the current iteration.We used a space separator when joining the elements of each sublist, but you can
use any other delimiter.
在调用之前,我们使用str()
该类将每个值转换为字符串
join()
。
最后一步是使用该join()
方法将列表中的子列表连接成一个字符串,以换行符 ( \n
) 字符分隔符。
由于换行符,每个嵌套列表的项目都打印在单独的行上。