某些元组用于字典

问题描述 投票:-1回答:1

我从循环中得到三个lists元组。我想知道如何将这些附加到dict。所以第一件事。我有一个function,它在循环中将tuples附加到lists,然后返回这些列表(其中3个)。所以像这样]

return list1, list2, list3

我正在这样做,将listliststuples更改为dict。首先,我将返回的lists附加到getted_datafor loop列表中(我需要此循环来填充其他内容)之后,我使该元组列表的列表变平并仅获得listtuples。并按代码所示制作了一个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'}

我从循环中的函数中获取三个元组列表。我想知道如何将这些附加到字典上。所以第一件事。我有一个函数,该函数在循环中将元组追加到列表中,然后...

python python-3.x list append tuples
1个回答
0
投票
print(data3)
© www.soinside.com 2019 - 2024. All rights reserved.