简体   繁体   English

如何从字典中提取以仅打印某些变量python

[英]How to extract from dictionaries to only print certain variables python

I had a tsv file like such我有一个像这样的 tsv 文件

Name    School   Course         
Nicole  UVA      Biology
Jenna   GWU      CS

from there, I only want to print the Name and the Course from that dictionary.从那里,我只想打印该字典中的名称和课程。 How would I go about this?我该怎么办?

The code below is how I put the original TSV file into the dictionary above.下面的代码是我如何将原始 TSV 文件放入上面的字典中。

import csv
data = csv.reader(open('data.tsv'),delimiter='\t')
fields = data.next()
for row in data:
    item = dict(zip(fields, row))
    print item 

So now I got a dictionary like such:所以现在我得到了这样的字典:

{'Name':'Nicole.', 'School':'UVA.','Course':'Biology'}
{'Name':'Jenna.', 'School':'GWU','Course':'CS'}
{'Name':'Shan', 'School':'Columbia','Course':'Astronomy'}
{'Name':'BILL', 'School':'UMD.','Course':'Algebra'}

I only want to print the Name and the Course from that dictionary.我只想打印该字典中的名称和课程。 How would I go about this?我该怎么办?

I want to add code so that I'm only printing我想添加代码以便我只打印

{'Name':'Jenna.','Course':'CS'}
{'Name':'Shan','Course':'Astronomy'}
{'Name':'BILL','Course':'Algebra'}

Please guide.请指导。 Thank You谢谢你

Just delete the key in the loop 只需删除循环中的密钥

for row in data:
    item = dict(zip(fields, row))
    del item['School']
    print item 

The easiest way is to just remove the item['School'] entry before printing 最简单的方法是在打印之前删除item['School']条目

for row in data:
    item = dict(zip(fields, row))
    del item['School']
    print item 

But this only works if you know exactly what the dictionary looks like, it has no other entries that you don't want, and it already has a School entry. 但这仅在您完全知道词典的外观,没有其他不需要的条目并且已经有School条目的情况下才有效。 I would instead recommend that you build a new dictionary out of the old one, only keeping the Name and Course entries 相反,我建议您在旧词典的基础上再建一个新词典,只保留NameCourse条目

for row in data:
    item = dict(zip(fields, row))
    item = {k, v for k, v in item.items() if k in ('Name', 'Course')}
    print item 

Maybe use a DictReader in the first place, and rebuild the row-dict only if key matches a pre-defined list: 也许首先使用DictReader ,并且仅在键与预定义列表匹配时才重新DictReader行字典:

import csv

keep = {"Name","Course"}
data = csv.DictReader(open('data.tsv'),delimiter='\t')

for row in data:
    row = {k:v for k,v in row.items() if k in keep}
    print(row)

result: 结果:

{'Course': 'Biology', 'Name': 'Nicole'}
{'Course': 'CS', 'Name': 'Jenna'}

根据这里的答案: 过滤python字典中的项,其中键包含特定的字符串

print {k:v for k,v in item.iteritems() if "Name" in k or "Course" in k}

You're better off using a library designed for these kinds of tasks (Pandas). 最好使用专为此类任务(熊猫)设计的库。 A dictionary is great for storing key-value pairs, but it looks like you have spreadsheet-like tabular data, so you should choose a storage type that better reflects the data at hand. 字典非常适​​合存储键值对,但是看起来您具有类似电子表格的表格数据,因此您应该选择一种存储类型,以更好地反映手头的数据。 You could simply do the following: 您可以简单地执行以下操作:

import pandas as pd

df = pd.read_csv('myFile.csv', sep = '\t')

print df[['Name','Course']]

You'll find that as you start doing more complicated tasks, it's better to use well written libraries than to cludge something together 您会发现,当您开始执行更复杂的任务时,最好使用编写良好的库而不是将某些东西组合在一起

replace the your " print(item) " line with the below line.用下面的行替换你的“print(item)”行。

print(dict(filter(lambda e: e[0]!='School', item)))

OUTPUT:输出:

{'Name':'Jenna.','Course':'CS'}
{'Name':'Shan','Course':'Astronomy'}
{'Name':'BILL','Course':'Algebra'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM