gpt4 book ai didi

python - 如何比较两列中的字符串并将一列中的字符串大小写替换为另一列?

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:44 26 4
gpt4 key购买 nike

我有两栏“句子”和“更新”。我想将 Url 末尾的更新列中的每个单词与相应的句子单词大小写进行匹配,并将其替换为句子中单词的大小写。

我不知道如何进行此比较,感谢任何帮助。实际数据有 43k 行,具有不同的 Url。

示例代码:

import pandas as pd

dict1 = {'Updates': ['The new abc.com/Line','Its a abc.com/bright and abc.com/Sunny Day','abc.com/smartphone have taken our the abc.com/WORLD','abc.com/GLOBAL Warming is abc.com/Reaching its abc.com/peak'],
'Sentences': ['The new line','Its a bright and sunny day','Smartphone have taken our the World','GLOBAL Warming is reaching its Peak ']
}

df = pd.DataFrame(dict1)

当前营业额:

Sentences           Updates
The new line The new abc.com/Line

Its a bright and sunny day Its a abc.com/bright and abc.com/Sunny Day

Smartphone have taken our the World abc.com/smartphone have taken our the abc.com/WORLD

GLOBAL Warming is reaching its Peak abc.com/GLOBAL Warming is abc.com/Reaching its abc.com/peak
Expected O/P:

Sentences Updates
The new line The new abc.com/line

Its a bright and sunny day Its a abc.com/bright and abc.com/sunny day

Smartphone have taken our the World abc.com/Smartphone have taken our the abc.com/World

GLOBAL Warming is reaching its Peak abc.com/GLOBAL Warming is abc.com/reaching its abc.com/Peak

最佳答案

使用re

代码:

import re

dict1 = {
'Sentences': [
'The new line',
'Its a bright and sunny day',
'Smartphone have taken our the World',
'GLOBAL Warming is reaching its Peak '
],
'Updates': [
'The new abc.com/Line',
'Its a abc.com/bright and abc.com/Sunny Day',
'abc.com/smartphone have taken our the abc.com/WORLD',
'abc.com/GLOBAL Warming is abc.com/Reaching its abc.com/peak'
]
}
for sentence, update in zip(dict1['Sentences'], dict1['Updates']):
urls = [x.split("/")[-1] for x in update.split() if "/" in x]
for url in urls:
update = (re.sub(url, re.search(url, sentence, re.IGNORECASE).group(), update, flags=re.IGNORECASE))

print(f"{sentence}\t{update}")

输出:

The new line    The new abc.com/line
Its a bright and sunny day Its a abc.com/bright and abc.com/sunny Day
Smartphone have taken our the World abc.com/Smartphone have taken our the abc.com/World
GLOBAL Warming is reaching its Peak abc.com/GLOBAL Warming is abc.com/reaching its abc.com/Peak

关于python - 如何比较两列中的字符串并将一列中的字符串大小写替换为另一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58403985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com