python,在尝试合并lis中的列表时,所有值都被分解成碎片

2024-04-19 10:58:45 发布

您现在位置: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 loop,但是它们都给了我相同的东西,所以我认为列表本身有问题,但我不确定是什么。请帮忙

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))

Tags: importurl列表datetimedateresultmergedcall
2条回答

所以我意识到我只需要改变

r.附加(结果.text)你知道吗

r.附加(结果.json())

然后一切都正常了哈哈

在列表定义的末尾有一个引号。在取出它并运行代码之后,它对我来说运行得很好。在两个收尾括号之间。你知道吗

相关问题 更多 >