基本上我想做的是用英文名称转换一些罗马尼亚语的名字。我只想在每个列表中仅转换索引4位置月份,第二个应该保持不变,我会给你举例:
我有这个列表清单:
mylist = [[1,2,3,4,'Septembrie', 'August'], [1,2,3,4,'Martie', 'August'], [1,2,3,4,'Mai', 'August']]
我想要的输出应该是这样的:
mylist = [[1,2,3,4,'September', 'August'], [1,2,3,4,'March', 'August'], [1,2,3,4,'May', 'August']]
对于8月份(在指数5位置),我不想做任何改变,只要保持原样!
我写了这段代码:
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"}
mylist = [[1,2,3,4,'Septembrie', 'August'], [1,2,3,4,'Martie', 'August'], [1,2,3,4,'Mai', 'August']]
for i in mylist:
for j in i:
if j in conversionsEnNames:
j = conversionsEnNames[j]
i[4]=j
但是当我打印mylist时,它会输出:
[[1, 2, 3, 4, 'August', 'August'], [1, 2, 3, 4, 'August', 'August'], [1, 2, 3, 4, 'August', 'August']]
但为什么呢?因为我已经声明了i[4]=j
,我有点困惑,所以我如何能够实现我想要的输出,如果可能的话,我更愿意处理我写的有点相同的代码结构,带有dict和for循环, 谢谢!!我使用python 3。
答案 0 :(得分:1)
您应该使用字典直接访问翻译:
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"}
mylist = [[1,2,3,4,'Septembrie', 'August'], [1,2,3,4,'Martie', 'August'], [1,2,3,4,'Mai', 'August']]
for i in mylist:
month = i[4]
if month in conversionsEnNames:
i[4] = conversionsEnNames[month]
print(mylist)
输出:
[[1, 2, 3, 4, 'September', 'August'], [1, 2, 3, 4, 'March', 'August'], [1, 2, 3, 4, 'May', 'August']]
答案 1 :(得分:1)
如果在要转换的名称中找到该名称,请将其替换。
for value in mylist:
if value[4] in conversionsEnNames:
value[4] = conversionEnNames[value[4]]
答案 2 :(得分:1)
我们可以通过向您的循环添加一些print
调用来了解您的代码产生该输出的原因。
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"
}
mylist = [
[1, 2, 3, 4, 'Septembrie', 'August'],
[1, 2, 3, 4, 'Martie', 'August'],
[1, 2, 3, 4, 'Mai', 'August'],
]
print('Data:')
for row in mylist:
print(row)
print()
for i in mylist:
print('i:', i)
for j in i:
print(' old j:', j)
if j in conversionsEnNames:
j = conversionsEnNames[j]
print(' new j:', j)
i[4] = j
<强>输出强>
Data:
[1, 2, 3, 4, 'Septembrie', 'August']
[1, 2, 3, 4, 'Martie', 'August']
[1, 2, 3, 4, 'Mai', 'August']
i: [1, 2, 3, 4, 'Septembrie', 'August']
old j: 1
old j: 2
old j: 3
old j: 4
old j: Septembrie
new j: September
old j: August
new j: August
i: [1, 2, 3, 4, 'Martie', 'August']
old j: 1
old j: 2
old j: 3
old j: 4
old j: Martie
new j: March
old j: August
new j: August
i: [1, 2, 3, 4, 'Mai', 'August']
old j: 1
old j: 2
old j: 3
old j: 4
old j: Mai
new j: May
old j: August
new j: August
因此 执行所需的翻译,但i[4]
的翻译会覆盖i[5]
。
这是您的代码的修改版本,可以满足您的需求。我们使用if
方法而不是使用get
来测试密钥是否在翻译词典中,如果密钥不在字典中,则告诉它返回密钥。
english_months = {
"Ianuarie": "January", "Februarie": "February", "Martie": "March",
"Aprilie": "April", "Mai": "May", "Iunie": "June", "Iulie": "July",
"August": "August", "Septembrie": "September", "Octombrie": "October",
"Noiembrie": "November", "Decembrie": "December"
}
mylist = [
[1, 2, 3, 4, 'Septembrie', 'August'],
[1, 2, 3, 4, 'Martie', 'August'],
[1, 2, 3, 4, 'Mai', 'August'],
]
print('Original:')
for row in mylist:
print(row)
print()
for row in mylist:
month = row[4]
row[4] = english_months.get(month, month)
print('Translated:')
for row in mylist:
print(row)
<强>输出强>
Original:
[1, 2, 3, 4, 'Septembrie', 'August']
[1, 2, 3, 4, 'Martie', 'August']
[1, 2, 3, 4, 'Mai', 'August']
Translated:
[1, 2, 3, 4, 'September', 'August']
[1, 2, 3, 4, 'March', 'August']
[1, 2, 3, 4, 'May', 'August']
答案 3 :(得分:0)
如果每个子列表中的name
保持在索引4:
for l in mylst: // iterate through list sublists [[sublist1], [sublist2], ... ]
if l[4] in conversionsEnNames: // check if key exists in dict
l[4] = conversionsEnNames[l[4]] // supply value of key to sublist index 4
答案 4 :(得分:0)
如果你想以你的方式回答你可以简单地添加break,因为在执行if block之后它还会检查最后一个元素。并且在字典“August”键中已经没有任何区别
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"}
mylist = [[1,2,3,4,'Septembrie', 'August'], [1,2,3,4,'Martie', 'August'], [1,2,3,4,'Mai', 'August']]
for i in mylist:
for j in i:
if j in conversionsEnNames:
j = conversionsEnNames[j]
i[4]=j
break