Изменение кодов, не используя много циклов для чтения больших наборов данных - PullRequest
0 голосов
/ 26 октября 2018

Есть ли простой способ сделать словарь для множества разных файлов json?Я хотел бы создать словарь, но также учитывать время обработки, например, если у меня есть 1 тысяча файлов json.

Мой текущий код:

#My current approach loop each file and then create a dictionary for the particular file. Since its looping so it will take much time if the files are too huge
# I would like to consider the huge file process time in making the dictionary and searching for another alternative to my current codes.

file = [[[json],[json],[json],[json],[json]]] #this is just an example of 5 json files for explanation and seeking help purpose. In real aim is to focus in 1000++ json files.

full_dic = {}
for i, f in enumerate(file): # this loop each file but what if I have thousands so it will take much time
    dictionary = {}
    for ii, fs in enumerate(f): # this loop each file sentence, even worse when have thousands f(file)
        **Here I will create my dictionary by reading the json file contents, this part does not matter because it depends on my json files, so can be ignored**
        **Finally, dictionary created like this**  
        dictionary[ii] = something
    full_dic[i] = dictionary

есть даже многоболее лучший способ или время, сокращающее способ сделать это, имея большие наборы данных?

1 Ответ

0 голосов
/ 26 октября 2018

Это может быть одна строка (при условии, что у вас есть список путей к файлам json в file списке):

my_dict = {i: json.load(open(file[i])) for i in range(len(file))}

Хотя, если вы должны убедиться, что эти файлы 1000 json помещаются в вашу память в первую очередь,Также желательно не называть вашу переменную file, так как это одна из значений по умолчанию __builtin__ в python.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...