【问题标题】:Using a function in another and having a hard time with a loop/argument在另一个函数中使用一个函数并且很难使用循环/参数
【发布时间】:2018-10-15 00:30:26
【问题描述】:

涉及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。函数 gain_date(date) 已经处理好了使它成为一个 int,所以我的问题是如何在 gain_events() 中调用这个函数,这样我就可以到达列表列表中具有日期的每个索引,这样我就可以将它们替换为 int。这只是我的列表列表的一个示例,因为我的列表实际上是一个 file.txt,它实际上有 160 行。但你明白了。

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

【问题讨论】:

  • 您只需要每个子列表中的月份和年份吗?日期是否总是在每个子列表的最后一个索引中?
  • 是的,但是有 2 个日期的事件要转换

标签: python function file for-loop arguments


【解决方案1】:

假设您的列表是:

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'

【讨论】:

  • 这是一个很好的答案,但我不允许修改返回我的日期的 int 的函数,我只有调用它的权利。
  • 然后使用这个 b = '-'.join(a.split('-')[1:]) 代码将 a = '2018-08-04' 转换为 b = '08- 04'
猜你喜欢
  • 2022-01-20
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多