gpt4 book ai didi

python - 列表中项目的最大长度(python)

转载 作者:行者123 更新时间:2023-12-01 09:07:46 24 4
gpt4 key购买 nike

我正在尝试获取嵌套列表中包含的项目的最大长度。这些最大长度值将用于创建数据库表。

获取嵌套列表中单个项目的最大长度的最佳方法是什么?

if len(news_article_rows) > 0:
try:
for item in news_article_rows:
article_type = item[0]
source_name = item[1]
writer_name = item[2]
article_href = item[3]
topic_class = item[4]
other_information = item[5]
date_published = item[6]
date_revised = item[7]

max_length = max(article_type, key=len)
print (max_length)

except Exception as exception:
traceback_str = ''.join(traceback.format_tb(exception.__traceback__))
print(traceback_str)

最佳答案

您的循环不会将不同的article_type值相互比较,因此您没有获得最大值。

试试这个:

max(len(item[0]) for item in news_article_rows)

这会将每个值相互比较,以便从所有值中获取实际的最大值。

关于python - 列表中项目的最大长度(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51909982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com