Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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

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_For Loop_Containers - Fatal编程技术网

Python 不使用循环访问容器中的所有类变量

Python 不使用循环访问容器中的所有类变量,python,list,for-loop,containers,Python,List,For Loop,Containers,我有一个“用户”列表,其中包含“用户”类的对象。每个用户都有一个“记录”列表,其中包含“记录”类的对象。每个记录都有一个datetime变量,描述特定记录的创建时间 我想创建一个包含所有用户记录的所有日期时间变量的列表。我已经使用嵌套循环实现了这一点,但我想有一种更聪明的方法 # Add all dates to one list dateList = list() for i in range(0,nUsers): for j in range(0, len(users[i].reco

我有一个“用户”列表,其中包含“用户”类的对象。每个用户都有一个“记录”列表,其中包含“记录”类的对象。每个记录都有一个datetime变量,描述特定记录的创建时间

我想创建一个包含所有用户记录的所有日期时间变量的列表。我已经使用嵌套循环实现了这一点,但我想有一种更聪明的方法

# Add all dates to one list
dateList = list()
for i in range(0,nUsers):
    for j in range(0, len(users[i].records)):
        dateList.append(users[i].records[j].datetime)

列表理解可能就是你的意思

date_list = [r.datetime for user in users for r in user.records]
Python变量应该是小写的,带有下划线,而不是小写


此外,在Python中迭代不需要使用索引。您可以直接对项目进行迭代。

列表理解可能就是您的意思

date_list = [r.datetime for user in users for r in user.records]
Python变量应该是小写的,带有下划线,而不是小写


此外,在Python中迭代不需要使用索引。您可以直接对项目进行迭代。

列表理解可能就是您的意思

date_list = [r.datetime for user in users for r in user.records]
Python变量应该是小写的,带有下划线,而不是小写


此外,在Python中迭代不需要使用索引。您可以直接对项目进行迭代。

列表理解可能就是您的意思

date_list = [r.datetime for user in users for r in user.records]
Python变量应该是小写的,带有下划线,而不是小写


此外,在Python中迭代不需要使用索引。您可以直接在项目上进行迭代。

您必须始终使用循环,但您可以在单个列表中执行此操作:

date_list = [record.datetime for user in users for record in user.records]

请注意,即使使用单独的循环,在Python中也不应在某个范围内迭代,然后使用索引获取元素;始终迭代对象本身,因此
对于用户中的用户
等。

您始终必须使用循环,但您可以在单个列表中完成:

date_list = [record.datetime for user in users for record in user.records]

请注意,即使使用单独的循环,在Python中也不应在某个范围内迭代,然后使用索引获取元素;始终迭代对象本身,因此
对于用户中的用户
等。

您始终必须使用循环,但您可以在单个列表中完成:

date_list = [record.datetime for user in users for record in user.records]

请注意,即使使用单独的循环,在Python中也不应在某个范围内迭代,然后使用索引获取元素;始终迭代对象本身,因此
对于用户中的用户
等。

您始终必须使用循环,但您可以在单个列表中完成:

date_list = [record.datetime for user in users for record in user.records]

请注意,即使使用单独的循环,在Python中也不应在某个范围内迭代,然后使用索引获取元素;始终迭代对象本身,因此
对于user in users
等等。

首先去掉索引并迭代对象本身

dateList = list()
for user in users:
    for record in user.records:
        dateList.append(record.datetime)
然后,您可以使用列表理解

dateList = [record.datetime for user in users for record in user.records]

首先,去掉索引并迭代对象本身

dateList = list()
for user in users:
    for record in user.records:
        dateList.append(record.datetime)
然后,您可以使用列表理解

dateList = [record.datetime for user in users for record in user.records]

首先,去掉索引并迭代对象本身

dateList = list()
for user in users:
    for record in user.records:
        dateList.append(record.datetime)
然后,您可以使用列表理解

dateList = [record.datetime for user in users for record in user.records]

首先,去掉索引并迭代对象本身

dateList = list()
for user in users:
    for record in user.records:
        dateList.append(record.datetime)
然后,您可以使用列表理解

dateList = [record.datetime for user in users for record in user.records]

可以使用两种嵌套列表理解:

date_list = [record.datetime for record in
                [user.record for user in user_list]]

可以使用两种嵌套列表理解:

date_list = [record.datetime for record in
                [user.record for user in user_list]]

可以使用两种嵌套列表理解:

date_list = [record.datetime for record in
                [user.record for user in user_list]]

可以使用两种嵌套列表理解:

date_list = [record.datetime for record in
                [user.record for user in user_list]]