Cam*_*son 5 python multidimensional-array python-3.x
我有一个数字列表:testList = [1,[1],[12],2,3]
我希望它成为:flatList = [1,1,12,2,3]
使用如下所示的典型列表理解无效。
flatList = [val for sublist in testList for val in sublist]
TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud)
我怀疑这是因为未嵌套的项目被视为可迭代的子列表,因此我尝试了以下操作:
flatList = [val if isinstance(sublist, int) == False else val for sublist in testlist for val in sublist]
Run Code Online (Sandbox Code Playgroud)
但是我不清楚语法,或者是否有更好的方法可以做到这一点。尝试从else子句中删除val意味着val未定义。照原样,它仍然给我相同的TypeError。
下面的代码确实对我有用,但是我很想看看它是否可以以列表理解方式完成,以及人们对此的看法。
for sublist in testlist:
if type(sublist) == int:
flat.append(sublist)
else:
for val in sublist:
flat.append(val)
print(flat)
>>>[1, 1, 12, 2, 3]
Run Code Online (Sandbox Code Playgroud)
由于您使用的是 Python 3,因此您可以利用yield from递归函数。它已在 Python 3.3 中引入。
作为奖励,您可以展平任意嵌套列表、元组、集合或范围:
test_list = [1, [1], [12, 'test', set([3, 4, 5])], 2, 3, ('hello', 'world'), [range(3)]]
def flatten(something):
if isinstance(something, (list, tuple, set, range)):
for sub in something:
yield from flatten(sub)
else:
yield something
print(list(flatten(test_list)))
# [1, 1, 12, 'test', 3, 4, 5, 2, 3, 'hello', 'world', 0, 1, 2]
print(list(flatten('Not a list')))
# ['Not a list']
print(list(flatten(range(10))))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
这是另一个带有调试行的示例:
def flatten(something, level=0):
print("%sCalling flatten with %r" % (' ' * level, something))
if isinstance(something, (list, tuple, set, range)):
for sub in something:
yield from flatten(sub, level+1)
else:
yield something
list(flatten([1, [2, 3], 4]))
#Calling flatten with [1, [2, 3], 4]
# Calling flatten with 1
# Calling flatten with [2, 3]
# Calling flatten with 2
# Calling flatten with 3
# Calling flatten with 4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
849 次 |
| 最近记录: |