gpt4 book ai didi

python - 如果字符串包含重复的单词,则仅保留第一个单词

转载 作者:行者123 更新时间:2023-12-04 08:31:51 25 4
gpt4 key购买 nike

customer_name                                               ANDY
number_of_product_variants 2
number_of_channels 1
number_of_discount_codes 1
order_count 1
order_name #1100,#1100
discount_code Christmas2020, Christmas2020
channel Instagram, Instagram
product_variant Avengers Set A, Avengers Set B
仅当字符串包含重复项时,我才想删除重复的单词。
预期输出:
customer_name                                                ANDY
number_of_product_variants 2
number_of_channels 1
number_of_discount_codes 1
order_count 1
order_name #1100
discount_code Christmas2020
channel Instagram
product_variant Avengers Set A, Avengers Set B
我试过的代码:
def unique_string(l):
ulist = []
[ulist.append(x) for x in l if x not in ulist]
return ulist

customer_df['channel_2']=customer_df['channel']
customer_df['channel_2'].apply(unique_string)
仅将以下代码用于 channel列返回:
0                                   [S, e, a, r, c, h, ,]
1 [P, a, i, d, , A, s, :, S, o, c, l]
2 [P, a, i, d, , A, s, :, S, o, c, l, ,]
3 [U, n, k, o, w, ,]
```

最佳答案

您可以使用 set, 分割值如果有多个值,则顺序不重要。
如果订单很重要,请使用 dict 和 .keys() :

customer_df = pd.DataFrame({"channel_2":['Instagram, Instagram',
'Instagram, Instagram1, Instagram, Instagram2']})

f1 = lambda x: ', '.join(set(y for y in x.split(', ')))
f2 = lambda x: ', '.join(dict.fromkeys(y for y in x.split(', ')).keys())

customer_df['channel_2_1'] = customer_df['channel_2'].apply(f1)
customer_df['channel_2_2'] = customer_df['channel_2'].apply(f2)
print (customer_df)
channel_2 \
0 Instagram, Instagram
1 Instagram, Instagram1, Instagram, Instagram2

channel_2_1 channel_2_2
0 Instagram Instagram
1 Instagram2, Instagram1, Instagram Instagram, Instagram1, Instagram2

关于python - 如果字符串包含重复的单词,则仅保留第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64984285/

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