组合单个元组以形成 Python 中的列表
Combine individual tuple to form a list in Python
我正在学习编码,但遇到困难。我试图定义一个函数,该函数从文件中读取数据并将其转换为单个列表,列表中的每个项目都是一个具有整数、浮点数和字符的元组。
def reader(a_file):
file = open(a_file, "r")
listy = file.readlines()
# print(listy)
# print(type(listy))
for lines in listy:
# print(lines)
a_list = list(lines.split())
# print(a_list)
short_list = []
return_list = []
for each in range(0, len(a_list)):
# print(a_list[each])
if a_list[each].isdigit() :
each_int = int(a_list[each])
# print(each_int)
short_list.append(each_int)
elif "." in a_list[each]:
each_float = float(a_list[each])
short_list.append(each_float)
else:
short_list.append(a_list[each])
tuply = tuple(short_list)
print(tuply)
# return_list.append(tuply)
# print(return_list)
# print(return_list)
如果我使用
调用函数
print(reader("sample.cs1301"))
我能够得到单独的元组
我无法将单个元组组合到列表中。
以下是输入文件 sample.cs1301
的内容
1 assignment_1 85 100 0.25
2 test_1 90 100 0.25
3 exam_1 95 100 0.5
我能够得到如下的单个元组
(1, 'assignment_1', 85, 100, 0.25)
(2, 'test_1', 90, 100, 0.25)
(3, 'exam_1', 95, 100, 0.5)
我在循环中感到困惑。我试着到处搜索,但不知何故它对我不起作用。
修复原代码
def reader(a_file):
file = open(a_file, "r")
listy = file.readlines()
file.close() # Always close if not using with
# print(listy)
# print(type(listy))
return_list = []
for lines in listy:
# print(lines)
a_list = list(lines.split())
# print(a_list)
short_list = []
for each in range(0, len(a_list)):
# print(a_list[each])
if a_list[each].isdigit() :
each_int = int(a_list[each])
# print(each_int)
short_list.append(each_int)
elif "." in a_list[each]:
each_float = float(a_list[each])
short_list.append(each_float)
else:
short_list.append(a_list[each])
tuply = tuple(short_list)
print(tuply)
return_list.append(tuply) # Need to append tuple to results list
print(return_list)
return return_list # Return the resulting list
reader('sample.cs1301')
改进建议
- 使用try/except检查根据类型转换数字
- 逐行迭代文件
- 去掉行尾的换行符
- 文件后缀是 txt 所以 Window/Mac 系统知道文件类型即 'sample.cs1301.txt'
改进代码
def reader(filenm):
with open(filenm, "r") as f:
result = []
for line in f:
a_list = line.rstrip().split() # rstrip to remove new line at end of line
values = []
for value in a_list:
try:
values.append(int(value))
except ValueError:
try: # Not int
values.append(float(value))
except ValueError: # Not float
values.append(value)
result.append(tuple(values)) # Appending values as tuple
return result
print(reader('sample.cs1301.txt')) # Use txt as file suffix
输出
[(1, 'assignment_1', 85, 100, 0.25),
(2, 'test_1', 90, 100, 0.25),
(3, 'exam_1', 95, 100, 0.5)]
我正在学习编码,但遇到困难。我试图定义一个函数,该函数从文件中读取数据并将其转换为单个列表,列表中的每个项目都是一个具有整数、浮点数和字符的元组。
def reader(a_file):
file = open(a_file, "r")
listy = file.readlines()
# print(listy)
# print(type(listy))
for lines in listy:
# print(lines)
a_list = list(lines.split())
# print(a_list)
short_list = []
return_list = []
for each in range(0, len(a_list)):
# print(a_list[each])
if a_list[each].isdigit() :
each_int = int(a_list[each])
# print(each_int)
short_list.append(each_int)
elif "." in a_list[each]:
each_float = float(a_list[each])
short_list.append(each_float)
else:
short_list.append(a_list[each])
tuply = tuple(short_list)
print(tuply)
# return_list.append(tuply)
# print(return_list)
# print(return_list)
如果我使用
调用函数print(reader("sample.cs1301"))
我能够得到单独的元组 我无法将单个元组组合到列表中。 以下是输入文件 sample.cs1301
的内容1 assignment_1 85 100 0.25
2 test_1 90 100 0.25
3 exam_1 95 100 0.5
我能够得到如下的单个元组
(1, 'assignment_1', 85, 100, 0.25)
(2, 'test_1', 90, 100, 0.25)
(3, 'exam_1', 95, 100, 0.5)
我在循环中感到困惑。我试着到处搜索,但不知何故它对我不起作用。
修复原代码
def reader(a_file):
file = open(a_file, "r")
listy = file.readlines()
file.close() # Always close if not using with
# print(listy)
# print(type(listy))
return_list = []
for lines in listy:
# print(lines)
a_list = list(lines.split())
# print(a_list)
short_list = []
for each in range(0, len(a_list)):
# print(a_list[each])
if a_list[each].isdigit() :
each_int = int(a_list[each])
# print(each_int)
short_list.append(each_int)
elif "." in a_list[each]:
each_float = float(a_list[each])
short_list.append(each_float)
else:
short_list.append(a_list[each])
tuply = tuple(short_list)
print(tuply)
return_list.append(tuply) # Need to append tuple to results list
print(return_list)
return return_list # Return the resulting list
reader('sample.cs1301')
改进建议
- 使用try/except检查根据类型转换数字
- 逐行迭代文件
- 去掉行尾的换行符
- 文件后缀是 txt 所以 Window/Mac 系统知道文件类型即 'sample.cs1301.txt'
改进代码
def reader(filenm):
with open(filenm, "r") as f:
result = []
for line in f:
a_list = line.rstrip().split() # rstrip to remove new line at end of line
values = []
for value in a_list:
try:
values.append(int(value))
except ValueError:
try: # Not int
values.append(float(value))
except ValueError: # Not float
values.append(value)
result.append(tuple(values)) # Appending values as tuple
return result
print(reader('sample.cs1301.txt')) # Use txt as file suffix
输出
[(1, 'assignment_1', 85, 100, 0.25),
(2, 'test_1', 90, 100, 0.25),
(3, 'exam_1', 95, 100, 0.5)]