如何使用 Python 方法和实例将文件中的数据存储到字典中?
How to use Python Methods and Instances to store data in a dictionary from a file?
我是 python 的新手,不太喜欢面向对象的编程。我正在尝试找出实现以下目标的最佳方法:
我想读取这样的文件:
来自文件的输入:
Name,semester,point
abc,5,3.5
cba,2,3.5
我想将它存储在一个字典中,例如名称作为主键以及与之相关的其他两个值。
如
输出可能:
abc:'5','3.5'
我需要在其他函数中使用我的存储值进行一些计算。所以只要我存储它们并且可以以某种方式访问它们就没问题。它可以是列表、数组列表、字典或任何看起来不错的解决方案。
根据我的阅读,我想使用方法和实例而不是 class 和对象。我认为根据我的理解,这听起来是正确的。
请提出实现这一目标的最佳方法,因为我已经检查过类似问题的 Whosebug,而且我只找到了面向对象的解决方案(我想避免)。
我认为您不需要为此创建 class,因为结果可以在一行中实现:
Python3 解决方案:
data = {a:b for a, *b in [i.strip('\n').split(',') for i in open('filename.txt')][1:]}
Python2解法:
data = {a[0]:a[1:] for a in [i.strip('\n').split(',') for i in open('filename.txt')][1:]}
但是,如果您正在寻找面向对象的解决方案:
class Data:
def __init__(self, filename):
self.data = {} #will store data in empty dictionary
self.filename = filename
def set_data(self):
for i in self.get_data():
self.data[i[0]] = i[1:]
def get_data(self):
return [i.strip('\n').split(',') for i in open(filename)]
file_data = Data("the_file.txt")
file_data.set_data()
print(file_data.data) #will print the full dictionary
One line solution :
print({tuple(i[:1]):i[1:] for i in [i.strip().split(',') for i in open('g.txt')][1:]})
Detailed solution:
final_dict={}
with open("g.txt") as f:
for i in [i.strip().split(',') for i in f][1:]:
final_dict[tuple(i[:1])]=i[1:]
print(final_dict)
我使用了上面@Ajax1234的例子。似乎是一个很好的解决方案和示例。
据我了解,实例和 Classes 之间的混淆以及它们与方法的关系
方法:
- 作为属性附加到 class 的函数是方法。
Class:
- 它们充当实例创建者工厂。
- 它们的属性提供=行为数据和功能
- 这些属性由从这些 Classes 生成的实例继承。
实例:
- 这些表示程序域中的具体项目。
- 他们从 class
继承了他们的属性
- 它们的属性记录的数据因对象而异。
- 内存中一次只能有一个给定方法的实例。
例如在数据处理模型中:程序和记录
- 实例就像记录
- Classes就像处理那些数据记录的软件
Class,方法和实例用例子解释:
如果我解释有误,请纠正我。
import sys
filename = sys.argv[1]
class Data: # class
def __init__(self, filename):
self.data = {} #will store data in empty dictionary
self.filename = filename
# funtion in class as attribute can be a METHOD
def get_data(self):
return [i.strip('\n').split(',') for i in open(filename)]
# funtion in class as attribute can be a METHOD
def set_data(self):
for i in self.get_data():
self.data[i[0]] = i[1:]
# Instances are derived from Class
# file_data is an Instance created from the class Data
file_data = Data(filename)
# method called through Instance
file_data.set_data()
# will print the full dictionary
print(file_data.data)
参考:Mark Lutz 的学习 Python
希望这能解释清楚。
我是 python 的新手,不太喜欢面向对象的编程。我正在尝试找出实现以下目标的最佳方法:
我想读取这样的文件: 来自文件的输入:
Name,semester,point
abc,5,3.5
cba,2,3.5
我想将它存储在一个字典中,例如名称作为主键以及与之相关的其他两个值。 如
输出可能:
abc:'5','3.5'
我需要在其他函数中使用我的存储值进行一些计算。所以只要我存储它们并且可以以某种方式访问它们就没问题。它可以是列表、数组列表、字典或任何看起来不错的解决方案。
根据我的阅读,我想使用方法和实例而不是 class 和对象。我认为根据我的理解,这听起来是正确的。
请提出实现这一目标的最佳方法,因为我已经检查过类似问题的 Whosebug,而且我只找到了面向对象的解决方案(我想避免)。
我认为您不需要为此创建 class,因为结果可以在一行中实现:
Python3 解决方案:
data = {a:b for a, *b in [i.strip('\n').split(',') for i in open('filename.txt')][1:]}
Python2解法:
data = {a[0]:a[1:] for a in [i.strip('\n').split(',') for i in open('filename.txt')][1:]}
但是,如果您正在寻找面向对象的解决方案:
class Data:
def __init__(self, filename):
self.data = {} #will store data in empty dictionary
self.filename = filename
def set_data(self):
for i in self.get_data():
self.data[i[0]] = i[1:]
def get_data(self):
return [i.strip('\n').split(',') for i in open(filename)]
file_data = Data("the_file.txt")
file_data.set_data()
print(file_data.data) #will print the full dictionary
One line solution :
print({tuple(i[:1]):i[1:] for i in [i.strip().split(',') for i in open('g.txt')][1:]})
Detailed solution:
final_dict={}
with open("g.txt") as f:
for i in [i.strip().split(',') for i in f][1:]:
final_dict[tuple(i[:1])]=i[1:]
print(final_dict)
我使用了上面@Ajax1234的例子。似乎是一个很好的解决方案和示例。
据我了解,实例和 Classes 之间的混淆以及它们与方法的关系
方法:
- 作为属性附加到 class 的函数是方法。
Class:
- 它们充当实例创建者工厂。
- 它们的属性提供=行为数据和功能
- 这些属性由从这些 Classes 生成的实例继承。
实例:
- 这些表示程序域中的具体项目。
- 他们从 class 继承了他们的属性
- 它们的属性记录的数据因对象而异。
- 内存中一次只能有一个给定方法的实例。
例如在数据处理模型中:程序和记录
- 实例就像记录
- Classes就像处理那些数据记录的软件
Class,方法和实例用例子解释:
如果我解释有误,请纠正我。
import sys
filename = sys.argv[1]
class Data: # class
def __init__(self, filename):
self.data = {} #will store data in empty dictionary
self.filename = filename
# funtion in class as attribute can be a METHOD
def get_data(self):
return [i.strip('\n').split(',') for i in open(filename)]
# funtion in class as attribute can be a METHOD
def set_data(self):
for i in self.get_data():
self.data[i[0]] = i[1:]
# Instances are derived from Class
# file_data is an Instance created from the class Data
file_data = Data(filename)
# method called through Instance
file_data.set_data()
# will print the full dictionary
print(file_data.data)
参考:Mark Lutz 的学习 Python
希望这能解释清楚。