英文:
Printing elements of a list and handling nested data types in Python
问题
最后一个索引类型(即list
和tuple
)中的最后一个元素被打印两次的原因是因为你的异常处理代码块中的print
语句位于内部循环之外,导致它们在内部循环完成后执行。这就是为什么它们会在内部循环中的每次迭代后再次打印。要修复这个问题,你可以将异常处理代码块移到内部循环之内,以便在内部循环中正确处理最后一个元素。
以下是修改后的代码:
def PrintListElements(lst):
for i in range(len(lst)):
try:
for j in range(len(lst[i])):
print('list [', i, ']', '[', j, '] =', lst[i][j])
except TypeError:
print('list [', i, ']', '=', lst[i])
PrintListElements(l)
这将产生你所期望的输出。
另外,你提到的错误是由于尝试对不支持len()
操作的对象(如整数和集合)进行索引操作而引发的。通过在代码中添加异常处理块,你已经正确地处理了这些情况。如果你有任何其他问题或需要进一步的帮助,请告诉我。
英文:
I am trying to print each element in this list:
l=[7, ['one', 'two', 'three'], (1, 2), 6, ('a',), {'set1', 'set2'}, 5.4, [None]]
I want the output to show this:
list [ 0 ] = 7
list [ 1 ] [ 0 ] = one
list [ 1 ] [ 1 ] = two
list [ 1 ] [ 2 ] = three
list [ 2 ] [ 0 ] = 1
list [ 2 ] [ 1 ] = 2
list [ 3 ] = 6
list [ 4 ] [ 0 ] = a
list [ 5 ] = {'set1', 'set2'}
list [ 6 ] = 5.4
list [ 7 ] [ 0 ] = None
I tried this piece of code:
def PrintListElements(list):
for i in range(len(list)):
try:
for j in range(len(list[i])):
print('list', '[', i, ']', '[', j, ']', '=', list[i][j])
except TypeError:
print('list', '[', i, ']', '=', list[i])
else:
print('list', '[', i, ']', '[', j, ']', '=', list[i][j])
PrintListElements(l)
However, the output is this:
list [ 0 ] = 7
list [ 1 ] [ 0 ] = one
list [ 1 ] [ 1 ] = two
list [ 1 ] [ 2 ] = three
list [ 1 ] [ 2 ] = three
list [ 2 ] [ 0 ] = 1
list [ 2 ] [ 1 ] = 2
list [ 2 ] [ 1 ] = 2
list [ 3 ] = 6
list [ 4 ] [ 0 ] = a
list [ 4 ] [ 0 ] = a
list [ 5 ] = {'set1', 'set2'}
list [ 6 ] = 5.4
list [ 7 ] [ 0 ] = None
list [ 7 ] [ 0 ] = None
I want to understand why the last element in each indexable type (i.e., list
and tuple
) are getting printed twice.
Also, any help regarding how I can get my desired output would be appreciated.
Thank you.
NOTE: I was getting the following errors:
TypeError: object of type 'int' has no len()
TypeError: 'set' object is not subscriptable
Hence, I added the exception-handling block.
答案1
得分: 0
This should work for any amount of nesting. This one uses recursion.
l = [7, ['one', 'two', 'three'], (1, 2), 6, ('a',), {'set1', 'set2', (5, 2)}, 5.4, [None]]
FORMAT = " [ {} ]"
def print_everything(l, indices=[]):
for i, v in enumerate(l):
if isinstance(v, list) or isinstance(v, tuple) or isinstance(v, set):
new_index = [*indices]
new_index.append(i)
print_everything(v, new_index)
else:
print_string = 'list'
for j in indices:
print_string += FORMAT.format(j)
print_string += FORMAT.format(i)
print_string += f' = {v}'
print(print_string)
print_everything(l)
Output:
list [ 0 ] = 7
list [ 1 ] [ 0 ] = one
list [ 1 ] [ 1 ] = two
list [ 1 ] [ 2 ] = three
list [ 2 ] [ 0 ] = 1
list [ 2 ] [ 1 ] = 2
list [ 3 ] = 6
list [ 4 ] [ 0 ] = a
list [ 5 ] [ 0 ] = set1
list [ 5 ] [ 1 ] = set2
list [ 5 ] [ 2 ] [ 0 ] = 5
list [ 5 ] [ 2 ] [ 1 ] = 2
list [ 6 ] = 5.4
list [ 7 ] [ 0 ] = None
If you input:
l2 = [5, ['one', 'two', ['one2', 'two2', 'three3'], 'four'], (1, 2, 3, (4, 5, 6, (7, 8))), 5.23, [None, 'something']]
then you get:
list [ 0 ] = 5
list [ 1 ] [ 0 ] = one
list [ 1 ] [ 1 ] = two
list [ 1 ] [ 2 ] [ 0 ] = one2
list [ 1 ] [ 2 ] [ 1 ] = two2
list [ 1 ] [ 2 ] [ 2 ] = three3
list [ 1 ] [ 3 ] = four
list [ 2 ] [ 0 ] = 1
list [ 2 ] [ 1 ] = 2
list [ 2 ] [ 2 ] = 3
list [ 2 ] [ 3 ] [ 0 ] = 4
list [ 2 ] [ 3 ] [ 1 ] = 5
list [ 2 ] [ 3 ] [ 2 ] = 6
list [ 2 ] [ 3 ] [ 3 ] [ 0 ] = 7
list [ 2 ] [ 3 ] [ 3 ] [ 1 ] = 8
list [ 3 ] = 5.23
list [ 4 ] [ 0 ] = None
list [ 4 ] [ 1 ] = something
EDIT: I didn't notice that you wanted to print the set as it is. For that, you can remove the check isinstance(v, set)
from my code.
英文:
This should work for any amount of nesting. This one uses recursion.
l=[7, ['one', 'two', 'three'], (1, 2), 6, ('a',), {'set1', 'set2', (5,2)}, 5.4, [None]]
FORMAT = " [ {} ]"
def print_everything(l, indices=[]):
for i,v in enumerate(l):
if isinstance(v, list) or isinstance(v, tuple) or isinstance(v, set):
new_index=[*indices]
new_index.append(i)
print_everything(v, new_index)
else:
print_string = 'list'
for j in indices:
print_string += FORMAT.format(j)
print_string += FORMAT.format(i)
print_string += f' = {v}'
print(print_string)
print_everything(l)
Output:
list [ 0 ] = 7
list [ 1 ] [ 0 ] = one
list [ 1 ] [ 1 ] = two
list [ 1 ] [ 2 ] = three
list [ 2 ] [ 0 ] = 1
list [ 2 ] [ 1 ] = 2
list [ 3 ] = 6
list [ 4 ] [ 0 ] = a
list [ 5 ] [ 0 ] = set1
list [ 5 ] [ 1 ] = set2
list [ 5 ] [ 2 ] [ 0 ] = 5
list [ 5 ] [ 2 ] [ 1 ] = 2
list [ 6 ] = 5.4
list [ 7 ] [ 0 ] = None
If you input:
l2 = [5, ['one', 'two', ['one2', 'two2', 'three3'], 'four'], (1,2,3,(4,5,6, (7,8))), 5.23, [None, 'something']]
then you get:
list [ 0 ] = 5
list [ 1 ] [ 0 ] = one
list [ 1 ] [ 1 ] = two
list [ 1 ] [ 2 ] [ 0 ] = one2
list [ 1 ] [ 2 ] [ 1 ] = two2
list [ 1 ] [ 2 ] [ 2 ] = three3
list [ 1 ] [ 3 ] = four
list [ 2 ] [ 0 ] = 1
list [ 2 ] [ 1 ] = 2
list [ 2 ] [ 2 ] = 3
list [ 2 ] [ 3 ] [ 0 ] = 4
list [ 2 ] [ 3 ] [ 1 ] = 5
list [ 2 ] [ 3 ] [ 2 ] = 6
list [ 2 ] [ 3 ] [ 3 ] [ 0 ] = 7
list [ 2 ] [ 3 ] [ 3 ] [ 1 ] = 8
list [ 3 ] = 5.23
list [ 4 ] [ 0 ] = None
list [ 4 ] [ 1 ] = something
EDIT: I didn't notice that you wanted to print the set as it is. For that you can remove the check isinstance(v, set)
from my code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论