简体   繁体   English

将 function 应用于循环索引的系统?

[英]system that applies the function to the index on loop?

I'm attempting to create a system where pre-defined variables are run through a function and placed in lists based on the value of the variable.我正在尝试创建一个系统,其中预定义的变量通过 function 运行并根据变量的值放置在列表中。

global i, st1, st2, st3, classa
A = []
B = []
st1 = '13'
st2 = '3'
st3 = '17'
list = [st1, st2, st3]

i = 0
while (i<2):
    i = i + 1
    a = list[i]


def listselect():
    sn = 'student name-'
    f = 'score-'
    a = ()
    if a in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}:
        A.append(sn+f+a)

    elif a in {'10', '11', '12', '13', '14', '15', '16', '17', '18', '19'}:
        B.append(sn+f+a)

    print(A)
    print(B)


listselect()

However it only outputs the empty lists, without the values being added.但是它只输出空列表,不添加值。

[]
[]

How would I be able to run this program on a while loop until all variables have been sorted?在对所有变量进行排序之前,我如何才能在 while 循环中运行该程序?

Sorry if anything is wrong with the post formatting, i'm new to the website.抱歉,如果帖子格式有任何问题,我是该网站的新手。

Does this do what you want?这会做你想要的吗?

st1 = '13'
st2 = '3'
st3 = '17'
A , B = [], []
a = list ([st1, st2, st3])

def listselect (a) :
    sn = 'student name-'
    f = 'score-'
    for element in a :
        if element in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') :
            A.append (sn+f+element)
        else : B.append (sn+f+element)  

listselect (a)
print (A, '\n', B)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单嵌套的for循环中的索引函数不 - Index function in a simple nested for loop not 将两个给定函数应用于Python中外部参数的函数 - Function that applies two given functions to an outside argument in Python 如何使用for循环修复函数而不迭代。 Python推荐系统 - How to fix function with for loop not iterating. Recommendation system in Python 为什么这个python函数在for循环中运行时跳过索引1到3而不迭代索引2 - why this python function skips index 1 to 3 without iterating index 2 when running in a for loop Python:改进for循环的性能,内部函数调用仅取决于循环索引 - Python: improving performance of a for loop with function call inside that depends on loop index only 如何在 Python 中创建适用于类的多值函数? - How do I create a multi-value function that applies to a class in Python? Python。 调用 function 将突变应用于 object。 突变不适用于调用上下文 - Python. Calling a function that applies a mutation to an object. Mutation does not apply in the calling context 如何从主代码继承一个函数并在模块内执行它。 (这也适用于变量) - How to inherit a function from the main code and execute it inside the module. (this also applies to variables) 获取嵌套的for循环的索引 - Get index of nested for loop 使用索引进行for循环打印 - Making a for loop print with an index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM