如何并排打印两个不同大小的列表?
How to print two lists of different size side by side?
我有两个不同长度的列表,我想并排打印,用制表符分隔。例如:
a_list = ["a","b","c","d","e"]
b_list = ["f","g","h","i","j","k","l","m","n"]
我试过了:
print('A-list:'+ '\t' + 'B-list:')
for i in range(len(b_list)):
print(a_list[i] + '\t' + b_list[i])
我当然得到 "out of range-trace" 回复,因为一个列表更短。我不希望使用 zip。
因为 b_list
的长度大于 a_list
的长度,所以你的代码变大了 IndexError
,为此,你可以使用 try except
语句:
for i in range(max(len(b_list), len(a_list))):
try:
print(f"{a_list[i]}\t", end="")
except IndexError:
print(f" \t", end="")
try:
print(f"{b_list[i]}\t")
except IndexError:
print(f" \t")
输出:
a f
b g
c h
d i
e j
k
l
m
n
这是一个可能的解决方案。 a_list
比 b_list
长还是短都没有关系。
def get(lst, idx):
try:
return lst[idx]
except:
return " "
a_list = ['a','b','c','d','e']
b_list = ['f','g','h','i','j','k','l','m','n']
result = []
for i in range(min(len(a_list), len(b_list))):
result.append(get(a_list, i))
result.append(get(b_list, i))
for i in range(min(len(a_list), len(b_list)), max(len(a_list), len(b_list))):
result.append(get(a_list, i))
result.append(get(b_list, i))
print('\n'.join('\t'.join((result[i], result[i+1]))
for i in range(0, len(result), 2)))
这会打印出预期的输出:
a f
b g
c h
d i
e j
k
l
m
n
没有任何额外的循环:
list_a = ["A", "B", "C"]
list_b = ["1", "2", "3", "4"]
len_a = len(list_a)
len_b = len(list_b)
i = 0
while True:
if i < len_a:
print(list_a[i], end="")
if i < len_b:
print("\t", list_b[i], end="")
print()
i += 1
if i > max(len_a, len_a):
break
def iterate(a_list, b_list):
# iterate through short list
min_length = min(((len(a_list), len(b_list))))
for i in range(min_length):
print('{}\t{}'.format(a_list[i], b_list[i]))
# print remaining items accordingly
if i + 1 == len(a_list):
for item in b_list[i+1:]:
print('\t{}'.format(item))
else:
for item in a_list[i+1:]:
print(item)
我建议使用字符串方法格式,它允许您在列表中包含任何类型的元素。
在所有解决方案中,诀窍是遍历最长的列表并使用空 space 来弥补较短列表中缺失的条目。
例如:
a_list = ['a','b','c','d','e']
b_list = ['f','g','h','i','j','k','l','m','n']
for i in range(max(len(a_list),len(b_list))):
a = a_list[i:i+1] or [""]
b = b_list[i:i+1] or [""]
print(a[0]+"\t"+b[0])
如果您不关心内存使用情况,您也可以用空白填充列表,以便有一个假设相同大小的列表的更简单的打印过程:
padding = len(b_list)-len(a_list)
a_padded = a_list + [""]*padding
b_padded = b_list + [""]*-padding
for i in range(len(a_padded)):
print(a_padded[i]+"\t"+b_padded[i])
你可以试试这个:
test1 = [1, 2, 3, 4]
test2 = [1, 2, 3, 4, 5]
test3 = [1, 2]
while test or test2 or test3:
print(test.pop() if test else '',\
test2.pop() if test2 else '',\
test3.pop()if test3 else '')
我有两个不同长度的列表,我想并排打印,用制表符分隔。例如:
a_list = ["a","b","c","d","e"]
b_list = ["f","g","h","i","j","k","l","m","n"]
我试过了:
print('A-list:'+ '\t' + 'B-list:')
for i in range(len(b_list)):
print(a_list[i] + '\t' + b_list[i])
我当然得到 "out of range-trace" 回复,因为一个列表更短。我不希望使用 zip。
因为 b_list
的长度大于 a_list
的长度,所以你的代码变大了 IndexError
,为此,你可以使用 try except
语句:
for i in range(max(len(b_list), len(a_list))):
try:
print(f"{a_list[i]}\t", end="")
except IndexError:
print(f" \t", end="")
try:
print(f"{b_list[i]}\t")
except IndexError:
print(f" \t")
输出:
a f
b g
c h
d i
e j
k
l
m
n
这是一个可能的解决方案。 a_list
比 b_list
长还是短都没有关系。
def get(lst, idx):
try:
return lst[idx]
except:
return " "
a_list = ['a','b','c','d','e']
b_list = ['f','g','h','i','j','k','l','m','n']
result = []
for i in range(min(len(a_list), len(b_list))):
result.append(get(a_list, i))
result.append(get(b_list, i))
for i in range(min(len(a_list), len(b_list)), max(len(a_list), len(b_list))):
result.append(get(a_list, i))
result.append(get(b_list, i))
print('\n'.join('\t'.join((result[i], result[i+1]))
for i in range(0, len(result), 2)))
这会打印出预期的输出:
a f
b g
c h
d i
e j
k
l
m
n
没有任何额外的循环:
list_a = ["A", "B", "C"]
list_b = ["1", "2", "3", "4"]
len_a = len(list_a)
len_b = len(list_b)
i = 0
while True:
if i < len_a:
print(list_a[i], end="")
if i < len_b:
print("\t", list_b[i], end="")
print()
i += 1
if i > max(len_a, len_a):
break
def iterate(a_list, b_list):
# iterate through short list
min_length = min(((len(a_list), len(b_list))))
for i in range(min_length):
print('{}\t{}'.format(a_list[i], b_list[i]))
# print remaining items accordingly
if i + 1 == len(a_list):
for item in b_list[i+1:]:
print('\t{}'.format(item))
else:
for item in a_list[i+1:]:
print(item)
我建议使用字符串方法格式,它允许您在列表中包含任何类型的元素。
在所有解决方案中,诀窍是遍历最长的列表并使用空 space 来弥补较短列表中缺失的条目。
例如:
a_list = ['a','b','c','d','e']
b_list = ['f','g','h','i','j','k','l','m','n']
for i in range(max(len(a_list),len(b_list))):
a = a_list[i:i+1] or [""]
b = b_list[i:i+1] or [""]
print(a[0]+"\t"+b[0])
如果您不关心内存使用情况,您也可以用空白填充列表,以便有一个假设相同大小的列表的更简单的打印过程:
padding = len(b_list)-len(a_list)
a_padded = a_list + [""]*padding
b_padded = b_list + [""]*-padding
for i in range(len(a_padded)):
print(a_padded[i]+"\t"+b_padded[i])
你可以试试这个:
test1 = [1, 2, 3, 4]
test2 = [1, 2, 3, 4, 5]
test3 = [1, 2]
while test or test2 or test3:
print(test.pop() if test else '',\
test2.pop() if test2 else '',\
test3.pop()if test3 else '')