如何获取嵌套字符串列表中最长字符串的长度?
How to get the length of the longest string in a nested list of strings?
这里是 Python 的新手。我试图在一系列嵌套列表中找到值的最长长度。这是一个示例列表类型:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
我在这里想要的答案是 8
,但如果列表更新,这可能会改变。
当我使用 print(len(tableData))
时,我得到 3,嵌套列表的数量。我也无法找到解决此问题的循环。
抱歉,这是一个非常简单的问题,但我不知所措。
在此先感谢您的帮助。
如您所见,len(tableData)
给出了 tableData
的元素数。你想要的是 tableData
的元素 的元素 的长度的最大值:
l = max(len(x) for sublist in tableData for x in sublist)
>>> print(l)
8
也许这对你有用:
new_list = []
for sub_list in tableData:
for item in sub_list:
new_list.append(item)
max_element = max(new_list, key=len)
print(max_element) # this actually prints the item
print(len(max_element)) # this will give you the length
遍历每个元素并获取其 len()
进行比较。
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
maxCount = 0
for lst in tableData:
for elem in lst:
maxCount = max(maxCount, len(elem))
print(maxCount)
输出:
8
from itertools import chain
chain.from_iterable(tableData)
这就像您有一长串单个值,而不是一列值列表。现在在扁平列表中找到最长的项目是微不足道的:
max(chain.from_iterable(tableData), key=len)
这个returns'cherries'
.
max(map(len, chain.from_iterable(tableData)))
这个returns8
.
你可以试试循环...
l = 0
for row in tableData:
for col in row:
l = len(col) if l < len(col) else l
maxLength = 0
for row in tableData:
maxRowElementLength = len(max(row, key=len))
if maxLength < maxRowElementLength:
maxLength = maxRowElementLength
print(maxLength)
>>> import numpy as np
>>> data=np.array([['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]).reshape(-1)
>>> max(data,key=len)
'cherries'
>>> len(max(data,key=len))
8
为这个答案贡献我的一份力量。
这里是 Python 的新手。我试图在一系列嵌套列表中找到值的最长长度。这是一个示例列表类型:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
我在这里想要的答案是 8
,但如果列表更新,这可能会改变。
当我使用 print(len(tableData))
时,我得到 3,嵌套列表的数量。我也无法找到解决此问题的循环。
抱歉,这是一个非常简单的问题,但我不知所措。
在此先感谢您的帮助。
如您所见,len(tableData)
给出了 tableData
的元素数。你想要的是 tableData
的元素 的元素 的长度的最大值:
l = max(len(x) for sublist in tableData for x in sublist)
>>> print(l)
8
也许这对你有用:
new_list = []
for sub_list in tableData:
for item in sub_list:
new_list.append(item)
max_element = max(new_list, key=len)
print(max_element) # this actually prints the item
print(len(max_element)) # this will give you the length
遍历每个元素并获取其 len()
进行比较。
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
maxCount = 0
for lst in tableData:
for elem in lst:
maxCount = max(maxCount, len(elem))
print(maxCount)
输出:
8
from itertools import chain
chain.from_iterable(tableData)
这就像您有一长串单个值,而不是一列值列表。现在在扁平列表中找到最长的项目是微不足道的:
max(chain.from_iterable(tableData), key=len)
这个returns'cherries'
.
max(map(len, chain.from_iterable(tableData)))
这个returns8
.
你可以试试循环...
l = 0
for row in tableData:
for col in row:
l = len(col) if l < len(col) else l
maxLength = 0
for row in tableData:
maxRowElementLength = len(max(row, key=len))
if maxLength < maxRowElementLength:
maxLength = maxRowElementLength
print(maxLength)
>>> import numpy as np
>>> data=np.array([['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]).reshape(-1)
>>> max(data,key=len)
'cherries'
>>> len(max(data,key=len))
8
为这个答案贡献我的一份力量。