迭代并追加列表

时间:2019-08-16 13:30:40

标签: python list dictionary

我在Python中有一个列表列表,我想用字典中的键号对其进行索引。

AST = ['AGENDA'],[“女性状态”,“ TREATIES”,   “签名,加入,批准”,   “预订和声明”,   '性别歧视'],['选举成员',   'BIOGRAPHY'],[[妇女权利],   “妇女状态”,   'PERIODIC REPORTS']“

如果我遍历列表,则会像往常一样获得单独的列表,但是当我要将它们添加到字典中时。 Python仅显示最后一个列表。 enter code here

0: [["WOMEN'S RIGHTS", "WOMEN'S STATUS", 'PERIODIC REPORTS']],
1: [["WOMEN'S RIGHTS", "WOMEN'S STATUS", 'PERIODIC REPORTS']],
2: [["WOMEN'S RIGHTS", "WOMEN'S STATUS", 'PERIODIC REPORTS']],
3: [["WOMEN'S RIGHTS", "WOMEN'S STATUS", 'PERIODIC REPORTS']]}

如何在Python中实现这种结构以使其具有这种结构?

1: ['AGENDA']
2: ["WOMEN'S STATUS", 'TREATIES', 'SIGNATURES, ACCESSIONS,
    RATIFICATIONS', 'RESERVATIONS AND DECLARATIONS', 'GENDER 
    DISCRIMINATION']
3: ['ELECTION OF MEMBERS', 'BIOGRAPHY']
4: ["WOMEN'S RIGHTS", "WOMEN'S STATUS", 'PERIODIC REPORTS']
for t in AST:
    print (t)
new_dict = {new_list: t for new_list in range(len(AST))} 
print(new_dict)

第三结果仅显示最后一个列表。何

3 个答案:

答案 0 :(得分:7)

您可以在字典理解中使用enumerate

>>> {idx: sublist for idx, sublist in enumerate(AST, 1)}
{1: ['AGENDA'],
 2: ["WOMEN'S STATUS", 'TREATIES', 'SIGNATURES, ACCESSIONS, RATIFICATIONS', 'RESERVATIONS AND DECLARATIONS', 'GENDER DISCRIMINATION'],
 3: ['ELECTION OF MEMBERS', 'BIOGRAPHY'],
 4: ["WOMEN'S RIGHTS", "WOMEN'S STATUS", 'PERIODIC REPORTS']}

答案 1 :(得分:2)

只需执行此操作-

var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(
    new KeyVaultClient.AuthenticationCallback(
        azureServiceTokenProvider.KeyVaultTokenCallback));
config.AddAzureKeyVault(builtConfig["Azure:Auth:Vault"], keyVaultClient, 
    new DefaultKeyVaultSecretManager());

答案 2 :(得分:0)

import requests
import lxml.html

# Get Auth & Login URL
get_login = requests.get('https://paypal.com/cgi-bin/webscr?cmd=_login-run')
get_login_response = get_login.text.encode('utf-8') #printing html source
get_login_html = lxml.html.fromstring(get_login_response) #printing <Element html at 0x7f19cb242ec0>
auth = get_login_html.xpath("//input[@name='auth']/@value") #printing [<InputElement 7fb0971e9f18 name='auth' type='hidden'>]
login_url = get_login_html.xpath("//form[@name='login_form']/@action")

# Post Login
payload = {
'login_cmd':'',
'login_params':'',
'login_email':'websec@spam4.me',
'login_password':'jancok666',
'auth':auth[0],
'submit.x':'Log In',
'form_charset':'UTF-8',
'browser_name':'Firefox',
'browser_version':'18',
'browser_version_full':'18.0'
}

#proxy
proxy = {
 'https': '1.2.3.4:1234',
 'http': '1.2.3.4:1234'
}
post_login = requests.post(login_url[0], data=payload, proxies=proxy)
post_login_response = post_login.text.encode('utf-8')

print post_login_response