Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过原始输入添加缺少的字典键/值_Python_List_Dictionary_List Comprehension_Raw Input - Fatal编程技术网

Python 通过原始输入添加缺少的字典键/值

Python 通过原始输入添加缺少的字典键/值,python,list,dictionary,list-comprehension,raw-input,Python,List,Dictionary,List Comprehension,Raw Input,我正在处理许多随机、混合布局/列顺序的文件。我有一套数据库表的模板,其中有“ACCOUNT_name”、“client”和“tin”,我希望这些文件可以在其中排序。我已经创建了一个字典,其中可能包含在其他文件中作为键的标题/列名,以及作为值的集合标题名。因此,例如,如果我想查看某个给定文件中“account number”列的位置,我会键入header_dict[“account number]”。 这将给我模板中相应的列“ACCOUNT\u name”。这很好用…我还添加了另一个功能。我不必键

我正在处理许多随机、混合布局/列顺序的文件。我有一套数据库表的模板,其中有“ACCOUNT_name”、“client”和“tin”,我希望这些文件可以在其中排序。我已经创建了一个字典,其中可能包含在其他文件中作为键的标题/列名,以及作为值的集合标题名。因此,例如,如果我想查看某个给定文件中“account number”列的位置,我会键入header_dict[“account number]”。 这将给我模板中相应的列“ACCOUNT\u name”。这很好用…我还添加了另一个功能。我不必键入“帐号”…而是制作了一个列表,按键查找每个值

我刚刚用“fileLayout”列表创建的这个列表实际上是将我给定的文件头转换成我想要的名称:
['ACCOUNT\u name','client']
这让生活变得容易多了……我知道我想查找“帐户名”或“客户”。接下来,我运行一个函数“getLayout”,它返回我正在搜索的所需列的索引…因此,如果我想查看所需列“ACCOUNT\u name”在文件中的位置,我只需运行如下调用的函数

import collections
header_dict = {'account number':'ACCOUNT_name','accountID':'ACCOUNT_name','name':'client','first name':'client','tax id':'tin'}
#header_dict = collections.defaultdict(lambda: 'tin') # attempted use of defaultdict...destroys my dictionary
given_header = ['account number','name','tax id']#,'tax identification number']#,'social security number'
#given_header = ['account number','name','tax identification number']...non working header layout
fileLayout = [header_dict[ting] for ting in given_header if ting] #create if else..if ting exists, add to list...else if not in list, add to dictionary

def getLayout(ting):
    global given_header
    global fileLayout
    return given_header[fileLayout.index(ting)]

print getLayout('ACCOUNT_name')
print getLayout('client')
print getLayout('tin')
rows = zip((getLayout('ACCOUNT_name'),getLayout('client'),getLayout('tin')))
print rows
现在,我可以轻松地按订单打印列…使用:

getLayout('ACCOUNT_name')  
上面的代码给了我
[('accountnumber'),('name'),('tax id')]
,这正是我想要的

但是如果有一个我不习惯的新头球呢??让我们使用上面相同的示例代码,但将列表“给定_头”更改为:

rows = zip((getLayout('ACCOUNT_name'),getLayout('client'),getLayout('tin')))
print rows
我很确定地得到了关键错误,KeyError:“tax identification number”我知道我可以使用defaultdict,但当我尝试将其与设置值“tin”一起使用时,我最终覆盖了我的整个字典。。。我最终想做的是

我想在我的列表理解中创建一个else,允许我在不存在标准输入字典条目的情况下输入它们。换句话说,由于“税务识别号”不作为键存在,因此将其作为一个键添加到我的dict中,并通过原始输入给它值“tin”。有人做过或尝试过类似的事情吗?有什么想法吗?如果你有什么建议,我洗耳恭听。我在这个问题上苦苦挣扎

我想这样做的方式在列表中

given_header = ['account number','name','tax identification number']

您需要和
if
else
for的
的另一侧<代码>[foo if bar else baz for bar in iterable]
。在
for
仅用于筛选条件之后,会出现一个隐式的“else skip the item”。您需要和
if
else
for的另一侧<代码>[foo if bar else baz for bar in iterable]。在的
仅用于筛选条件之后,会出现一个隐式的“else skip the item”。
fileLayout = [header_dict[ting] for ting in given_header if ting else raw_input('add missing key value pair to dictionary')] # or do something of the sort.