Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
Python-创建具有多个子列表的列表_Python_List - Fatal编程技术网

Python-创建具有多个子列表的列表

Python-创建具有多个子列表的列表,python,list,Python,List,我需要做的很简单,但我不知道怎么做 我在列表中组织了很多字符串: list = ['my name is Marco and i'm 24 years old', 'my name is Jhon and i'm 30 years old'] 我使用正则表达式从列表的每个元素中提取信息: for element in list: name = re.findall('my name is (.*?) and i\'m', element, re.DOTALL)[0] age = re.

我需要做的很简单,但我不知道怎么做

我在列表中组织了很多字符串:

list = ['my name is Marco and i'm 24 years old', 'my name is Jhon and i'm 30 years old']
我使用正则表达式从列表的每个元素中提取信息:

for element in list:
  name = re.findall('my name is (.*?) and i\'m', element, re.DOTALL)[0]
  age = re.findall('and i\'m (.*?) years old', element, re.DOTALL)[0]
现在我要做的是重新编译一个新的列表,它包含由名称和年龄组成的子列表元素

例如:

for element in newlist:
  name = element[0]
  age = element[1]

有可能这样做吗?

这里有一个解决方案,它可以完全按照您的要求来做。这将创建一个新列表,其中包含具有名称和年龄的子列表

new_list = []
for element in list:
   name = re.findall('my name is (.*?) and i\'m', element, re.DOTALL)[0]
   age = re.findall('and i\'m (.*?) years old', element, re.DOTALL)[0]
   new_list.append([name, age])
>>> s = "my name is Marco and i'm 24 years old"
>>> pattern = r"my name is\s+(.+)\s+and i'm\s+(\d+)\s+years old"
>>> m = re.match(pattern, s)
>>> print(m.groups())
('Marco', '24')

您可以使用一个简单的列表来做您想做的事情:

name_pat = re.compile('my name is (.*?) and i\'m', re.DOTALL)
age_pat = re.compile('and i\'m (.*?) years old', re.DOTALL)

new_list = [[name_pat.findall(elem)[0], age_pat.findall(elem)[0]] for elem in your_list]

首先,不需要两个正则表达式来提取name和age的两个值

new_list = []
for element in list:
   name = re.findall('my name is (.*?) and i\'m', element, re.DOTALL)[0]
   age = re.findall('and i\'m (.*?) years old', element, re.DOTALL)[0]
   new_list.append([name, age])
>>> s = "my name is Marco and i'm 24 years old"
>>> pattern = r"my name is\s+(.+)\s+and i'm\s+(\d+)\s+years old"
>>> m = re.match(pattern, s)
>>> print(m.groups())
('Marco', '24')
您可以使用列表理解来构建新列表:

>>> data = ["my name is Marco and i'm 24 years old", "my name is Jhon and i'm 30 years old"]
>>> new_list = [re.match(pattern, s).groups() for s in data]
>>> print(new_list)
[('Marco', '24'), ('Jhon', '30')]
结果是一个元组列表。如果您确实需要列表列表,可以执行以下操作:

new_list = [list(re.match(pattern, s).groups()) for s in data]
列表理解是此循环的简称:

new_list = []
for s in data:
    m = re.match(pattern, s)
    if m:
        new_list.append(m.groups())

此循环与列表理解之间的主要区别在于前者可以处理与模式不匹配的字符串,而列表理解假定模式始终匹配(如果不匹配,则会产生异常)。您可以在列表理解中处理这个问题,但是,它开始变得难看,因为您需要执行两次正则表达式匹配:一次检查模式是否匹配,然后再次提取实际值。在这种情况下,我认为显式for循环更干净。

是的,这是可能的,而且非常简单。请看下面的答案,我已经更新了你的代码