我从循环中得到三个lists
元组。我想知道如何将这些附加到dict
。所以第一件事。我有一个function
,它在循环中将tuples
附加到lists
,然后返回这些列表(其中3个)。所以像这样]
return list1, list2, list3
我正在这样做,将
list
的lists
的tuples
更改为dict
。首先,我将返回的lists
附加到getted_data
的for loop
列表中(我需要此循环来填充其他内容)之后,我使该元组列表的列表变平并仅获得list
的tuples
。并按代码所示制作了一个special dict
-我需要像这样的dict
进行下一步处理。但有个问题!当我将这些lists
附加到getted_data
时,我从tuples
中丢失了一些lists
-是因为键值相同吗?我不知道...无论如何-这些值在我print(data1, data2, data3)
时存在,但在我打印(getted_data)时不存在。所以我想尝试另一种方法来使dict
像从返回的lists
的代码中一样您能帮我吗?
informations = [] table_informations = [] res = defaultdict(list) getted_data = [] counter2 = 0 x = 0 items = [] curr_dict = None f = open('profile_urls.csv', 'r') dataReader = csv.reader(f) for row in dataReader: counter2 += 1 row = str(row).replace("['", "").replace("']", "") print(row) if counter2 > 50: with open("agents.txt", "r") as f: x = (random_line(f)).rstrip() print("") print("[INFO] Changing User-Agent") my_session.headers.update({'User-Agent':x}) print(f"[INFO] Changed User-Agent to: {my_session.headers}") print("") data1, data2, data3 = get_soup(row) print(data3) # data here getted_data.append(data1) getted_data.append(data2) getted_data.append(data3) print(getted_data) # Wh00t? Some data not here.... flat_list = [item for sublist in getted_data for item in sublist] # Its strange but I need this part below ! d = dict((y, x) for x, y in flat_list) for key in d.keys(): new_key = d[key] new_value = key if key else 'None' if new_key == 'url': curr_dict = {} items.append(curr_dict) curr_dict[new_key] = new_value
print(data3)
('ID', '39965930'), ('Birth date', '3/12/1954'), ('Status', 'Compliant'), ('Race', 'White'), ('Gender', 'Male'), ('Age', '58'), ('Height', '6′ 0″ (1.83 m)'), ('Weight', '170 lb (77 kg)'), ('Eyes', 'Hazel'), ('Hair', 'Brown'), ('Address', '203 YOUNG ROAD'), ('City', 'REMLAP'), ('State', 'Alabama'), ('Zip', '35133'), ('Address is Temporary', 'No'), ('Address Last Verified', '3/26/2012'), ('Registration Date', '5/07/2002'), ('Aliases', 'N/A'), ('Released', '20000804')]
print(getted_data)
'https://thumbs-25535606.400x800.jpg': 'url', '39965930': 'com ID', '3/12/1954': 'Birth date', '58': 'Age', '170 lb (77 kg)': 'Weight', 'Hazel': 'Eyes', '203 YOUNG ROAD': 'Address', 'REMLAP': 'City', '35133': 'Zip', '3/26/2012': 'Address Last Verified', '5/07/2002': 'Registration Date', '20000804': 'Released'}
我从循环中的函数中获取三个元组列表。我想知道如何将这些附加到字典上。所以第一件事。我有一个函数,该函数在循环中将元组追加到列表中,然后...
print(data3)