我的 140 个元素列表只打印第一个
Posted
技术标签:
【中文标题】我的 140 个元素列表只打印第一个【英文标题】:my list of 140 elements just print the first one 【发布时间】:2021-12-03 23:20:58 【问题描述】:我的代码只打印列表(活动)的第一个元素,因为 0;但是,当我尝试[:]
时,我得到'列表索引必须是整数或切片,而不是 str' 类型错误。
我需要打印列表中的 140 个元素,但我真的不知道如何修复它。而且我真的不想编写其他代码,因为这是我可以在几秒钟内获得两个日期之间差异的唯一方法。
这是我的代码:
import json
import datetime
import copy
#Read json
with open('/Users/kenyacastellanos/Downloads/data.json') as json_data_file:
data = json.load(json_data_file)
print(data)
#Just show first 10 elements
print("Sin ordenar:",data['activities'][:10])
## Realizamos el ordenamiento por llave, la llave es user_id, creamos una funcion lambda para el ordenamiento
data['activities'].sort(key = lambda x: x['user_id'])
#Just show first 10 elements
print("Ordenamos:", data['activities'][:10])
# Duration
date1 = datetime.datetime.fromisoformat(data['activities'][:]['answered_at'])
date2 = datetime.datetime.fromisoformat(data['activities'][:]['first_seen_at'])
difference_date = (date1-date2)
print("Duration in seconds:", difference_date.seconds, difference_date.microseconds)
这是我的清单包含的内容,其中 140 个。 enter image description here
"activities":[
"id":272961,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:09:53.830+00:00",
"first_seen_at":"2021-09-19T21:09:40.830+00:00"
,
"id":402616,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:10:27.830+00:00",
"first_seen_at":"2021-09-19T21:10:09.830+00:00"
,
"id":64828,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:13:19.830+00:00",
"first_seen_at":"2021-09-19T21:13:03.830+00:00"
,
"id":397256,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:18:06.830+00:00",
"first_seen_at":"2021-09-19T21:17:47.830+00:00"
,
"id":202872,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:19:00.830+00:00",
"first_seen_at":"2021-09-19T21:18:54.830+00:00"
,
"id":367396,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:23:30.830+00:00",
"first_seen_at":"2021-09-19T21:23:18.830+00:00"
,
"id":378117,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:25:12.830+00:00",
"first_seen_at":"2021-09-19T21:25:05.830+00:00"
,
"id":257362,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:30:30.830+00:00",
"first_seen_at":"2021-09-19T21:30:11.830+00:00"
,
"id":80097,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:34:23.830+00:00",
"first_seen_at":"2021-09-19T21:34:13.830+00:00"
,
【问题讨论】:
哪个列表?您的代码中没有列表? 为什么不使用基本的for… in
循环?
“活动”是我的清单
这是什么,熊猫数据框?给我们完整的图片
欢迎来到 ***,感谢您坚持提出您的问题并对其进行编辑以将其转换为我们可以使用的形式!这是一个很好的问题,我希望你能得到你需要的帮助。请参阅本指南以获取 how to ask a good question 以供将来参考。
【参考方案1】:
您遇到了麻烦,因为data['activities'][0]
返回的数据类型与data['activities'][:]
非常不同。
data['activities'][0]
返回列表中的第一个字典:
"id":272961,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:09:53.830+00:00",
"first_seen_at":"2021-09-19T21:09:40.830+00:00"
另一方面,列表的任何子集,包括data['activities'][:10]
或data['activities'][:]
,都会返回一个列表:
>>> data['activities'][:]
[
"id":272961,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:09:53.830+00:00",
"first_seen_at":"2021-09-19T21:09:40.830+00:00"
,
"id":402616,
"user_id":"izi57ti5",
"answered_at":"2021-09-19T21:10:27.830+00:00",
"first_seen_at":"2021-09-19T21:10:09.830+00:00"
,
...
]
您对结果的处理是尝试从字典中访问'answered_at'
项。在第一种情况下,这很好用:
>>> data['activities'][0]['answered_at']
"2021-09-19T21:09:53.830+00:00"
但是第二个产生了一个TypeError,表明你不能使用字符串键访问列表的元素,这是真的。
相反,您必须决定如何修改工作流程以处理列表中的许多项目。例如,您可以使用循环:
for entry in data['activities']:
# entry now points to the dictionaries within the list
date1 = datetime.datetime.fromisoformat(entry['answered_at'])
date2 = datetime.datetime.fromisoformat(entry['first_seen_at'])
difference_date = (date1-date2)
# this will print out once for every element in data['activities']
print(
"Duration in seconds:",
difference_date.seconds,
difference_date.microseconds,
)
【讨论】:
非常感谢您提供示例和整个解释,以及您的时间,现在一切正常!?以上是关于我的 140 个元素列表只打印第一个的主要内容,如果未能解决你的问题,请参考以下文章
只有第一个和第三个元素从 python 列表打印到 csv 文件