查找字典时Python字典和循环混淆

查找字典时Python字典和循环混淆,python,django,algorithm,numpy,scipy,Python,Django,Algorithm,Numpy,Scipy,我从下面的代码中得到的结果是错误的: [ '199|4|11|GDSPV', '199|4|11|GDSPV|199|16|82|GDSPV', '199|4|11|GDSPV|199|16|82|GDSPV|205|16|82|GDSPV' ] 我希望结果与fr列表相同,但将电子邮件替换为dictionaryd的值。我完全不明白为什么这不能像预期的那样起作用 fr = [ '7@comp1.COM|4|11|GDSPV', '7@comp1.COM|16|82|GDSPV', '13@c

我从下面的代码中得到的结果是错误的:

[
'199|4|11|GDSPV', 
'199|4|11|GDSPV|199|16|82|GDSPV', 
'199|4|11|GDSPV|199|16|82|GDSPV|205|16|82|GDSPV'
]
我希望结果与fr列表相同,但将电子邮件替换为dictionaryd的值。我完全不明白为什么这不能像预期的那样起作用

fr = [
'7@comp1.COM|4|11|GDSPV',
'7@comp1.COM|16|82|GDSPV',
'13@comp1.COM|16|82|GDSPV'
]

d= {
'7@comp1.COM': '199',
'8@comp4.COM': '200',
'13@comp1.COM': '205'
}


col_list=[]
line_list=[]

for line in fr:
    columns = line.split("|")
    for col in columns:
        if col==columns[0]:
            col_list.append(d[col])
            continue
        col_list.append(col)
        #i = i + 1
    line_list.append("|".join(col_list))
print line_list

开始处理每一行时,需要重置列列表。例如,将
列列表=[]
调用放在列中列的
前面:

开始处理每行时,需要重置列列表。例如,将
列列表=[]
调用放在列中列的
前面:

尝试以下操作:

line_list=[]
for line in fr:
    cols = line.split('|')
    key = cols[0]
    cols[0]=d[key]
    line_list.append('|'.join(cols))

print line_list
试试这个:

line_list=[]
for line in fr:
    cols = line.split('|')
    key = cols[0]
    cols[0]=d[key]
    line_list.append('|'.join(cols))

print line_list

检查这个。。。在sample.py中

fr = [
'7@comp1.COM|4|11|GDSPV',
'7@comp1.COM|16|82|GDSPV',
'13@comp1.COM|16|82|GDSPV'
]

d= {
'7@comp1.COM': '199',
'8@comp4.COM': '200',
'13@comp1.COM': '205'
}

col_list=[]
line_list=[]

for line in fr:
    columns = line.split("|")
    if d.has_key(columns[0]):
        col_list.append(d[columns[0]])
    else:
        col_list.append(columns[0])
    col_list.extend(columns[1:])
    line_list.append("|".join(col_list))

print line_list
输出

D:\>python sample.py
['199|4|11|GDSPV', '199|4|11|GDSPV|199|16|82|GDSPV', '199|4|11|GDSPV|199|16|82|G
DSPV|205|16|82|GDSPV']

检查这个。。。在sample.py中

fr = [
'7@comp1.COM|4|11|GDSPV',
'7@comp1.COM|16|82|GDSPV',
'13@comp1.COM|16|82|GDSPV'
]

d= {
'7@comp1.COM': '199',
'8@comp4.COM': '200',
'13@comp1.COM': '205'
}

col_list=[]
line_list=[]

for line in fr:
    columns = line.split("|")
    if d.has_key(columns[0]):
        col_list.append(d[columns[0]])
    else:
        col_list.append(columns[0])
    col_list.extend(columns[1:])
    line_list.append("|".join(col_list))

print line_list
输出

D:\>python sample.py
['199|4|11|GDSPV', '199|4|11|GDSPV|199|16|82|GDSPV', '199|4|11|GDSPV|199|16|82|G
DSPV|205|16|82|GDSPV']
你就快到了:

# col_list=[] you don't need this
line_list=[]

for line in fr:
    columns = line.split("|")
    looking_for = columns[0] # this is what we need to search
    if looking_for in d:
        # by default, iterating over a dictionary will return keys
        new_line = d[looking_for]+'|'+'|'.join(columns[1:])
        line_list.append(new_line)
print line_list
你就快到了:

# col_list=[] you don't need this
line_list=[]

for line in fr:
    columns = line.split("|")
    looking_for = columns[0] # this is what we need to search
    if looking_for in d:
        # by default, iterating over a dictionary will return keys
        new_line = d[looking_for]+'|'+'|'.join(columns[1:])
        line_list.append(new_line)
print line_list

如果
key
不在
d
中,这将引发
KeyError
异常。如果字典没有用户id,我想用找到的新电子邮件作为key填充字典,整数作为值填充下一个整数206、207、208等。。如何执行此操作?如果
key
不在
d
中,这将引发
keyrerror
异常。如果字典没有用户id,我想用找到的新电子邮件作为key填充字典,用整数作为值填充下一个整数206、207、208等。。我该怎么做?这段代码给出了一个错误
TypeError:line
new_line=d[looking_]+'.'+columns[1://code>Salam mr.Khalid无法连接'str'和'list'对象。我如何向字典中添加一封新的电子邮件,其中包含相应的下一个整数206、207等。。。如果在fr中找到,我还需要跟踪fr中的新电子邮件地址,因此我需要在文件中附加dict d的详细信息。此代码给出一个错误
TypeError:无法将行
new_line=d[查找]+“|”+列[1:]
Salam Khalid先生的'str'和'list'对象连接起来。我如何向字典中添加一封新的电子邮件,其中包含相应的下一个整数206、207等。。。如果在fr中找到,我还需要跟踪fr中的新电子邮件地址,因此我需要在文件中附加dict d的详细信息。