【发布时间】:2019-03-10 08:59:21
【问题描述】:
注意:我是 Python 的初学者,所以请多多包涵!
编辑:我已经修复了错误,但我需要帮助解决下面的问题!
我的问题是:
1) 如果我想将最小值和最常见的单词/数字放在表格上,我如何索引最小值/最常见的单词并将其提取并放置在我的正确位置桌子?
说明
以下代码应该使用函数转置给定的嵌套列表 A
def rows2cols(A):,
然后遍历该列表,对于每一列,我检查它是否具有数值或不使用
def isnumericlist(A):。
如果列表确实有数值,我将字符串转换为浮点数并从该列表中找到最小值和最常见的单词/数字。
代码如下:
A = [['OrderDate', 'Region', 'Rep', 'Item', 'Units', 'Unit Price'],['4-Jul-2014', 'East', 'Richard', 'Pen Set', '62', '4.99'], ['12-Jul-2014', 'East', 'Nick', 'Binder', '29', '1.99'], ['21-Jul-2014', 'Central', 'Morgan', 'Pen Set', '55', '12.49'], ['29-Jul-2014', 'East', 'Susan', 'Binder', '81', '19.99'],['7-Aug-2014', 'Central', 'Matthew', 'Pen Set', '42', '23.95'], ['15-Aug-2014', 'East', 'Richard', 'Pencil', '35', '4.99'], ['24-Aug-2014', 'West', 'James', 'Desk', '3', '275']]
minVal = []
maxVal = []
hist = []
average = []
stanDev = []
headers = A[0] #this sets the variable "headers" as the first row
rows = A[1:] #skips the first row
def rows2cols(A):
if len(A) == 0:
return [] #this covers the base case of having an empty csv file
res = [[] for x in headers] # would create a list of empty lists
for line in A:
for col in range(len(line)):
res[col].append(line[col])
return res
def convertstringtofloats(A):
res = []
for x in A:
res.append(float(x))
return res
def isnumericlist(A):
for x in A:
try:
numeric = float(x)
except:
return False
return True
def getMin(A):
res = B[0] #first column AFTER you transpose the nested list
for x in A:
if x < res:
res = x
return res
def most_common(A):
counts = {}
for x in A:
counts[tuple(x)] = counts.get(tuple(x), 0) + 1
max = -1
maxKey = ""
for key,value in counts.items():
if max < value:
max = value
maxKey = key
return maxKey
def notnumeric(A):
return "n/a"
cols = rows2cols(rows)
for col in range(len(headers)):
if isnumericlist(cols[col]):
B = convertstringtofloats(cols[col])
minVal.append(getMin(B))
maxVal.append(getMax(B))
average.append(getAvg(B))
stanDev.append(getSD(B))
else:
notnumeric(col)
mode.append(most_common(cols[col]))
tablevalues = [minVal, maxVal, average, stanDev, mode]
下面是我生成表格的代码,以及我希望结果如何的示例表格!
def print_table(table):
longest_cols = [
(max([len(str(row[i])) for row in table]) + 0) for i in range(len(table[0]))
]
row_format = "|".join([" {:>" + str(longest_col) + "} " for longest_col in longest_cols])
first = True
for row in table:
print(row_format.format(*row))
if first:
print((sum(longest_cols) + (len(table[0]) - 0) * 3) * "-")
first = False
table = [
["Columns:", "Min", "Max", "Avg", "Std. Dev.", "Most Common Word"],
["OrderDate", "n/a", "n/a", "n/a", "n/a", "John"],
["Region", 3.3, 6.29, 4.888, 1.333, 4.911],
["Rep", 1.3, 3.2, 1.888, 0.333, 1.9],
["Item", 1.3, 3.2, 1.888, 0.333, 1.9],
["Units","n/a", "n/a", "n/a", "n/a", "John"],
["Unit Price","n/a", "n/a", "n/a", "n/a", "John"]
]
print_table(table)
【问题讨论】:
-
你能把你的问题分解成哪一行出现错误吗?您是否希望读者首先阅读您的整个代码,然后逐行找出错误可能来自哪里?此外,
most_common(cols)只是调用该函数,但您没有将返回值存储在任何变量中 -
您的错误发生在哪里对我来说并不明显。您可以编辑问题以包含异常的完整回溯吗?你已经
-
@Bazingaa,@Blckknght;对不起!我在尝试修复我的错误时创建了这个问题,最终我自己修复了它。我只需要代码下方的问题帮助。
-
@Bazingaa 如何存储返回值?此外,当我将代码放入 PythonTutor 网站时,“most_common(cols)”不会遍历每个 cols 中的每个元素。它似乎会跳过每个列表,直到它到达带有数值的列表。
标签: python indexing max minimum