0

我有巨大的 if elif else 语句可以正常工作......它们看起来非常不合时宜,我即将增加一倍我需要的数量。基本上,我正在接受用户输入图表的 x 值数量,通过数据分类器发送它,然后绘制图表。为简单起见,我只输入 x=1(最小值)和 x=6(最大值)值,但我想添加“多少个 y?” 但我担心这会过于庞大和混乱。

有什么办法可以浓缩这个?

代码:

howManyX = int(raw_input('Input number of x-values for this graph: '))

if howManyX == 1:
    x1 = int(raw_input("Input column number for x1-value: "))
    x2 = 1
    x3 = 1
    x4 = 1
    x5 = 1
    x6 = 1
elif howManyX == 6:
    x1 = int(raw_input("Input column number for x1-value: "))
    x2 = int(raw_input("Input column number for x2-value: "))
    x3 = int(raw_input("Input column number for x3-value: "))
    x4 = int(raw_input("Input column number for x4-value: "))
    x5 = int(raw_input("Input column number for x5-value: "))
    x6 = int(raw_input("Input column number for x6-value: "))

下面这部分是相同的,除了 x1 是 x(n) 到 6。代码都是正确的,我只需要将 x1 更改为 x(1-6)

if x1 == 0:
    x1a = sheet.col_values(x1)
    x1b = [i for i in x1a if i != '']
    x1c = [i for i in x1b if type(i) == float][1:]
    x1Axis = [datetime.strptime(str(int(d)), '%Y%m%d') for d in x1c]
elif x1 == 31:
    x1a = sheet.col_values(x1)
    clear()
    print "\n\n\n1: Top"
    print "2. Bottom"
    is_valid = 0
    while not is_valid :
        try :
            choice = int ( raw_input('Enter your choice [1 or 2] : ') )
            is_valid = 1 ## set it to 1 to validate input and to terminate the while..not loop
        except ValueError, e :
            print ("'%s' is not a valid integer." % e.args[0].split(": ")[1])
    if choice == 1:
        x1Axis = filter(None, [i for i, j in zip(x1a, x1a[1:] + ['']) if j != ''])[1:]
    elif choice == 2:
        x1Axis = filter(None, [i for i, j in zip(x1a, x1a[1:] + ['']) if j == ''])[2:]
    else:
        print ("Invalid number. Try again...")
else:
    x1a = sheet.col_values(x1)
    x1Axis = filter(None, [i for i, j in zip(x1a, x1a[1:] + ['']) if j == ''])[2:]

看起来很多,那可能只是我的代码很乱。我认为它只需要一个简单的 for 循环,但我不知所措,所以我不知道从哪里开始。一切运作良好。只是看起来很乱

4

5 回答 5

1

输入部分可以重写,只需让它保留一个包含六个列号的列表,而不是六个单独的变量:

howManyX = int(raw_input('Input number of x-values for this graph: '))

x_col_nums = [1] * 6
for i in range(howManyX):
    x_col_nums[i] = int(raw_input("Input column number for x%d-value: " % (i + 1)))

然后简单地将整个第二个块放入这个 for 循环中:

for x1 in x_col_nums:
    # all your second block of code goes here
    # you might want to change the variable name x1 to just be x, which might
    # make it clearer

要保存变量x(n)Axis以便以后可以使用它们,请保留这些变量的列表,方法是放置以下行:

xaxes = []

在循环之前,并添加该行

    xaxes.append(x1Axis)

在循环。之后,您可以访问以前的x1Axis, x2Axis... 作为xaxes[0],xaxes[1]等等。

于 2013-09-25T15:20:17.500 回答
0

您可以让用户输入他们感兴趣的值(比如说以 a 的形式dict)并对其进行评估。这样他们就可以指定第 7 列和第 12 列,而不必担心其他max(vals)列...如果您愿意,您可以使用它来获取引用的最高列...(或者定义您的键-> 值对关系)

from ast import literal_eval

# User enters something like: { 1:3, 5: 2}
vals = literal_eval(raw_input()) 
if not isinstance(vals, dict):
    # Uh oh... maybe do something here?
# Then use (to get columns, or 1 or whatever if not entered)
col_val3 = vals.get(3, 1)
于 2013-09-25T15:38:13.017 回答
0

您可能只需将每个xN变量转换为某个数组中的等效变量。例如xs......虽然你真的应该用它来命名它,而只是x在这种情况下。

第一部分将类似于:

howManyX = int(raw_input('Input number of x-values for this graph: '))
xs = [1]*howManyX
for i in range(howManyX):
    xs[i] = int(raw_input("Input column number for x%i-value: " % (i+1)))

当然,另一个块也需要更新变量。如果您使用不同xs 之间的变量,您可以这样做xAxes[i],或者如果它们只是 single 的本地变量,则x完全删除该数字。

于 2013-09-25T15:26:07.183 回答
0
howManyX = int(raw_input('Input number of x-values for this graph: '))

if howManyX == 1:
    x1 = int(raw_input("Input column number for x1-value: "))
    x2 = x3 = x4 = x5 = = x6 = 1

elif howManyX == 6:
    x1, x2, x3, x4, x5, x6 = (
        int(raw_input("Input column number for x{}-value: ".format(i)))
        for i in range(1, 7)
    )
于 2013-09-25T15:19:53.370 回答
-1

您使用了太多变量而不是数组。您可以使用 for 循环和数组x

x = [None]*howManyX
for i in range(howManyX):
    x[i] = int(raw_input("Input column number for x%d-value: " % (i+1)))

在特定情况下,您可以设置回类变量(使用内置setattr),或者在简单脚本的情况下,您甚至可以设置全局变量(如果您真的需要):

scope = globals()   # in case of simple script
scope = self.__dict__
for i, val in enumerate(x):
   scope["x%d" % (i+1)] = val
于 2013-09-25T15:22:39.697 回答