You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将列表中的数字元素转换为对应英文单词形式?

解决方法

最直接的思路是建立数字到对应英文单词的映射关系,再通过遍历原列表完成元素替换。以下是几种实用实现方式:

方法一:字典映射 + 列表推导式

先定义数字与英文单词的对应字典,再用列表推导式快速生成目标列表:

L = [1, 2, 3]
num_to_word = {1: 'one', 2: 'two', 3: 'three'}
L1 = [num_to_word[num] for num in L]
print(L1)  # 输出: ['one', 'two', 'three']

方法二:循环遍历赋值

如果偏好更直观的循环写法:

L = [1, 2, 3]
num_to_word = {1: 'one', 2: 'two', 3: 'three'}
L1 = []
for num in L:
    L1.append(num_to_word[num])

方法三:适配更多数字(可选)

如果你的列表可能包含更多数字,可以借助第三方库num2words(先通过pip install num2words安装),它支持从0到超大数字的英文转换:

from num2words import num2words

L = [1, 2, 3]
L1 = [num2words(num) for num in L]
print(L1)  # 输出: ['one', 'two', 'three']

注:该库还支持多语言转换,比如num2words(5, lang='fr')会返回法语单词"cinq"。

内容的提问来源于stack exchange,提问作者AlexDeSouza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 12:15:25