计算四分位数 Python

问题描述 投票:0回答:1

我的代码有问题,我似乎无法计算 q1 和 q3。用户输入一个数字列表,它应该为您提供数据摘要,但是,我似乎无法计算第一和第三四分位数。我输入

1,2,3,4,5,6
,得到q1 = 2,q3 = 5,这是正确的,然后我输入
1,2,3,4,5,6,7,8,9,10
,得到q1 = 2,q3 = 7,这是错误的。感谢您的帮助。

def quartiles(user_input):
    user_input.sort()
    length = len(user_input)

    # Initialize Values
    if length % 2 == 0:
        section_one = user_input[:(length // 2)]
        section_three = user_input[(length // 2):]
    else:
        section_one = user_input[:(length // 2)]
        section_three = user_input[(length // 2) + 1:]
    one_length = len(section_one)
    three_length = len(section_three)

    # Calculate Quartiles
    if one_length % 2 == 0:
        q1 = (section_one[one_length // 2 - 1] + section_one[one_length // 2]) / 2
    else:
        q1 = section_one[one_length // 2]

    if three_length % 2 == 0:
        q3 = (section_three[three_length // 2 - 1] + section_three[three_length // 2]) / 2
    else:
        q3 = section_three[three_length // 2]

    return q1, q3

  q1, q3 = quartiles(user_input)
  print("First Quartile:", q1)
  time.sleep(0.05)
  print("Third Quartile:", q3)
python math logic
1个回答
0
投票

在测试您的代码时,我需要尝试确定您的列表是如何输入的,我最有根据的猜测是您输入了一个数字列表,然后拆分输入的值,与此类似。

user_input = list(input("Enter list: ").split(","))    # Comes in as a list of strings

如果确实如此,则后续列表是字符串值列表而不是整数值,因此函数中的行为会有所不同。事实上,当我测试该输入法时,我能够复制您得到的意外行为。

craig@Vera:~/Python_Programs/Quartiles$ python3 Quartile.py 
Enter list: 1,2,3,4,5,6
The list: ['1', '2', '3', '4', '5', '6']
First Quartile: 2
Third Quartile: 5
craig@Vera:~/Python_Programs/Quartiles$ python3 Quartile.py 
Enter list: 1,2,3,4,5,6,7,8,9,10
The list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
First Quartile: 2
Third Quartile: 7

为了便于说明,我在代码的测试版本中添加了一行代码来打印生成的列表。

最有可能的是,一旦根据用户输入最初构建了列表,就需要将其从字符串值转换为整数值。这样,以下是代码的重构版本。

import time

def quartiles(user_input):

    user_input.sort()
    length = len(user_input)

    # Initialize Values
    if length % 2 == 0:
        section_one = user_input[:(length // 2)]
        section_three = user_input[(length // 2):]
    else:
        section_one = user_input[:(length // 2)]
        section_three = user_input[(length // 2) + 1:]
    one_length = len(section_one)
    three_length = len(section_three)

    # Calculate Quartiles
    if one_length % 2 == 0:
        q1 = (section_one[one_length // 2 - 1] + section_one[one_length // 2]) / 2
    else:
        q1 = section_one[one_length // 2]

    if three_length % 2 == 0:
        q3 = (section_three[three_length // 2 - 1] + section_three[three_length // 2]) / 2
    else:
        q3 = section_three[three_length // 2]

    return q1, q3

entry_input = list(input("Enter list: ").split(","))    # Comes in as a list of strings

user_input = []                                         # Initialize the user_input list

for i in entry_input:
    user_input.append(int(i))                           # Converting each list entry to an integer

print("The list:", user_input)

q1, q3 = quartiles(user_input)
print("First Quartile:", q1)
time.sleep(0.05)
print("Third Quartile:", q3)

以下是使用您的两个列表示例的测试输出结果。

craig@Vera:~/Python_Programs/Quartiles$ python3 Quartile.py 
Enter list: 1,2,3,4,5,6
The list: [1, 2, 3, 4, 5, 6]
First Quartile: 2
Third Quartile: 5
craig@Vera:~/Python_Programs/Quartiles$ python3 Quartile.py 
Enter list: 1,2,3,4,5,6,7,8,9,10
The list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
First Quartile: 3
Third Quartile: 8

尝试看看它是否满足您的要求。这样做的要点是要小心如何评估字符串值而不是整数值。

© www.soinside.com 2019 - 2024. All rights reserved.