我有一个列表清单
list = [[390, 2645, 760, 2736], [395, 2848, 703, 2950], [399, 2747, 767, 2843], [1083, 2641, 1743, 2732], [1083, 2641, 1743, 2732], [1085, 2845, 1697, 2932], [1085, 2845, 1697, 2932], [1087, 2737, 1741, 2833], [1087, 2737, 1741, 2833], [2055, 2728, 2348, 2831], [2059, 2638, 2351, 2725], [2062, 2840, 2360, 2927], [2065, 2933, 2203, 3033]]
我想创建一个子列表,其中第一个值之间的差异不大于200个示例,换句话说,我想对那些不大于20个差异的值进行分组
像示例399-395 <200进入相同列表list1 1083-399> 200进入其他列表list2 对于其他值类似
desired_list = [[[390, 2645, 760, 2736], [395, 2848, 703, 2950], [399, 2747, 767, 2843]], [[1083, 2641, 1743, 2732], [1083, 2641, 1743, 2732], [1085, 2845, 1697, 2932], [1085, 2845, 1697, 2932], [1087, 2737, 1741, 2833], [1087, 2737, 1741, 2833]], [[2055, 2728, 2348, 2831], [2059, 2638, 2351, 2725], [2062, 2840, 2360, 2927], [2065, 2933, 2203, 3033]]
我尝试过使用groupby,FOR循环,但没有成功
答案 0 :(得分:2)
假设原始列表是有序的
from functools import reduce
def calcList(s,i):
if (i[0]-s[-1][-1][0] ) < 200:
return s[:-1]+[s[-1]+[i]]
else:
return s[:-1]+[s[-1]]+[[i]]
res=reduce(lambda s,i: calcList(s,i) ,lst[1:],[[lst[0]]])
>>>res
[[[390, 2645, 760, 2736], [395, 2848, 703, 2950], [399, 2747, 767, 2843]], [[1083, 2641, 1743, 2732], [1083, 2641, 1743, 2732], [1085, 2845, 1697, 2932], [1085, 2845, 1697, 2932], [1087, 2737, 1741, 2833], [1087, 2737, 1741, 2833]], [[2055, 2728, 2348, 2831], [2059, 2638, 2351, 2725], [2062, 2840, 2360, 2927], [2065, 2933, 2203, 3033]]]
答案 1 :(得分:2)
使用top
,您可以简单地计算出每个元素到另一个元素的距离,并使用“组”选择,如果尚不存在,该选择将添加到新列表中。
// these locations will likely be different on your local.
#include "../cygwin64/usr/include/curl/curl.h"
#include "../cygwin64/usr/include/json/json.h"
size_t write_callback(char *buf, size_t size, size_t nmemb, void* up) {
size_t num_bytes = size*nmemb;
std::string* data = (std::string*) up;
for(int i = 0; i < num_bytes; i++) {
data->push_back(buf[i]);
}
return num_bytes;
}
CURL* init_curl(struct curl_slist *headers, std::string* chunk) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) chunk);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
return curl;
}
bool curl_get(struct curl_slist *headers, const std::string& url,
Json::Value* json_res) {
// this is where my data should be written to
std::string data;
CURL* curl = init_curl(headers, &data);
CURLcode res;
bool success = true;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cout << "Could not perform get for " << url << std::endl;
std::cout << curl_easy_strerror(res) << std::endl;
success = false;
} else {
Json::Value json_data;
Json::CharReaderBuilder json_reader;
std::istringstream stream_data(data);
std::string errs;
if(Json::parseFromStream(json_reader, stream_data, &json_data, &errs)) {
std::cout << "successfully parsed JSON data for: " << url << std::endl;
*json_res = json_data;
} else {
std::cout << "failed to parse JSON data for: " << url << std::endl;
std::cout << errs << std::endl;
std::cout << json_data << std::endl;
std::cout << "finished failing" << std::endl;
success = false;
}
}
} else {
success = false;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return success;
}
int main(int argc, char** argv) {
curl_global_init(CURL_GLOBAL_ALL);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
while (true) {
Json::Value response;
if (curl_get(headers, "https://api.robinhood.com/quotes/?symbols=AMZN", &response)) {
std::cout << response << std::endl;
} else {
// reaches here sometimes because response (my data) is null
std::cout << "failed to get last trade price" << std::endl;
}
}
curl_global_cleanup();
return 0;
}
答案 2 :(得分:1)
尝试一下:
shareReplay()