在列中查找元组列表的第一个元素的第一个单词?

2024-05-12 22:41:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个datafreme:

import pandas as pd

test = {'text': [
    ('tom-mark', 'tom', 'tom is a good guy.'),
    ('Nick X','nick', 'Is that Nick?')
]}, {'text': [
    ('juli', 'juli', 'Tom likes juli so much.'),
    ('tony', 'tony', 'Steve and Tony listen in as well.')
]}

我想在每个元组的第一个元素中找到第一个单词(即tom、Nick、juli、tony)

我尝试了以下代码,但它无法处理tom mark中的“-”

    name = t[0].lower()
    name = name.split()
    name = name[0]

但是,有些元组有两个单词作为第一个元素。如何找到每个元组的第一个单词


Tags: textnameimport元素pandasas单词nick
2条回答

您可以使用dataframe并使用函数映射text列的值以获得第一个名称,然后从该特定列的列表中创建列表

在函数内部,使用正则表达式仅从该列表中的所有元组中提取名字,并返回名字列表

import pandas as pd
import re


def get_first(x):
    return list(map(lambda tup: re.match(r'\w+', tup[0])[0].lower(), x))

test = {'text': [
    ('tom-mark', 'tom', 'tom is a good guy.'),
    ('Nick X','nick', 'Is that Nick?')
]}, {'text': [
    ('juli', 'juli', 'Tom likes juli so much.'),
    ('tony', 'tony', 'Steve and Tony listen in as well.')
]}

data = sum(pd.DataFrame(test).applymap(get_first)['text'].tolist(), [])

print(data)

这样做是否有帮助:

import re

test = {'text': [
    ('tom-mark', 'tom', 'tom is a good guy.'),
    ('Nick X','nick', 'Is that Nick?'),
    ('juli', 'juli', 'Tom likes juli so much.'),
    ('tony', 'tony', 'Steve and Tony listen in as well.')]
}

first_names = []

for names in test['text']:
    name = re.match(r'\w+', names[0])
    first_names.append(name[0].lower())


print(first_names)

['tom', 'nick', 'juli', 'tony']

相关问题 更多 >