如何修复具有相同键但不同值的字典错误

huangapple go评论79阅读模式
英文:

How to fix dictionary error with same keys having different values

问题

I have a text file with lines having two columns (separated by a Tab). I want to open this file and have as output a dictionary, store the 1st column as keys, then the 2nd column as values. But I am difficulty handling keys that have different values (like Camiguin below):

Sandingan	Loon, Bohol
Pangangan	Calape, Bohol
Limasawa	Limasawa, Southern Leyte
Catalat	San Vicente, Palawan
Camiguin	Catarman, Camiguin
Camiguin	Guinsiliban, Camiguin
Camiguin	Mahinog, Camiguin
Camiguin	Mambajao, Camiguin
Camiguin	Sagay, Camiguin
Rasa	Narra, Palawan

The code I used is:

filesDir = "C:\Users\user\Desktop"

fhand = open(filesDir+"\input.txt")
fout = open(filesDir+"output.txt","w")

d = dict()

for line in fhand:
x = line.rstrip("\n")
x = x.split("\t")

if x[0] not in d:
d[x[0]] = x[1]

else:
d[x[0]] += x[1]
continue

print(d)

The result I am getting is this (note the result for Camiguin which simply concatenated the values together. I probably would need to use a list, but I cannot figure how to):

{'Limasawa': 'Limasawa, Southern Leyte', 'Rasa': 'Narra, Palawan', 'Catalat': 'San Vicente, Palawan', 'Camiguin': 'Catarman, CamiguinGuinsiliban, CamiguinMahinog, CamiguinMambajao, CamiguinSagay, Camiguin', 'Pangangan': 'Calape, Bohol', 'Sandingan': 'Loon, Bohol'}

EDIT: My desired output should be something like this:

{'Limasawa': 'Limasawa, Southern Leyte', 'Rasa': 'Narra, Palawan', 'Catalat': 'San Vicente, Palawan', 'Camiguin': ['Catarman, Camiguin','Guinsiliban, Camiguin','Mahinog, Camiguin','Mambajao, Camiguin','Sagay, Camiguin'], 'Pangangan': 'Calape, Bohol', 'Sandingan': 'Loon, Bohol'}

英文:

I have a text file with lines having two columns (separated by a Tab). I want to open this file and have as output a dictionary, store the 1st column as keys, then the 2nd column as values. But I am difficulty handling keys that have different values (like Camiguin below):

<pre>
Sandingan Loon, Bohol
Pangangan Calape, Bohol
Limasawa Limasawa, Southern Leyte
Catalat San Vicente, Palawan
Camiguin Catarman, Camiguin
Camiguin Guinsiliban, Camiguin
Camiguin Mahinog, Camiguin
Camiguin Mambajao, Camiguin
Camiguin Sagay, Camiguin
Rasa Narra, Palawan
</pre>

The code I used is:

filesDir = &quot;C:\Users\user\Desktop&quot;

fhand = open(filesDir+&quot;\input.txt&quot;)
fout = open(filesDir+&quot;output.txt&quot;,&quot;w&quot;)

d = dict()

for line in fhand:
  x = line.rstrip(&quot;\n&quot;)
  x = x.split(&quot;\t&quot;)

  if x[0] not in d:
    d[x[0]] = x[1]
	
  else:
    d[x[0]] += x[1]
    continue

print(d)

The result I am getting is this (note the result for Camiguin which simply concatenated the values together. I probably would need to use a list, but I cannot figure how to):

{&#39;Limasawa&#39;: &#39;Limasawa, Southern Leyte&#39;, &#39;Rasa&#39;: &#39;Narra, Palawan&#39;, &#39;Catalat&#39;: &#39;San Vicente, Palawan&#39;, &#39;Camiguin&#39;: &#39;Catarman, CamiguinGuinsiliban, CamiguinMahinog, CamiguinMambajao, CamiguinSagay, Camiguin&#39;, &#39;Pangangan&#39;: &#39;Calape, Bohol&#39;, &#39;Sandingan&#39;: &#39;Loon, Bohol&#39;}

EDIT: My desired output should be something like this:

{&#39;Limasawa&#39;: &#39;Limasawa, Southern Leyte&#39;, &#39;Rasa&#39;: &#39;Narra, Palawan&#39;, &#39;Catalat&#39;: &#39;San Vicente, Palawan&#39;, &#39;Camiguin&#39;: [&#39;Catarman, Camiguin&#39;,&#39;Guinsiliban, Camiguin&#39;,&#39;Mahinog, Camiguin&#39;,&#39;Mambajao, Camiguin&#39;,&#39;Sagay, Camiguin&#39;], &#39;Pangangan&#39;: &#39;Calape, Bohol&#39;, &#39;Sandingan&#39;: &#39;Loon, Bohol&#39;}

答案1

得分: 2

使用defaultdict而不是使用字典,这样你可以始终添加:

from collections import defaultdict
d = defaultdict(list)

for line in fhand:
  x = line.rstrip("\n")
  x = x.split("\t")
  d[x[0]].append(x[1])

print(d)
英文:

Instead of using a dictionary, use a defaultdict, then you can always append

from collections import defaultdict
d = defaultdict(list)

for line in fhand:
  x = line.rstrip(&quot;\n&quot;)
  x = x.split(&quot;\t&quot;)
  d[x[0]].append(x[1])

print(d)

答案2

得分: 2

Using dict.setdefault

Ex:

res = {}
with open(filename) as infile:
    for line in infile:
        key, value = line.strip().split("\t")    # 获取行中的键值对
        res.setdefault(key, []).append(value)    # 使用列表追加值
print(res)

Output:

{'Camiguin': ['Catarman, Camiguin',
              'Guinsiliban, Camiguin',
              'Mahinog, Camiguin',
              'Mambajao, Camiguin',
              'Sagay, Camiguin'],
 'Catalat San': ['Vicente, Palawan'],
 'Limasawa': ['Limasawa, Southern Leyte'],
 'Pangangan': ['Calape, Bohol'],
 'Rasa': ['Narra, Palawan'],
 'Sandingan': ['Loon, Bohol']}
英文:

Using dict.setdefault

Ex:

res = {}
with open(filename) as infile:
    for line in infile:
        key, value = line.strip().split(&quot;\t&quot;)    #Get key-value from line
        res.setdefault(key, []).append(value)    #Use list to append values. 
print(res)

Output:

{&#39;Camiguin&#39;: [&#39;Catarman, Camiguin&#39;,
              &#39;Guinsiliban, Camiguin&#39;,
              &#39;Mahinog, Camiguin&#39;,
              &#39;Mambajao, Camiguin&#39;,
              &#39;Sagay, Camiguin&#39;],
 &#39;Catalat San&#39;: [&#39;Vicente, Palawan&#39;],
 &#39;Limasawa&#39;: [&#39;Limasawa, Southern Leyte&#39;],
 &#39;Pangangan&#39;: [&#39;Calape, Bohol&#39;],
 &#39;Rasa&#39;: [&#39;Narra, Palawan&#39;],
 &#39;Sandingan&#39;: [&#39;Loon, Bohol&#39;]}

答案3

得分: 0

在你的 else 循环下,你可以检查分配给该键的值的类型。如果类型是字符串,将其替换为包含当前字符串和新值的列表。如果值已经是一个列表,则将新值附加到它。

else:
    if type(d[x]) == 'str':
        d[x] = [d[x], x[1]]
    elif type(d[x]) == 'list':
        d[x].append(x[1])
英文:

Under your else loop, you can check the type of the value assigned to that key. If the type is a string, replace it with a list containing the current string and the new value. If the value is already a list, append the new value to it.

  else:
    if type(d[x]) == &#39;str&#39;:
      d[x] = [d[x], x[1]]
    elif type(d[x] == &#39;list&#39;):
      d[x].append(x[1])

huangapple
  • 本文由 发表于 2020年1月3日 21:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定