如何在列表中提取字符串的一部分并将其更改为int。 [英] How do I extract a part of a string in a list and change it into an int.
问题描述
所以我有一份清单:
So i have a list of list:
[['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'],
我只想提取每个列表的月份和日期,我想在另一个函数中使用它们。但我很难解读几个月和几天。
我需要把它们放在这个功能中:
Im looking to extract only the months and days of every lists and i want to use them in another function. but im having a hard time extracting the months and days only.
I need to put them in this function :
def obtain_date(date):
列表list实际上也是另一个函数 obtain_events
并且在我提取了我的月/天之后,我将不得不找到另一个函数的确切天数,但我主要关注的是找到我的能力仅在我的 obtain_date(date):
函数中只有一个参数时提取月份和日期。此外,在我提取月份/天之后(或之前),我需要将它们更改为int,因为它们目前是字符串形式。
我尝试过:
尝试了几个循环,例如:
the list of lists is actually another function too obtain_events
and after i extracted my months/days, im gonna have to find the exact days with another function, but im mostly focused on finding how i can extract the months and days only while having only 1 argument in my obtain_date(date):
function. Also, after( or before) i extract the months/days, i need to change them to int, as they are currently in a form of strings.
What I have tried:
Tried a few loops like:
for element in obtain_events:
if '2018' in element:
element.remove(2018)
但它没有用,我也尝试了一些其他的循环,但我很困惑。
But it didnt work and i also tried a few other loops but im getting confused.
推荐答案
for element in obtain_events:
if '2018' in element:
numbers = element.split('-')
...
变量数字
将包含以下内容: ['2018','08','24']
。您现在可以对每个字段执行所需的操作,必要时将它们转换为整数值。
The variable numbers
will be a list containing something like: [ '2018', '08', '24' ]
. You can now do what you want with each field, if necessary converting them to integer values.
这篇关于如何在列表中提取字符串的一部分并将其更改为int。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!