Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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
在dict中搜索一个键,并将该键的值分配给变量Python_Python_Dictionary - Fatal编程技术网

在dict中搜索一个键,并将该键的值分配给变量Python

在dict中搜索一个键,并将该键的值分配给变量Python,python,dictionary,Python,Dictionary,我有一句话: dict_meses = {1: 'Enero', 2: 'Febrero', 3: 'Marzo', 4: 'Abril', 5: 'Mayo', 6: 'Junio', 7: 'Julio', 8: 'Agosto', 9: 'Setiembre', 10: 'Octubre', 11: 'Noviembre', 12: 'Diciembre'} 我需要将类似于“14/1/2015”的字符串上的月份更改为dict中对应的月份。例如,如果a有“14/

我有一句话:

dict_meses = {1: 'Enero', 2: 'Febrero', 3: 'Marzo', 4: 'Abril', 5: 'Mayo', 6: 'Junio', 7: 'Julio', 8: 'Agosto',
              9: 'Setiembre', 10: 'Octubre', 11: 'Noviembre', 12: 'Diciembre'}
我需要将类似于“14/1/2015”的字符串上的月份更改为dict中对应的月份。例如,如果a有“14/1/2015”,我需要将其更改为“1/Enero/2015”

我正在尝试这样做:

def xxx(days):   -----> days is a list of tuples like this [('14/1/2015', 500), ...]

    dict_months = {1: 'Enero', 2: 'Febrero', 3: 'Marzo', 4: 'Abril', 5: 'Mayo', 6: 'Junio', 7: 'Julio', 8: 'Agosto',
              9: 'Setiembre', 10: 'Octubre', 11: 'Noviembre', 12: 'Diciembre'}
    days_list = []
    for i in days:
        lista = list(i)
        fecha_entera = lista[0].split('/') ---> ['14','1','2015']
        dia = fecha_entera[1] ----------------> '1'
        if int(dia) in dict_meses.keys():
            fecha_entera[1] = ????------------> want to change '1' to 'Enero'
            dias_lista.append(fecha_entera)
    return dias_lista
问题:如何获取与日期表示的键对应的值

如果我没有解释清楚,请让我知道,我会更加努力


提前感谢您提供的帮助

有关字符串解决方案,请使用“/1/”上的字符串“replace”函数


您可以使用datetime使用%B和srftime解析日期,以获得所需的输出:

from datetime import datetime
dte = '14/1/2015'
print(datetime.strptime(dte,"%d/%m/%Y").strftime("%d/%B/%Y"))
%B
将为您提供区域设置的完整月份名称

In [1]: from datetime import datetime   
In [2]: dte = '14/1/2015'    
In [3]: import locale    
In [4]: locale.setlocale(locale.LC_ALL,"es_SV.utf_8")
Out[4]: 'es_SV.utf_8'    
In [5]: print(datetime.strptime(dte,"%d/%m/%Y").strftime("%d/%B/%Y"))
14/enero/2015
如果每个第一个元素都是日期字符串:

def xxx(days):
    return [datetime.strptime(dte, "%d/%m/%Y").strftime("%d/%B/%Y")
            for dte, _ in days]
如果您想使用dict:

def xxx(days):
    dict_months = {"1": 'Enero', "2": 'Febrero', "3": 'Marzo', "4": 'Abril', "5": 'Mayo', "6": 'Junio', "7": 'Julio',
                   "8": 'Agosto',
                   "9": 'Setiembre', "10": 'Octubre', "11": 'Noviembre', "12": 'Diciembre'}
    days_list = []
    for sub in map(list, days):
        dy, mn, year = sub[0].split()
        days_list.append("{}/{}/{}".format(dy, dict_months[mn], year))
    return days_list

你应该将键用作字符串,必须转换为int来进行比较是毫无意义的。

为什么否决票???…有人不喜欢这样的西班牙语单词?如果你问“如果我知道键,我如何获得dict的值?”,试试
value=my_dict[key]
你实际的问题似乎是“如何更改列表中项目的值”答案是fecha_entera[1]=“Enero”。此
1
不是该项的值,而是列表中从零开始计数的第二项。
def xxx(days):
    dict_months = {"1": 'Enero', "2": 'Febrero', "3": 'Marzo', "4": 'Abril', "5": 'Mayo', "6": 'Junio', "7": 'Julio',
                   "8": 'Agosto',
                   "9": 'Setiembre', "10": 'Octubre', "11": 'Noviembre', "12": 'Diciembre'}
    days_list = []
    for sub in map(list, days):
        dy, mn, year = sub[0].split()
        days_list.append("{}/{}/{}".format(dy, dict_months[mn], year))
    return days_list