首页 > 解决方案 > 在另一个函数中使用一个函数并且很难使用循环/参数

问题描述

涉及3个功能:

Obtain_events()
obtain_date(date)
obtain_day_year(month, day) 

此列表列表位于函数 gain_events() 中。

[['Musique', 'Shawn Phillips', '2018-08-24', '2018-08-24'],
 ['Musique', "L'avenue Royale fête l'été!", '2018-08-25', '2018-08-25'],
 ['Musique', 'Perséides musicales', '2018-08-03', '2018-08-03'],
 ['Musique', 'Gaétan Leclerc chante Félix et…', '2018-08-17', '2018-08-17'], 
 ['Musique', 'The Ring of Fire : a Johnny Cash Experience', '2018-07-21', '2018-07-21'],

我必须在 gain_events() 中调用 gain_date(date) 才能更改所有字符串为 int 的日期。现在,函数 gain_date(date) 已经工作并且负责将参数中的 str 作为 'YYYY-MM-DD'。我在参数方面遇到了问题,我应该如何获取每个具有日期的索引并将它们放入我的日期参数中(每个子列表都有 2 个日期)。现在这是每个功能的代码

def obtenir_events():

    evenement = open("evenements.txt", "r", encoding="utf-8-sig")
    ma_liste = []

    for element in evenement.readlines():
        #return the list of lists.
        ma_liste.append(element.strip().split('/'))      


def obtain_date(date):

date_list = date[5:].split('-')
        month,day = date_list
        day = obtenir_day_year(int(month),int(day))
        # This function works as far as I can tell. I'm allowed to modify it,
        # but I don't see the point. 
        return  day    


 obtain_day_year(month, day)   

 date_reference = date(2017, 12, 31) 

 date_actuelle = date(2018, mois, jour) 

## works and I'm not allowed to modify it. It's in french but it works only if you
## import **from datetime import date**. It returns the days of the year as
## long as it's an int, and it's working since I'm using it in
## `obtain_date(date).`
return (date_actuelle - date_reference).days

所以问题来了:

我需要能够在我在 gain_events() 中使用的同一个 for 循环中调用 gain_date(date),以将列表列表中的所有日期更改为返回一年中的天数的 int。因此,例如,如果我在列表列表中输入“2018-01-01”,它将被 1 替换,如果我输入“2018-12-31”,它将是 365。函数获取日期(日期)已经处理好了使它成为一个 int,所以我的问题是如何在 gain_events() 中调用此函数,以便我可以访问列表列表中具有日期的每个索引,以便我可以将它们替换为 int。这只是我的列表列表的一个示例,因为我的列表实际上是一个 file.txt,它实际上有 160 行。但你明白了。

PS我觉得我的问题很容易解决。我只是认为我仍在为调用另一个函数而苦苦挣扎,而且我也无法达到列表中所有 sub_lists 的索引。因此,如果您不确定您是否理解我的解释,请尝试编写一些非常简单的代码,以向我展示如何使用正确的索引实现我的函数,以便我可以在我的循环中访问它们。

标签: pythonfunctionfilefor-looparguments

解决方案


假设您的列表是:

a = [['Musique', 'Shawn Phillips', '2018-08-24', '2018-08-24'], ['Musique', 
      "L'avenue Royale fête l'été!", '2018-08-25', '2018-08-25'], ['Musique', 
      'Perséides musicales', '2018-08-03', '2018-08-03'], ['Musique', 'Gaétan 
      Leclerc chante Félix et…', '2018-08-17', '2018-08-17'], ['Musique', 'The 
      Ring of Fire : a Johnny Cash Experience', '2018-07-21', '2018-07-21']]

现在编写一个函数来获取年份的日期:

def get_date(a):
    try:
        a = [int(i) for i in a.split('-')]
        return date(*a).strftime('%j')
    except ValueError:
        return a

现在使用以下列表推导获取新列表:

from datetime import date
b = [[get_date(i) for i in j] for j in a]

输出:

[['Musique', 'Shawn Phillips', '236', '236'],
 ['Musique', "L'avenue Royale fête l'été!", '237', '237'],
 ['Musique', 'Perséides musicales', '215', '215'],
 ['Musique', 'Gaétan Leclerc chante Félix et…', '229', '229'],
 ['Musique', 'The Ring of Fire : a Johnny Cash Experience', '202', '202']]

要获取日期的最后两位数字:

a = '2018-08-24'
b = '-'.join(a.split('-')[1:])
print(b) # will return '08-24'

推荐阅读