简体   繁体   English

在python中设置字典值不会更新该值吗?

[英]Setting dictionary value in python does not update the value?

I am trying to simply replace missing date values from two strings, but splitting them into a dictionary of "year", "month", etc. However I am running into a very odd issue I did not find the answer for after 20 minutes googling. 我试图从两个字符串中替换掉缺失的日期值,但是将它们分成“ year”,“ month”等字典。但是我遇到了一个很奇怪的问题,在谷歌搜索20分钟后我找不到答案。

Here's my code: 这是我的代码:

def date_split(date):
    parts = date.split('-')
    if len(parts) == 1:
        year = parts[0]
        dates = {'y' : year}
    if len(parts) == 2:
        year = parts[0]
        month = parts[1]
        dates = {'y' : year, 'm' : month}
    if len(parts) == 3:
        year = parts[0]
        month = parts[1]
        day = parts[2]
        dates = {'y' : year, 'm' : month, 'd' : day}
    return dates

def recompile(date):
    lista = list(date.values())
    date = "-".join(lista)
    return date

std = "2018-07-18"
x = "XXXX-01"

if date_split(x)['y'] == 'XXXX':
    new = str(date_split(std)['y'])
    date_split(x)['y'] = new
    date = recompile(date_split(x))
    print(date)

I just began twiddling with it so it's very rough, but I am working on it. 我刚刚开始花哨的时间,所以它很粗糙,但是我正在研究它。 However, while date_split(std)['y'] prints out as 2018 , after date_split(x)['y'] = new , the value of date_split(x)['y'] prints out as "XXXX" still. 然而,虽然date_split(std)['y']打印出作为2018 ,后date_split(x)['y'] = new ,的值date_split(x)['y']打印出作为"XXXX"静止。 So the dictionary does not update the value. 因此,字典不会更新该值。 Is there something I'm missing about how dictionaries work in python? 关于字典在python中的工作方式,我缺少什么吗? I'm quite new to it. 我很新。 I've tried also different forms of the dictionary update command. 我也尝试了字典更新命令的不同形式。

Using Python 3.6 with Anaconda. 将Python 3.6与Anaconda结合使用。

You change the return value of a function. 您更改函数的返回值。 Since you don't store it anywhere, it's lost. 由于您不将其存储在任何地方,因此丢失了。 Instead, do something like 而是做类似的事情

 custom_date = date_split(x)
 custom_date['y'] = new
 date = recompile(custom_date)

Each time you call date_split(x) it returns a new dict, so updating it won't have an effect on the next call. 每次调用date_split(x)它都会返回一个新的字典,因此对其进行更新不会对下一个调用产生影响。 Instead, you should assign the returning value of date_split(x) to a variable and then use that variable for further processing. 相反,您应该将date_split(x)的返回值分配给变量,然后使用该变量进行进一步处理。

std = "2018-07-18"
x = "XXXX-01"
s = date_split(x)
if s['y'] == 'XXXX':
    new = str(date_split(std)['y'])
    s['y'] = new
    date = recompile(s)
    print(date)
def date_split(date):
    parts = date.split('-')
    if len(parts) == 1:
        year = parts[0]
        dates = {'y' : year}
    if len(parts) == 2:
        year = parts[0]
        month = parts[1]
        dates = {'y' : year, 'm' : month}
    if len(parts) == 3:
        year = parts[0]
        month = parts[1]
        day = parts[2]
        dates = {'y' : year, 'm' : month, 'd' : day}
    return dates

def recompile(date):
    lista = list(date.values())
    date = "-".join(lista)
    return date

std = "2018-07-18"
x = "XXXX-01"

if date_split(x)['y'] == 'XXXX':
    old = date_split(x)
    new = str(date_split(std)['y'])
    print(old)
    print(new)
    old['y'] = new
    date = recompile(old)
    print(date)

you cannot save changes on a function call, you have to store it in a variable. 您不能在函数调用中保存更改,而必须将其存储在变量中。

As others have said you need to save the dictionary returned by date_split so you can modify it. 正如其他人所说,您需要保存date_split返回的字典,以便对其进行修改。 However, your date_split function is much larger than it needs to be, so I've written a more compact version using zip . 但是,您的date_split函数比需要的要大得多,因此我使用zip编写了一个更紧凑的版本。

Your recompile function won't always work correctly on earlier versions of Python, where a dict is an unordered collection, but it's ok on Python 3.6+ since dict now retains the insertion order. 您的recompile函数将无法始终在早期版本的python上正常运行,因为dict是无序集合,但是在Python 3.6+上是可以的,因为dict现在保留了插入顺序。 Also, there's no need to call str in new = str(date_split(std)['y']) . 另外,也不需要在new = str(date_split(std)['y'])调用str All the values in the dict returned by date_split are already strings. date_split返回的dict中的所有值已经是字符串。

def date_split(date):
    return dict(zip("ymd", date.split("-")))

def recompile(date):
    return "-".join(date.values())

std = "2018-07-18"
x = "XXXX-01"

x_split = date_split(x)
print("Original", x_split)
if x_split["y"] == "XXXX":
    x_split["y"] = date_split(std)["y"]
    print("Updated ", x_split)
    date = recompile(x_split)
    print("String  ", date)

output 产量

Original {'y': 'XXXX', 'm': '01'}
Updated  {'y': '2018', 'm': '01'}
String   2018-01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM