添加一个for循环作为键,另一个for循环作为值

时间:2020-07-26 10:18:08

标签: python loops dictionary for-loop

这是我的代码:

   from bs4 import BeautifulSoup
   import requests
   import re

    try:
        for item in range(0, 10):
            print("Page {} of 10".format(item))
            url = 'https://picsum.photos/v2/list?page={}&limit=100'.format(item)
            page = requests.get(url)
            for i in range(0, 100):
                print(page.json()[i]['id'])
    except IndexError:
        print('Finished')

此代码显示:

0 of 10
0
1
10
100
1000

但是我想这样打印:

{"0 of 10":[0, 1, 10, 100, 1000]}

我该怎么做?

2 个答案:

答案 0 :(得分:0)

from bs4 import BeautifulSoup
import requests
import re

data = {}

try:
    for item in range(0, 10):
        print("Page {} of 10".format(item))
        url = 'https://picsum.photos/v2/list?page={}&limit=100'.format(item)
        page = requests.get(url)
        page_data = page.json()
        temp = []
        for i in range(0, 100):
            temp.append(int(page_data[i]['id']))
        data["{} out of 10".format(item)] = temp
except IndexError:
    print('Finished')

print(data)

输出:

{'0 out of 10': [0, 1, 10, 100, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1008, 1009, 101, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1018, 1019, 102, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 103, 1031, 1032, 1033, 1035, 1036, 1037, 1038, 1039, 104, 1040, 1041, 1042, 1043, 1044, 1045, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 106, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 107, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077, 1078, 1079, 108, 1080, 1081, 1082, 1083, 1084, 109, 11, 110, 111, 112, 113, 114, 115, 116], '1 out of 10': [0, 1, 10, 100, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1008, 1009, 101, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1018, 1019, 102, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 103, 1031, 1032, 1033, 1035, 1036, 1037, 1038, 1039, 104, 1040, 1041, 1042, 1043, 1044, 1045, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 106, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 107, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077, 1078, 1079, 108, 1080, 1081, 1082, 1083, 1084, 109, 11, 110, 111, 112, 113, 114, 115, 116], '2 out of 10': [117, 118, 119, 12, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 13, 130, 131, 132, 133, 134, 135, 136, 137, 139, 14, 140, 141, 142, 143, 144, 145, 146, 147, 149, 15, 151, 152, 153, 154, 155, 156, 157, 158, 159, 16, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 17, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 18, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 19, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 2, 20, 200, 201, 202, 203, 204, 206, 208, 209, 21, 210], '3 out of 10': [211, 212, 213, 214, 215, 216, 217, 218, 219, 22, 220, 221, 222, 223, 225, 227, 228, 229, 23, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 24, 240, 241, 242, 243, 244, 247, 248, 249, 25, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 26, 260, 261, 263, 264, 265, 266, 267, 268, 269, 2
...
..

答案 1 :(得分:0)

这是代码的修改版本,可以满足您的需求:

try:
    for item in range(0, 10):
        page_str = "Page {} of 10: ".format(item)
        url = 'https://picsum.photos/v2/list?page={}&limit=100'.format(item)
        page = requests.get(url)
        id_list = [page.json()[i]['id'] for i in range(100)]
        page_str = page_str + ",".join(id_list)
        print(page_str)

except IndexError:
    print('Finished')

输出为:

Page 0 of 10: 0,1,10,100,1000,1001,1002,1003,1004,1005,1006,1008,1009,101,1010,1011,1012,1013,1014,1015,1016,1018,1019,102,1020,1021,1022,1023,1024,1025,1026,1027,1028,1029,103,1031,1032,1033,1035,1036,1037,1038,1039,104,1040,1041,1042,1043,1044,1045,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,106,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,107,1070,1071,1072,1073,1074,1075,1076,1077,1078,1079,108,1080,1081,1082,1083,1084,109,11,110,111,112,113,114,115,116
Page 1 of 10: 0,1,10,100,1000,1001,1002,1003,1004,1005,1006,1008,1009,101,1010,1011,1012,1013,1014,1015,1016,1018,1019,102,1020,1021,1022,1023,1024,1025,1026,1027,1028,1029,103,1031,1032,1033,1035,1036,1037,1038,1039,104,1040,1041,1042,1043,1044,1045,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,106,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,107,1070,1071,1072,1073,1074,1075,1076,1077,1078,1079,108,1080,1081,1082,1083,1084,109,11,110,111,112,113,114,115,116
...