gpt4 book ai didi

python - 列表内的映射转换

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

这是用于输入的 mylist 示例:

mylist = [['RWILY MORSHED', 7670315350025, 'August/2014, Iulie/2014, Septembrie/2014', 1620, 1620], ['AL BADRI MOHAMMED YAHYA TAWFEEQ', 7700119350028, 'Martie/2015, Aprilie/2015, Februarie/2015', 1620, 1620]]

我想要的输出是这样的:

mylist = [['RWILY MORSHED', 7670315350025, 'August 2014, July 2014, September 2014', 1620, 1620], ['AL BADRI MOHAMMED YAHYA TAWFEEQ', 7700119350028, 'March 2015, April 2015, February 2015', 1620, 1620]]

基本上我想将月份名称从罗马尼亚语转换为英语并获得上述所需的输出(我需要所有月份都在与输出相同的索引 2 位置)!

最后我得到了这个:

conversionsEnNames = {"Ianuarie": "January", "Februarie": "February","Martie": "March", "Aprilie": "April","Mai": "May","Iunie": "June", "Iulie": "July","August": "August", "Septembrie": "September","Octombrie": "October", "Noiembrie": "November","Decembrie": "December"}

for i in mylist:
i[2]=i[2].replace("/", " ")
for j in i:
if j in conversionsEnNames:
j = conversionsEnNames[j]
i[2]=j

print(mylist)

但这会打印:

[['RWILY MORSHED', 7670315350025, 'August 2014, Iulie 2014, Septembrie 2014', 1620, 1620], ['AL BADRI MOHAMMED YAHYA TAWFEEQ', 7700119350028, 'Martie 2015, Aprilie 2015, Februarie 2015', 1620, 1620]]

并且可以肯定的是,自从 'Iulie 2014' != 'Iulie' 以来, map 将无法工作,因此它可以将其转换为 July,我要修改什么以实现我的期望输出?非常感谢您的宝贵时间!

最佳答案

应该这样做。

for item in mylist:
item[2] = item[2].replace('/', ' ')
for roman, english in conversionsEnNames.items():
item[2] = item[2].replace(roman, english)

关于python - 列表内的映射转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157331/

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