gpt4 book ai didi

python - 遍历嵌套列表并对每个元素进行操作

转载 作者:太空宇宙 更新时间:2023-11-04 09:25:43 24 4
gpt4 key购买 nike

<分区>

我有一个列表列表,我正在尝试删除所有非字母字符。

我尝试使用 isalpha()

data = [
['we', '\n\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best'],
['though', '10/3/2011', 'it', 'may', 'not', '\n\n', 'make', 'landfall', 'all', 'week', '2 000 €', 'if', 'it', 'follows', 'that', '•', 'track'],
['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '•', 'floods', '\n\n', 'are', 'possible'],
]

new_data = ''.join([i for i in data if i.isalpha()])

预期输出:

['we will pray and hope for the best', 
'though it may not make landfall all week if it follows that track',
'heavy rains capable of producing life threatening flash floods are possible']

我的输出:

AttributeError: 'list' object has no attribute 'isalpha'

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