字典中的Python排序文本文件
我有一个文本文件,看起来像这样:字典中的Python排序文本文件,python,file,sorting,dictionary,Python,File,Sorting,Dictionary,我有一个文本文件,看起来像这样: John Graham 2 Marcus Bishop 0 Bob Hamilton 1 ... and like 20 other names. 每个名字出现几次,后面有不同的数字(分数)。 我需要做一个列表,每个名字只显示一次,然后加上该名字的总分。我需要用字典 这就是我所做的,但它只是让一个类似文本文件的列表从一开始就看起来像: dict = {} with open('scores.txt', 'r+') as f: data = f.rea
John Graham 2
Marcus Bishop 0
Bob Hamilton 1
... and like 20 other names.
每个名字出现几次,后面有不同的数字(分数)。
我需要做一个列表,每个名字只显示一次,然后加上该名字的总分。我需要用字典
这就是我所做的,但它只是让一个类似文本文件的列表从一开始就看起来像:
dict = {}
with open('scores.txt', 'r+') as f:
data = f.readlines()
for line in data:
nameScore = line.split()
print (nameScore)
我不知道如何做下一部分。这里有一个使用
defaultdict(int)
的选项:
如果scores.txt
的内容为:
John Graham 2
Marcus Bishop 0
Bob Hamilton 1
John Graham 3
Marcus Bishop 10
它打印:
defaultdict(<type 'int'>,
{'Bob Hamilton': 1, 'John Graham': 5, 'Marcus Bishop': 10})
这里有一个使用
defaultdict(int)
的选项:
如果scores.txt
的内容为:
John Graham 2
Marcus Bishop 0
Bob Hamilton 1
John Graham 3
Marcus Bishop 10
它打印:
defaultdict(<type 'int'>,
{'Bob Hamilton': 1, 'John Graham': 5, 'Marcus Bishop': 10})
我的第一个通行证看起来像:
scores = {} # Not `dict`. Don't reuse builtin names.
with open('scores.txt', 'r') as f: # Not "r+" unless you want to write later
for line in f:
name, score = line.strip().rsplit(' ', 1)
score = int(score)
if name in scores:
scores[name] = scores[name] + score
else:
scores[name] = score
print scores.items()
我并不是这样写的,但我想说得足够清楚,让大家都能理解。我的第一个过程看起来像:
scores = {} # Not `dict`. Don't reuse builtin names.
with open('scores.txt', 'r') as f: # Not "r+" unless you want to write later
for line in f:
name, score = line.strip().rsplit(' ', 1)
score = int(score)
if name in scores:
scores[name] = scores[name] + score
else:
scores[name] = score
print scores.items()
我并不是这样写的,但我想说得足够清楚,让大家都能理解。使用字典获取:
dict = {}
with open('file.txt', 'r+') as f:
data = f.readlines()
for line in data:
nameScore = line.split()
l=len(nameScore)
n=" ".join(nameScore[:l-1])
dict[n] = dict.get(n,0) + int(nameScore[-1])
print dict
输出:
{'Bob Hamilton': 1, 'John Graham': 2, 'Marcus Bishop': 0}
使用字典获取:
dict = {}
with open('file.txt', 'r+') as f:
data = f.readlines()
for line in data:
nameScore = line.split()
l=len(nameScore)
n=" ".join(nameScore[:l-1])
dict[n] = dict.get(n,0) + int(nameScore[-1])
print dict
输出:
{'Bob Hamilton': 1, 'John Graham': 2, 'Marcus Bishop': 0}
我也有过类似的情况。我修改了代码以适应我的具体情况。我有一个映射文件“sort.txt”,它由不同的.pdf文件和数字组成,根据一个网站DOM操作的输出指示我希望它们的顺序。我想把所有这些单独的pdf文件合并成一个单独的pdf文件,但我想保留它们在网站上的顺序。所以我想根据它们在导航菜单中的树位置添加数字
1054 spellchecking.pdf
1055 using-macros-in-the-editor.pdf
1056 binding-macros-with-keyboard-shortcuts.pdf
1057 editing-macros.pdf
1058 etc........
下面是我想出的代码:
import os, sys
# A dict with keys being the old filenames and values being the new filenames
mapping = {}
# Read through the mapping file line-by-line and populate 'mapping'
with open('sort.txt') as mapping_file:
for line in mapping_file:
# Split the line along whitespace
# Note: this fails if your filenames have whitespace
new_name, old_name = line.split()
mapping[old_name] = new_name
# List the files in the current directory
for filename in os.listdir('.'):
root, extension = os.path.splitext(filename)
#rename, put number first to allow for sorting by name and
#then append original filename +e extension
if filename in mapping:
print "yay" #to make coding fun
os.rename(filename, mapping[filename] + filename + extension)
我没有像_full这样的后缀,所以我不需要那个代码。除此之外,它是相同的代码,我从未真正接触过python,因此这对我来说是一次很好的学习经历 我也遇到过类似的情况。我修改了代码以适应我的具体情况。我有一个映射文件“sort.txt”,它由不同的.pdf文件和数字组成,根据一个网站DOM操作的输出指示我希望它们的顺序。我想把所有这些单独的pdf文件合并成一个单独的pdf文件,但我想保留它们在网站上的顺序。所以我想根据它们在导航菜单中的树位置添加数字
1054 spellchecking.pdf
1055 using-macros-in-the-editor.pdf
1056 binding-macros-with-keyboard-shortcuts.pdf
1057 editing-macros.pdf
1058 etc........
下面是我想出的代码:
import os, sys
# A dict with keys being the old filenames and values being the new filenames
mapping = {}
# Read through the mapping file line-by-line and populate 'mapping'
with open('sort.txt') as mapping_file:
for line in mapping_file:
# Split the line along whitespace
# Note: this fails if your filenames have whitespace
new_name, old_name = line.split()
mapping[old_name] = new_name
# List the files in the current directory
for filename in os.listdir('.'):
root, extension = os.path.splitext(filename)
#rename, put number first to allow for sorting by name and
#then append original filename +e extension
if filename in mapping:
print "yay" #to make coding fun
os.rename(filename, mapping[filename] + filename + extension)
我没有像_full这样的后缀,所以我不需要那个代码。除此之外,它是相同的代码,我从未真正接触过python,因此这对我来说是一次很好的学习经历 +1表示不重用内置名称。这是初学者需要注意的一件重要事情!这也可以,但结果需要是一个列表。我也使用split吗?
dict.items()。这是初学者需要注意的一件重要事情!这也可以,但结果需要是一个列表。我也使用split吗?dict.items()必须在那里吗?我想打印一个列表,就像文件从一开始看起来一样。