【问题标题】:Duplicate removal after spliting pandas拆分熊猫后重复删除
【发布时间】:2021-08-05 22:20:16
【问题描述】:

我想在使用以下代码拆分后作为键值发送:

for row in all_data2.itertuples():
    for ele in row.vertical_concat.split(" "):
        if 3 < len(ele) < 25:
            ele_new = str(ele) + str(i) + str(ele)
            print(ele_new)
            ActionChains(driver).move_to_element(send).send_keys(ele_new).perform()
            ActionChains(driver).move_to_element(send).send_keys(Keys.ENTER).perform()

我正在转换这些值并将它们发送为:apple::apple(在这种情况下是必需的格式)。

有没有办法可以删除重复的单词,因为我的值可能为:

apple::apple
apple::apple

我想发送一次。

非常感谢您的任何建议。

【问题讨论】:

  • 重复单词是什么意思?你想要的输出是 ['apple::apple'] 而不是 ['apple::apple' ,'apple::apple' ] ?
  • @BlackMath 确切地说,只发送一次该值作为键

标签: python pandas split


【解决方案1】:

基本上,您可以将循环的每个输出存储在一个列表中(例如already_printed)。 使用set(),我们可以管理该列表中的最终重复项。

然后,你可以这样做:

already_printed = set()
for row in all_data2.itertuples():
    for ele in row.vertical_concat.split(" "):
        if ele not in already_printed:
           if 3 < len(ele) < 25:
             ele_new = str(ele) + str(i) + str(ele)
             print(ele_new)
        else:
             already_printed.add(ele)

【讨论】:

  • 我认为这将维持秩序pd.unique(my_list).tolist()
  • @AnuragDabas 使用“列表理解”+ enumerate() 也将保持顺序。
  • @BlackMath 我需要维护两个“for”并遍历行。我尝试使用列表,但是当将它们作为键发送时,我将无法将它们添加到另一个之下,它只是在发送第一个值后阻塞。所以迭代需要对行而不是列表中的元素进行
猜你喜欢
  • 2016-01-30
  • 2013-10-28
  • 1970-01-01
  • 2023-03-31
  • 2020-06-23
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多