从字符串列表中的拆分字符串创建字典

时间:2015-12-16 18:09:34

标签: python dictionary split list-comprehension string-split

我觉得这很简单,我接近解决方案,但我已经堆积,无法在互联网上找到建议。
我的列表看起来像:

// Dear modders: I dont recomend that you edit this part but what do I know? Im just the creator of the game
var Name=prompt("What is your name?");
var Class=prompt("What is your class? No Class, Ranger, Recon, Engineer, Soldier, Spy, Agent, Knight");
// This is how the stats are set
Stats=confirm("What are your stats? (You have 20 points to use)");
var BaseSTstat=prompt("What is your streagth stat?");
var BaseDXstat=prompt("What is your dexterity stat?");
var BaseCHstat=prompt("What is your chrisma stat?");
var BaseHTstat=prompt("What is your health stat?");
var BaseINstat=prompt("What is your intlegniince stat?");
var BaseWSstat=prompt("What is your wisdom stat?");
// This is how it checks if the ammont of stat points you have are under 20
// After here I get errors
If (BaseSTstat + BaseDXstat + BaseCHstat + BaseHTstat + BaseINstat + BaseWSstat > 20);{
    confirm("Too many points, try again");
}
// This is how skills are set
Skills=confirm("What are your skills? (You may add 10 points to any skills)");
CombatSkills=confirm("What are your combat skills?");
var  BaseRIskill=prompt("How good are you with rilfes?");
var  BasePIskill=prompt("How good are you with pistols?");
var  BaseHEskill=prompt("How good are you with heavy wepons?");
var  BaseLRskill=prompt("How good are you with long-ranged wepons?");
var  BaseFIskill=prompt("How good are you with your fists?");
var  BaseMEskill=prompt("How good are you with melee wepons?");
ActiveSkills=confirm("What are your active skills?");
var  BaseENskill=prompt("How good are you at engeerings?");
var  BaseCOskill=prompt("How good are you at computers?");
var  BaseLPskill=prompt("How good are you at lock picking?");
PassiveSkills=confirm("What are you passive skills?");
var BaseACskill=prompt("How good is your accuacy");
var BaseDOskill=prompt("How good are you at dodgeing?");
// add the sneaking skill to all the class IF commands
var BaseSNskill=prompt("How good are you at sneaking?");
// add a check for if the player added too many skill points
// Right here I get an error as well VVV
if (BaseRIskill + BasePIskill + BaseHEskill + BaseLRskill + BaseFIskill + BaseMEskill BaseENskill + BaseCOskill + BaseLPskill + BaseACskill + BaseDOskill > 10);{
    confirm("too many points, try again");
}
// This is how skill and stat values are redone as per classes
If (Class="No Class");{
    STstat = BaseSTstat;
    DXstat = BaseDXstat;
    CHstat = BaseCHstat;
    HTstat = BaseHTstat;
    INstat = BaseINstat;
    WSstat = BaseWSstat;
    RIskill = BaseRIskill;
    PIskill = BasePIskill;
    HEskill = BaseHEskill;
    LRskill = BaseLRskill;
    FIskill = BaseFIskill;
    MEskill = BaseMEskill;
    ENskill = BaseENskill;
    COskill = BaseCOskill;
    LPskill = BaseLPskill;
    ACskill = BaseACskill;
    DOskill = BaseDOskill;
}
If (Class="Ranger");{
    STstat = 1 + BaseSTstat;
    DXstat = BaseDXstat;
    CHstat = BaseCHstat;
    HTstat = BaseHTstat;
    INstat = BaseINstat;
    WSstat = BaseWSstat;
    RIskill = BaseRIskill;
    PIskill = BasePIskill;
    HEskill = BaseHEskill;
    LRskill = 1 + BaseLRskill;
    FIskill = BaseFIskill;
    MEskill = BaseMEskill;
    ENskill = BaseENskill;
    COskill = BaseCOskil - 2;
    LPskill = BaseLPskill;
    ACskill = BaseACskill;
    DOskill = BaseDOskill;
}
If (Class="Recon");{
    HTstat = 1 + BaseHTstat;
    PIskill = 1 + BasePIskill;
    STstat = BaseSTstat - 1;
    HEskill = BaseHEskill - 1;
    DXstat = BaseDXstat;
    CHstat = BaseCHstat;
    INstat = BaseINstat;
    WSstat = BaseWSstat;
    RIskill = BaseRIskill;
    LRskill = BaseLRskill;
    FIskill = BaseFIskill;
    MEskill = BaseMEskill;
    ENskill = BaseENskill;
    COskill = BaseCOskill;
    LPskill = BaseLPskill;
    ACskill = BaseACskill;
    DOskill = BaseDOskill;
}
If (Class="Engineer");{
    STstat = BaseSTstat;
    DXstat = BaseDXstat;
    CHstat = BaseCHstat;
    HTstat = BaseHTstat;
    INstat = BaseINstat;
    WSstat = BaseWSstat;
    RIskill = BaseRIskill;
    PIskill = BasePIskill;
    HEskill = BaseHEskill - 2;
    LRskill = BaseLRskill;
    FIskill = BaseFIskill;
    MEskill = BaseMEskill;
    ENskill = 1 + BaseENskill;
    COskill = 1 + BaseCOskill;
    LPskill = BaseLPskill;
    ACskill = BaseACskill;
    DOskill = BaseDOskill;
}
If (Class="Soldier");{
    STstat = BaseSTstat;
    DXstat = BaseDXstat;
    CHstat = BaseCHstat;
    HTstat = BaseHTstat;
    INstat = BaseINstat;
    WSstat = BaseWSstat;
    RIskill = 1 + BaseRIskill;
    PIskill = BasePIskill;
    HEskill = BaseHEskill;
    LRskill = BaseLRskill;
    FIskill = BaseFIskill;
    MEskill = BaseMEskill;
    ENskill = BaseENskill - 1;
    COskill = BaseCOskill - 1;
    LPskill = BaseLPskill;
    ACskill = 1 + BaseACskill;
    DOskill = BaseDOskill;
}
If (Class="Spy");{
    STstat = BaseSTstat;
    DXstat = BaseDXstat;
    CHstat = BaseCHstat;
    HTstat = BaseHTstat;
    INstat = BaseINstat;
    WSstat = BaseWSstat;
    RIskill = BaseRIskill - 1;
    PIskill = 1 + BasePIskill;
    HEskill = BaseHEskill - 1;
    LRskill = BaseLRskill;
    FIskill = BaseFIskill;
    MEskill = BaseMEskill;
    ENskill = BaseENskill;
    COskill = BaseCOskill;
    LPskill = 1 + BaseLPskill;
    ACskill = BaseACskill;
    DOskill = BaseDOskill;
}
If (Class="Agent");{
    STstat = BaseSTstat;
    DXstat = BaseDXstat;
    CHstat = BaseCHstat;
    HTstat = BaseHTstat;
    INstat = BaseINstat;
    WSstat = BaseWSstat;
    RIskill = BaseRIskill - 1;
    PIskill = 1 + BasePIskill;
    HEskill = BaseHEskill - 1;
    LRskill = BaseLRskill;
    FIskill = BaseFIskill;
    MEskill = BaseMEskill;
    ENskill = BaseENskill;
    COskill = 1 + BaseCOskill;
    LPskill = BaseLPskill;
    ACskill = BaseACskill;
    DOskill = BaseDOskill;
}
If (Class="Knight");{
    STstat = 1 + BaseSTstat;
    DXstat = BaseDXstat;
    CHstat = BaseCHstat - 1;
    HTstat = BaseHTstat;
    INstat = BaseINstat;
    WSstat = BaseWSstat;
    RIskill = BaseRIskill;
    PIskill = BasePIskill;
    HEskill = BaseHEskill;
    LRskill = BaseLRskill;
    FIskill = BaseFIskill;
    MEskill = 1 + BaseMEskill;
    ENskill = BaseENskill;
    COskill = BaseCOskill;
    LPskill = BaseLPskill;
    ACskill = BaseACskill;
    DOskill = BaseDOskill - 1;
}
confirm("Is this how you want you charitor to be?");

通常,列表中的每个元素都具有以下形式:my_list = ['name1@1111', 'name2@2222', 'name3@3333'] 我想以漂亮的方式制作字典,它有namex@some_numberkey = namex。我可以通过以下方式实现:

value = some_number

但是我想在一行中完成它,包括列表理解等等。我试过跟随,我想我离我想要的不远。

md = {}
for item in arguments:
    md[item.split('@')[0]] = item.split('@')[1]  

然而,我收到错误:md2 = dict( (k,v) for k,v in item.split('@') for item in arguments ) 。不知道怎么摆脱这个。

1 个答案:

答案 0 :(得分:15)

您实际上并不需要创建元组的额外步骤

>>> my_list = ['name1@1111', 'name2@2222', 'name3@3333']
>>> dict(i.split('@') for i in my_list)
{'name3': '3333', 'name1': '1111', 'name2': '2222'}