python,尝试合并列表中的列表时,所有值都被破坏了

我在列表中有 id 列表,如下所示:


[[1347,1805371369,1805371401,1805371409,1805371424,1805371434,1805371462],[1805371691,1805371699,1805371712,1805371715,1805371725,1805371742,1917952665]']


我试图将它们合并到一个列表中,如下所示:


[1347,1805371369,1805371401,1805371409,1805371424,1805371434,1805371462,1805371691,1805371699,1805371712,1805371715,1805371725,1805371742,1917952665' ]


但我的结果变成了这样,我不知道为什么:


['1','3','4','7',',','1','8','0','5','3','7','1','3','6','9'.......']']

我尝试过 itertools 、 flat 和 using for 循环的方法,但它们都给了我同样的东西,所以我认为 list 本身有问题,但我不确定是什么。请帮忙


import itertools    

r=[[1347,1805371369,1805371401,1805371409,1805371424,1805371434,1805371462],[1805371691,1805371699,1805371712,1805371715,1805371725,1805371742,1917952665]]

merged = list(itertools.chain(*r))

print(merged)

实际代码:


import requests

import json

from datetime import datetime

from datetime import timedelta

import itertools


url='https://www.example.com/api/rest/1.1/etl/surveyresponses/ids/'

headers = {'Content-Type': "application/x-www-form- 

urlencoded",'Authorization': "Bearer ******-****-****-****-*********"}

start_date = "2016-01-01"

stop_date = "2016-01-08"

endDate = "2016-02-014"

orgid='****'

r=[]

#Can only call 7 days at once, need to call the API multiple time and combine the result together for another API call 

while stop_date < endDate:

    start = datetime.strptime(start_date, "%Y-%m-%d") + timedelta(days=7) 

    stop = datetime.strptime(stop_date, "%Y-%m-%d") + timedelta(days=7) 

    start_date=start.strftime('%Y-%m-%d')

    stop_date=stop.strftime('%Y-%m-%d')

    if stop_date>datetime.today().strftime('%Y-%m-%d'):

            stop_date = datetime.today().strftime('%Y-%m-%d')

    req_url=url+start_date+'/'+stop_date+'/'+orgid


    result =requests.get(req_url, headers=headers)

    r.append(result.text)




merged = list(itertools.chain(*r))


回首忆惘然
浏览 156回答 2
2回答

慕的地10843

所以我意识到我只需要改变r.append(result.text)到r.append(result.json())然后一切正常,哈哈

肥皂起泡泡

在列表定义的末尾有一个杂散的引号。取出并运行您的代码后,它对我来说效果很好。它位于两个右括号之间。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python