Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 转换列表月份名称的Dict_Python_Python 3.x_List - Fatal编程技术网

Python 转换列表月份名称的Dict

Python 转换列表月份名称的Dict,python,python-3.x,list,Python,Python 3.x,List,基本上,我想做的是将一些月份的罗马尼亚语名称转换成英语名称。我只想在每个列表中仅转换索引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',

基本上,我想做的是将一些月份的罗马尼亚语名称转换成英语名称。我只想在每个列表中仅转换索引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。

如果每个子列表中的
名称保持在索引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处:

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

您应该使用字典直接访问翻译:

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']]

您应该使用字典直接访问翻译:

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']]

如果在要转换的名称中找到该名称,请替换它们

for value in mylist:
    if value[4] in conversionsEnNames:
        value[4] = conversionEnNames[value[4]]

如果在要转换的名称中找到该名称,请替换它们

for value in mylist:
    if value[4] in conversionsEnNames:
        value[4] = conversionEnNames[value[4]]

如果你想以自己的方式回答,你可以简单地在最后添加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

如果你想以自己的方式回答,你可以简单地在最后添加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

我们可以通过向循环中添加一些
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
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']
因此,它确实执行了所需的翻译,但是
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)  
输出

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
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']

我们可以通过向循环中添加一些
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
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']
因此,它确实执行了所需的翻译,但是
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)  
输出

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
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']

虽然这个答案很实用,但它并没有按照设计的方式使用dict类。是的,没错。这就是为什么我提到了他是否想以自己的方式回答问题。:)虽然这个答案很实用,但它并没有按照设计的方式使用dict类。是的,没错。这就是为什么我提到了他是否想以自己的方式回答问题。:)你不应该以你现在的方式编辑列表,尤其是当你在上面迭代的时候。列表是使用生成器函数生成的,在列表仍在迭代时修改列表可能会导致各种奇怪的错误。如果您想这样修改,请参考hspandher的答案或使用数值迭代(对于范围内的i(len(list))@NM,我同意修改您正在迭代的列表(或其他集合)可能会很危险。但如果您添加新项目或删除旧项目,这通常是一个问题如果您只是在更改现有项,则可以。如果您反向迭代列表,则可以安全地从列表中删除项。您不应该按现在的方式编辑列表,尤其是在迭代列表时。列表是使用生成器函数生成的,在列表仍在迭代时修改列表可能会导致各种错误d bug。如果你想这样修改,可以参考hspandher的答案,或者使用数值迭代(对于范围内的i(len(list))@NM,我同意修改你正在迭代的列表(或其他集合)可能是危险的。但如果你添加新项或删除旧项,这通常是一个问题好的,如果你只是更改现有的项目。如果你反向迭代列表,你可以安全地从列表中删除项目。谢谢,先生,我知道了。谢谢,先生,我知道了。