将循环迭代合并为一行,并且不进行匹配处理 [英] combining for loop iterations into a single line and no match handling

查看:46
本文介绍了将循环迭代合并为一行,并且不进行匹配处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个非常基本的问题,但希望有人能帮忙.

Probably a very basic question but hoping someone can help out.

我有以下内容:

query = ['whole regular milk', 'gatorade is better', 'whole almond chocolate 
milk', 'chocolate milk']

types = ['whole', 'regular', 'chocolate' ]

new_list = []

for i in query:
    for k in types:
        regex_concat = r"\b" + k + r"\b"
        new_regex =  re.search(regex_concat,i)
        if (str(new_regex)) != 'None':
            print((new_regex.group()))
        else:
            print('no match')

谁的输出生成以下内容:

who's output generates the following:

whole
regular
no match
no match
no match
no match
whole
no match
chocolate
no match
no match
chocolate

我的理想输出是:

whole | regular
Blank
whole | chocolate
chocolate

问题:

我认为我应该能够使用以下内容将输出合并为一行:

I think I should be able to use the following to combine the output into a single line:

print((new_regex.group()), end= "|", flush=True)

这会给我:

whole|regular|no match
no match
no match
no match
whole|no match
chocolate|no match
no match
chocolate|

我似乎无法弄清楚如何得出上述请求的输出.

I can't seem to figure out how to net out to the requested output above.

一些附加说明-

查询列表将从pd DataFrame编译.从那里,我想使用所需的输出(将其转换为list> series)映射回pd DataFrame.这就是为什么我希望空白行仍然存在的原因,因为最终输出应如下所示:

The query list will be compiled from a pd DataFrame. From there, I would like to use the desired output, which I'd convert to a list > series, to map back to the pd DataFrame. This is why I'd like the blank row to still be present because the final output should look like this:

Query                         Type
whole regular milk            whole | regular
gatorade is better             
whole almond chocolate milk   whole | choclate  
chocolate milk                chocolate

推荐答案

如果您的输入已经是datarframe,则可以在dataframe级别上完成所有操作:

If your input is already a datarframe, you can do the whole thing in the dataframe level:

import re

query = ['whole regular milk', 'gatorade is better',
         'whole almond chocolate milk', 'chocolate milk', 'wholes']

types = [{'type': t, 'regex': re.compile(r'\b{}\b'.format(t))}
         for t in ['whole', 'regular', 'chocolate']]

df = pd.DataFrame({'Query': query})

def check(q):
    return ' | '.join(type_info['type'] for type_info in types
                      if type_info['regex'].findall(q))

df['Type'] = df['Query'].apply(check)

print(df)

#                           Query                Type
#  0           whole regular milk     whole | regular
#  1           gatorade is better                   
#  2  whole almond chocolate milk   whole | chocolate
#  3               chocolate milk           chocolate
#  4                       wholes                            

这篇关于将循环迭代合并为一行,并且不进行匹配处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆