打印列表元素和处理Python中的嵌套数据类型

huangapple go评论99阅读模式
英文:

Printing elements of a list and handling nested data types in Python

问题

最后一个索引类型(即listtuple)中的最后一个元素被打印两次的原因是因为你的异常处理代码块中的print语句位于内部循环之外,导致它们在内部循环完成后执行。这就是为什么它们会在内部循环中的每次迭代后再次打印。要修复这个问题,你可以将异常处理代码块移到内部循环之内,以便在内部循环中正确处理最后一个元素。

以下是修改后的代码:

  1. def PrintListElements(lst):
  2. for i in range(len(lst)):
  3. try:
  4. for j in range(len(lst[i])):
  5. print('list [', i, ']', '[', j, '] =', lst[i][j])
  6. except TypeError:
  7. print('list [', i, ']', '=', lst[i])
  8. PrintListElements(l)

这将产生你所期望的输出。

另外,你提到的错误是由于尝试对不支持len()操作的对象(如整数和集合)进行索引操作而引发的。通过在代码中添加异常处理块,你已经正确地处理了这些情况。如果你有任何其他问题或需要进一步的帮助,请告诉我。

英文:

I am trying to print each element in this list:

  1. l=[7, ['one', 'two', 'three'], (1, 2), 6, ('a',), {'set1', 'set2'}, 5.4, [None]]

I want the output to show this:

  1. list [ 0 ] = 7
  2. list [ 1 ] [ 0 ] = one
  3. list [ 1 ] [ 1 ] = two
  4. list [ 1 ] [ 2 ] = three
  5. list [ 2 ] [ 0 ] = 1
  6. list [ 2 ] [ 1 ] = 2
  7. list [ 3 ] = 6
  8. list [ 4 ] [ 0 ] = a
  9. list [ 5 ] = {'set1', 'set2'}
  10. list [ 6 ] = 5.4
  11. list [ 7 ] [ 0 ] = None

I tried this piece of code:

  1. def PrintListElements(list):
  2. for i in range(len(list)):
  3. try:
  4. for j in range(len(list[i])):
  5. print('list', '[', i, ']', '[', j, ']', '=', list[i][j])
  6. except TypeError:
  7. print('list', '[', i, ']', '=', list[i])
  8. else:
  9. print('list', '[', i, ']', '[', j, ']', '=', list[i][j])
  10. PrintListElements(l)

However, the output is this:

  1. list [ 0 ] = 7
  2. list [ 1 ] [ 0 ] = one
  3. list [ 1 ] [ 1 ] = two
  4. list [ 1 ] [ 2 ] = three
  5. list [ 1 ] [ 2 ] = three
  6. list [ 2 ] [ 0 ] = 1
  7. list [ 2 ] [ 1 ] = 2
  8. list [ 2 ] [ 1 ] = 2
  9. list [ 3 ] = 6
  10. list [ 4 ] [ 0 ] = a
  11. list [ 4 ] [ 0 ] = a
  12. list [ 5 ] = {'set1', 'set2'}
  13. list [ 6 ] = 5.4
  14. list [ 7 ] [ 0 ] = None
  15. 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:

  1. TypeError: object of type 'int' has no len()
  2. 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.

  1. l = [7, ['one', 'two', 'three'], (1, 2), 6, ('a',), {'set1', 'set2', (5, 2)}, 5.4, [None]]
  2. FORMAT = " [ {} ]"
  3. def print_everything(l, indices=[]):
  4. for i, v in enumerate(l):
  5. if isinstance(v, list) or isinstance(v, tuple) or isinstance(v, set):
  6. new_index = [*indices]
  7. new_index.append(i)
  8. print_everything(v, new_index)
  9. else:
  10. print_string = 'list'
  11. for j in indices:
  12. print_string += FORMAT.format(j)
  13. print_string += FORMAT.format(i)
  14. print_string += f' = {v}'
  15. print(print_string)
  16. print_everything(l)

Output:

  1. list [ 0 ] = 7
  2. list [ 1 ] [ 0 ] = one
  3. list [ 1 ] [ 1 ] = two
  4. list [ 1 ] [ 2 ] = three
  5. list [ 2 ] [ 0 ] = 1
  6. list [ 2 ] [ 1 ] = 2
  7. list [ 3 ] = 6
  8. list [ 4 ] [ 0 ] = a
  9. list [ 5 ] [ 0 ] = set1
  10. list [ 5 ] [ 1 ] = set2
  11. list [ 5 ] [ 2 ] [ 0 ] = 5
  12. list [ 5 ] [ 2 ] [ 1 ] = 2
  13. list [ 6 ] = 5.4
  14. list [ 7 ] [ 0 ] = None

If you input:

  1. l2 = [5, ['one', 'two', ['one2', 'two2', 'three3'], 'four'], (1, 2, 3, (4, 5, 6, (7, 8))), 5.23, [None, 'something']]

then you get:

  1. list [ 0 ] = 5
  2. list [ 1 ] [ 0 ] = one
  3. list [ 1 ] [ 1 ] = two
  4. list [ 1 ] [ 2 ] [ 0 ] = one2
  5. list [ 1 ] [ 2 ] [ 1 ] = two2
  6. list [ 1 ] [ 2 ] [ 2 ] = three3
  7. list [ 1 ] [ 3 ] = four
  8. list [ 2 ] [ 0 ] = 1
  9. list [ 2 ] [ 1 ] = 2
  10. list [ 2 ] [ 2 ] = 3
  11. list [ 2 ] [ 3 ] [ 0 ] = 4
  12. list [ 2 ] [ 3 ] [ 1 ] = 5
  13. list [ 2 ] [ 3 ] [ 2 ] = 6
  14. list [ 2 ] [ 3 ] [ 3 ] [ 0 ] = 7
  15. list [ 2 ] [ 3 ] [ 3 ] [ 1 ] = 8
  16. list [ 3 ] = 5.23
  17. list [ 4 ] [ 0 ] = None
  18. 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.

  1. l=[7, ['one', 'two', 'three'], (1, 2), 6, ('a',), {'set1', 'set2', (5,2)}, 5.4, [None]]
  2. FORMAT = " [ {} ]"
  3. def print_everything(l, indices=[]):
  4. for i,v in enumerate(l):
  5. if isinstance(v, list) or isinstance(v, tuple) or isinstance(v, set):
  6. new_index=[*indices]
  7. new_index.append(i)
  8. print_everything(v, new_index)
  9. else:
  10. print_string = 'list'
  11. for j in indices:
  12. print_string += FORMAT.format(j)
  13. print_string += FORMAT.format(i)
  14. print_string += f' = {v}'
  15. print(print_string)
  16. print_everything(l)

Output:

  1. list [ 0 ] = 7
  2. list [ 1 ] [ 0 ] = one
  3. list [ 1 ] [ 1 ] = two
  4. list [ 1 ] [ 2 ] = three
  5. list [ 2 ] [ 0 ] = 1
  6. list [ 2 ] [ 1 ] = 2
  7. list [ 3 ] = 6
  8. list [ 4 ] [ 0 ] = a
  9. list [ 5 ] [ 0 ] = set1
  10. list [ 5 ] [ 1 ] = set2
  11. list [ 5 ] [ 2 ] [ 0 ] = 5
  12. list [ 5 ] [ 2 ] [ 1 ] = 2
  13. list [ 6 ] = 5.4
  14. list [ 7 ] [ 0 ] = None

If you input:

  1. l2 = [5, ['one', 'two', ['one2', 'two2', 'three3'], 'four'], (1,2,3,(4,5,6, (7,8))), 5.23, [None, 'something']]

then you get:

  1. list [ 0 ] = 5
  2. list [ 1 ] [ 0 ] = one
  3. list [ 1 ] [ 1 ] = two
  4. list [ 1 ] [ 2 ] [ 0 ] = one2
  5. list [ 1 ] [ 2 ] [ 1 ] = two2
  6. list [ 1 ] [ 2 ] [ 2 ] = three3
  7. list [ 1 ] [ 3 ] = four
  8. list [ 2 ] [ 0 ] = 1
  9. list [ 2 ] [ 1 ] = 2
  10. list [ 2 ] [ 2 ] = 3
  11. list [ 2 ] [ 3 ] [ 0 ] = 4
  12. list [ 2 ] [ 3 ] [ 1 ] = 5
  13. list [ 2 ] [ 3 ] [ 2 ] = 6
  14. list [ 2 ] [ 3 ] [ 3 ] [ 0 ] = 7
  15. list [ 2 ] [ 3 ] [ 3 ] [ 1 ] = 8
  16. list [ 3 ] = 5.23
  17. list [ 4 ] [ 0 ] = None
  18. 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.

huangapple
  • 本文由 发表于 2023年5月25日 21:32:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定