欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中列表和字符串常用的數(shù)據(jù)去重方法總結(jié)

 更新時(shí)間:2023年11月23日 11:32:30   作者:蟲(chóng)無(wú)涯  
關(guān)于數(shù)據(jù)去重,咱們這里簡(jiǎn)單理解下,就是刪除掉重復(fù)的數(shù)據(jù),應(yīng)用的場(chǎng)景比如某些產(chǎn)品產(chǎn)生的大數(shù)據(jù),有很多重復(fù)的數(shù)據(jù),為了不影響分析結(jié)果,我們可能需要對(duì)這些數(shù)據(jù)進(jìn)行去重,所以本文給大家總結(jié)了Python中列表和字符串常用的數(shù)據(jù)去重方法,需要的朋友可以參考下

1 關(guān)于數(shù)據(jù)去重

  • 關(guān)于數(shù)據(jù)去重,咱們這里簡(jiǎn)單理解下,就是刪除掉重復(fù)的數(shù)據(jù);
  • 應(yīng)用的場(chǎng)景比如某些產(chǎn)品產(chǎn)生的大數(shù)據(jù),有很多重復(fù)的數(shù)據(jù),為了不影響分析結(jié)果,我們可能需要對(duì)這些數(shù)據(jù)進(jìn)行去重,刪除重復(fù)的數(shù)據(jù),提高分析效率等等。

2 字符串去重

2.1 for方法

  • 基本思路是for循環(huán)先遍歷字符串;
  • 遍歷的字符要是沒(méi)在結(jié)果字符串中,就添加到結(jié)果字符串即可。
  • 代碼如下:
import unittest

class TestDeduplication(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.char_date = "12344312abcdcbdaABCDDCBA張王李張"
        print(f"原始字符串為:{cls.char_date}")

    @classmethod
    def tearDownClass(cls) -> None:
        pass

    def test_char_for(self):
        char_date01 = ""
        for data in self.char_date:
            if data not in char_date01:
                char_date01 += data
        print(f"for方法去重后數(shù)據(jù):{char_date01}")
        
if __name__ == "__main__":
    unittest.main()
  • 結(jié)果輸出為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
for方法去重后數(shù)據(jù):1234abcdABCD張王李

2.2 while方法

  • 思路和for差不多;
  • 這里主要是通過(guò)通過(guò)索引的方式查找;
  • 代碼如下:
import unittest

class TestDeduplication(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.char_date = "12344312abcdcbdaABCDDCBA張王李張"
        print(f"原始字符串為:{cls.char_date}")

    @classmethod
    def tearDownClass(cls) -> None:
        pass

    def test_char_while(self):
        char_date02 = ""
        flag = len(self.char_date) - 1
        while True:
            if flag >= 0:
                if self.char_date[flag] not in char_date02:
                    char_date02 += self.char_date[flag]
                flag -= 1
            else:
                break
        print(f"while方法去重后數(shù)據(jù):{char_date02}")

if __name__ == "__main__":
    unittest.main()
  • 輸出結(jié)果為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
while方法去重后數(shù)據(jù):張李王ABCDadbc2134

2.3 列表方法

  • 我們先把字符串轉(zhuǎn)為集合去重;
  • 再將集合轉(zhuǎn)為列表;
  • 將列表轉(zhuǎn)為字符串,最后排序進(jìn)行輸出即可;
  • 部分代碼如下,其他關(guān)于類(lèi)的內(nèi)容和以上一樣:
    def test_char_list(self):
        char_date03 = set(self.char_date)
        char_date04 = list(char_date03)
        char_date04.sort(key=self.char_date.index)
        print(f"列表方法去重后數(shù)據(jù):{''.join(char_date04)}")
  • 輸出后為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
列表方法去重后數(shù)據(jù):1234abcdABCD張王李

2.4 直接刪除法

  • 這個(gè)主要是直接對(duì)原字符串直接操作;
  • 通過(guò)下標(biāo)以及字符串切片方法實(shí)現(xiàn);
  • 部分代碼如下:
    def test_char_delete(self):
        for data in self.char_date:
            if self.char_date[0] in self.char_date[1:len(self.char_date)]:
                self.char_date = self.char_date[1:len(self.char_date)]
            else:
                self.char_date = self.char_date[1:len(self.char_date)] + self.char_date[0]
        print(f"直接刪除方法去重后數(shù)據(jù):{''.join(self.char_date)}")
  • 輸出為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
直接刪除方法去重后數(shù)據(jù):4312cbdaDCBA王李張

2.5 fromkeys方法

  • 直接使用fromkeys()方法,它的作用是從序列鍵和值設(shè)置為value來(lái)創(chuàng)建一個(gè)新的字典;
  • 部分代碼如下:
    def test_char_fromkeys(self):
        char_date05 = {}
        char_date06 = char_date05.fromkeys(self.char_date)
        list_char = list(char_date06.keys())
        print(f"fromkeys方法去重后數(shù)據(jù):{''.join(list_char)}")
  • 輸出為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
fromkeys方法去重后數(shù)據(jù):1234abcdABCD張王李

3 列表去重

3.1 for方法

  • 循環(huán)遍歷列表后添加到新的列表即可;
  • 這個(gè)方法不會(huì)改變?cè)瓉?lái)的順序;
  • 代碼如下:
class TestDeduplication(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.list_data = ["A", "B", "C", "D", "E", "C", "A", "B"]
        print(f"原始列表為:{cls.list_data}")

    @classmethod
    def tearDownClass(cls) -> None:
        pass
        
    def test_list_for(self):
        list_data01 = []
        for data in self.list_data:
            if data not in list_data01:
                list_data01.append(data)
        print(f"for方法:{list_data01} ")

if __name__ == "__main__":
    unittest.main()
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
for方法:['A', 'B', 'C', 'D', 'E'] 

3.2 set方法1

  • 直接使用set方法后轉(zhuǎn)為列表即可;
  • 這個(gè)方法會(huì)改變?cè)瓉?lái)的順序;
  • 部分代碼如下:
 def test_list_set(self):
        list_data02 = list(set(self.list_data))
        print(f"set方法1:{list_data02}")
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
set方法1:['D', 'C', 'B', 'E', 'A']

3.3 set方法2

  • 直接使用set方法后轉(zhuǎn)為列表;
  • 這個(gè)方法會(huì)改變?cè)瓉?lái)的順序,可進(jìn)行排序;
  • 部分代碼:
 def test_list_set01(self):
        list_data03 = list(set(self.list_data))
        list_data03.sort(key=self.list_data.index)
        print(f"set方法2:{list_data03}")
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
set方法2:['A', 'B', 'C', 'D', 'E']

3.4 count方法

  • 先對(duì)原序列進(jìn)行排序;
  • 循環(huán)遍歷列表后使用count()方法;
  • 部分代碼:
    def test_list_count(self):
        self.list_data.sort()
        for data in self.list_data:
            while self.list_data.count(data) > 1:
                del self.list_data[self.list_data.index(data)]
        print(f"count方法:{self.list_data}")
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
count方法:['A', 'B', 'C', 'D', 'E']

3.5 轉(zhuǎn)字典法

  • 直接把列表轉(zhuǎn)為字典方法即可;
  • 部分代碼:
 def test_list_dict(self):
        list_data04 = {}
        list_data05 = list_data04.fromkeys(self.list_data).keys()
        list_data06 = list(list_data05)
        print(f"字典法:{list_data06}")
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
字典法:['A', 'B', 'C', 'D', 'E']

4 完整代碼

  • 以下為列表和字符串常用的數(shù)據(jù)去重方法的完整代碼;
  • 使用unittest中的TestCase類(lèi)組織測(cè)試用例;
  • 代碼如下:
# -*- coding:utf-8 -*-
# 作者:蟲(chóng)無(wú)涯
# 日期:2023/11/22 
# 文件名稱(chēng):test_deduplication.py
# 作用:字符串和列表去重
# 聯(lián)系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson


import unittest


class TestDeduplication(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.char_date = "12344312abcdcbdaABCDDCBA張王李張"
        cls.list_data = ["A", "B", "C", "D", "E", "C", "A", "B"]
        print(f"原始字符串為:{cls.char_date}")
        print(f"原始列表為:{cls.list_data}")

    @classmethod
    def tearDownClass(cls) -> None:
        pass

    def test_char_for(self):
        char_date01 = ""
        for data in self.char_date:
            if data not in char_date01:
                char_date01 += data
        print(f"for方法去重后數(shù)據(jù):{char_date01}")

    def test_char_while(self):
        char_date02 = ""
        flag = len(self.char_date) - 1
        while True:
            if flag >= 0:
                if self.char_date[flag] not in char_date02:
                    char_date02 += self.char_date[flag]
                flag -= 1
            else:
                break
        print(f"while方法去重后數(shù)據(jù):{char_date02}")

    def test_char_list(self):
        char_date03 = set(self.char_date)
        char_date04 = list(char_date03)
        char_date04.sort(key=self.char_date.index)
        print(f"列表方法去重后數(shù)據(jù):{''.join(char_date04)}")

    def test_char_delete(self):
        for data in self.char_date:
            if self.char_date[0] in self.char_date[1:len(self.char_date)]:
                self.char_date = self.char_date[1:len(self.char_date)]
            else:
                self.char_date = self.char_date[1:len(self.char_date)] + self.char_date[0]
        print(f"直接刪除方法去重后數(shù)據(jù):{''.join(self.char_date)}")

    def test_char_fromkeys(self):
        char_date05 = {}
        char_date06 = char_date05.fromkeys(self.char_date)
        list_char = list(char_date06.keys())
        print(f"fromkeys方法去重后數(shù)據(jù):{''.join(list_char)}")

    print("===============================================")

    def test_list_for(self):
        list_data01 = []
        for data in self.list_data:
            if data not in list_data01:
                list_data01.append(data)
        print(f"for方法:{list_data01} ")

    def test_list_set(self):
        list_data02 = list(set(self.list_data))
        print(f"set方法1:{list_data02}")

    def test_list_set01(self):
        list_data03 = list(set(self.list_data))
        list_data03.sort(key=self.list_data.index)
        print(f"set方法2:{list_data03}")

    def test_list_count(self):
        self.list_data.sort()
        for data in self.list_data:
            while self.list_data.count(data) > 1:
                del self.list_data[self.list_data.index(data)]
        print(f"count方法:{self.list_data}")

    def test_list_dict(self):
        list_data04 = {}
        list_data05 = list_data04.fromkeys(self.list_data).keys()
        list_data06 = list(list_data05)
        print(f"字典法:{list_data06}")
        
        
if __name__ == "__main__":
    unittest.main()
  • 全部輸出為:

===============================================
原始字符串為:12344312abcdcbdaABCDDCBA張王李張
原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']

直接刪除方法去重后數(shù)據(jù):4312cbdaDCBA王李張
for方法去重后數(shù)據(jù):1234abcdABCD張王李
fromkeys方法去重后數(shù)據(jù):1234abcdABCD張王李
列表方法去重后數(shù)據(jù):1234abcdABCD張王李
while方法去重后數(shù)據(jù):張李王ABCDadbc2134
count方法:['A', 'B', 'C', 'D', 'E']
字典法:['A', 'B', 'C', 'D', 'E']
for方法:['A', 'B', 'C', 'D', 'E'] 
set方法1:['B', 'A', 'D', 'C', 'E']
set方法2:['A', 'B', 'C', 'D', 'E']

放一張圖吧(雖然用處不大,哈哈):

以上就是Python中列表和字符串常用的數(shù)據(jù)去重方法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Python列表和字符串?dāng)?shù)據(jù)去重的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python?hashlib模塊詳情

    Python?hashlib模塊詳情

    本文,我們來(lái)對(duì)Python?提供的安全哈希算法的通用接口-hashlib模塊進(jìn)行學(xué)習(xí),文章內(nèi)容具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2021-11-11
  • PyCharm設(shè)置中文(漢化與解除漢化)的方法

    PyCharm設(shè)置中文(漢化與解除漢化)的方法

    這篇文章介紹了PyCharm設(shè)置中文(漢化與解除漢化)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • FFrpc python客戶(hù)端lib使用解析

    FFrpc python客戶(hù)端lib使用解析

    這篇文章主要介紹了FFrpc python客戶(hù)端lib使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python爬蟲(chóng)beautifulsoup解析html方法

    python爬蟲(chóng)beautifulsoup解析html方法

    這篇文章主要介紹了python爬蟲(chóng)beautifulsoup解析html方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Python實(shí)現(xiàn)在線(xiàn)音樂(lè)播放器

    Python實(shí)現(xiàn)在線(xiàn)音樂(lè)播放器

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)在線(xiàn)音樂(lè)播放器的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Python下使用Scrapy爬取網(wǎng)頁(yè)內(nèi)容的實(shí)例

    Python下使用Scrapy爬取網(wǎng)頁(yè)內(nèi)容的實(shí)例

    今天小編就為大家分享一篇Python下使用Scrapy爬取網(wǎng)頁(yè)內(nèi)容的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Python實(shí)現(xiàn)的隨機(jī)森林算法與簡(jiǎn)單總結(jié)

    Python實(shí)現(xiàn)的隨機(jī)森林算法與簡(jiǎn)單總結(jié)

    這篇文章主要介紹了Python實(shí)現(xiàn)的隨機(jī)森林算法,結(jié)合實(shí)例形式詳細(xì)分析了隨機(jī)森林算法的概念、原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2018-01-01
  • 最新評(píng)論