Skip to main content
 首页 » 编程设计

python之使用 Python 按顺序选择行

2024年12月31日11pengyingh

我正在寻找一种解决方案来订购我的循环选择。 My problem is that when is select the rows, these aren't sorted.所以我想找到所有匹配的行并处理它们,然后根据下一个关键字找到下一个匹配的行。另一方面,我首先要找到所有匹配的行,然后是基于下一个关键字的所有匹配行。最后我想指出,我不想将结果附加到不同的变量中,因为我有很多关键字。

这是我的问题的一个简单示例:

key_word=['one', 'two'] 
list=['one1', 'one2', 'two1', 'two2', 'two3', 'one3'] 
 
for i in list: 
    for word in key_word: 
        if (word in i): 
            print (i)  

我的输出:

one1 
one2 
two1 
two2 
two3 
one3 

我想要的输出:

one1 
one2 
one3 
two1 
two2 
two3 

请您参考如下方法:

只需改变循环的顺序。

key_word=['one', 'two'] 
lst=['one1', 'one2', 'two1', 'two2', 'two3', 'one3'] 
 
 
for word in key_word: 
    for i in lst: 
        if (word in i): 
            print (i)