在我的代码结束后,我有一个这样的字典:
{'"WS1"': 1475.9778073075058, '"BRO"': 1554.1437268304624, '"CHA"': 1552.228925324831}
我想要做的是在一个单独的文件teams.txt中找到每个键,其格式如下:
1901,'BRO','LAD'
1901,'CHA','CHW'
1901,'WS1','MIN'
使用年份,即1901年,以及团队,这是字典中每个项目的关键,我想创建一个新的字典,其中键是teams.txt中的第三列,如果年份和团队都是匹配,值是第一个字典中团队的值。
我认为如果我创建一个函数来查找"这将是最简单的。年份和团队,并返回" franch",然后将该功能应用于字典中的每个键。这是我到目前为止,但它给了我KeyError
def franch(year, team_str):
team_str = str(team_str)
with open('teams.txt') as imp_file:
teams = imp_file.readlines()
for team in teams:
(yearID, teamID, franchID) = team.split(',')
yearID = int(yearID)
if yearID == year:
if teamID == team_str:
break
franchID = franchID[1:4]
return franchID
在我想要将此函数应用于字典的其他函数中:
franch_teams={}
for team in teams:
team = team.replace('"', "'")
franch_teams[franch(year, team)] = teams[team]
我想要实现的目标的理想输出如下:
{'"MIN"': 1475.9778073075058, '"LAD"': 1554.1437268304624, '"CHW"': 1552.228925324831}
谢谢!
答案 0 :(得分:0)
不要破坏循环,而是通过它并更改franchID的密钥。
考虑q)num:10
q)num?10 / works
q)-num?10 / dont work
是您的基础词典(在您的示例中为dic
)并使用您的代码,您可以执行以下操作:
{'"WS1"': 1475.9778073075058, '"BRO"': 1554.1437268304624, '"CHA"': 1552.228925324831}
这部分代码
def franch(year,team_str):
team_str=str(team_str)
with open('teams.txt') as imp_file:
teams=imp_file.readlines()
for team in teams:
(yearID,teamID,franchID)=team.split(',')
yearID=int(yearID)
if yearID==year:
if teamID==team_str:
#Found
dic[franchID] = dic[teamID]
del dic[teamID]
通过使用franchID创建一个新条目并删除旧条目,删除'更改'franchID的teamID,输出将是
dic[franchID] = dic[teamID]
del dic[teamID]
正如所料。
如果要保留旧字典,则不必删除密钥。只是做
{'"MIN"': 1475.9778073075058, '"LAD"': 1554.1437268304624, '"CHW"': 1552.228925324831}
你最终会有两本词典。只需声明dic2[franchID] = dic1[teamID]
首先不要获取NameError。
答案 1 :(得分:0)
此代码是否符合您的需求?
我正在对等式进行额外检查,因为代码的不同部分有不同的字符串符号。
def almost_equals(one, two):
one = one.replace('"', '').replace("'", "")
two = two.replace('"', '').replace("'", "")
return one == two
def create_data(year, data, text_content):
""" This function returns new dictionary. """
content = [line.split(',') for line in text_content.split('\n')]
res = {}
for key in data.keys():
for one_list in content:
if year == one_list[0] and almost_equals(key, one_list[1]):
res[one_list[2]] = data[key]
return res
teams_txt = """1901,'BRO','LAD'
1901,'CHA','CHW'
1901,'WS1','MIN'"""
year = '1901'
data = { '"WS1"': 1475.9778073075058, '"BRO"': 1554.1437268304624, '"CHA"': 1552.228925324831 }
result = create_data(year, data, teams_txt)
输出:
{"'CHW'": 1552.228925324831, "'LAD'": 1554.1437268304624, "'MIN'": 1475.9778073075058}
要从文本文件中读取,请使用此功能:
def read_text_file(filename):
with open(filename) as file_object:
result = file_object.read()
return result
teams_txt = read_text_file('teams.txt')
答案 2 :(得分:0)
您可以尝试以下方式:
#!/usr/bin/env python
def clean(_str):
return _str.strip('"').strip("'")
first = {'"WS1"': 1475.9778073075058, '"BRO"': 1554.1437268304624, '"CHA"': 1552.228925324831}
clean_first = dict()
second = dict()
for k,v in first.items():
clean_first[clean(k)] = v
with open("teams.txt", "r") as _file:
lines = _file.readlines()
for line in lines:
_,old,new = line.split(",")
second[new.strip()] = clean_first[clean(old)]
print second
给出了预期的结果:
{"'CHW'": 1552.228925324831, "'LAD'": 1554.1437268304624, "'MIN'": 1475.9778073075058}