Text:
Each row contains some numbers after promotion. If these numbers are the same, they are considered to be the same row. For the same rows, only one row is kept.
Thoughts:
Cut based on dictionary and string.
Create an empty dictionary.
Read the text and cut the first half of each line. During the process of reading the text, loop through the dictionary to search. If not found, write the line to the dictionary. Otherwise, it means that the row has been written into the dictionary (that is, a duplicate row has appeared) and will no longer be written into the dictionary. This achieves the purpose of retaining only one row for duplicate rows.
The text is as follows:
/promotion/232 utm_source /promotion/237 LandingPage/borrowExtend/? ; /promotion/25113 LandingPage/mhd /promotion/25113 LandingPage/mhd /promotion/25199 com/LandingPage /promotion/254 LandingPage/mhd/mhd4/? ; /promotion/259 LandingPage/ydy/? ; /promotion/25113 LandingPage/mhd /promotion/25199 com/LandingPage /promotion/25199 com/LandingPage
The procedure is as follows:
line_dict_uniq = dict() with open('1.txt','r') as fd: for line in fd: key = line.split(' ')[0] if key not in line_dict_uniq.values(): line_dict_uniq[key] = line else: continue print line_dict_uniq print len(line_dict_uniq) # 这里是打印了不重复的行(重复的只打印一次),实际再把这个结果写入文件就可以了, # 就不写这段写入文件的代码了
The execution efficiency of the above program is relatively low, changing it to the following will improve it:
line_dict_uniq = dict() with open('1.txt','r') as fd: for line in fd: key = line.split(' ')[0] if key not in line_dict_uniq.keys(): line_dict_uniq[key] = line else: continue print line_dict_uniq print len(line_dict_uniq)
The above is the Python that the editor introduces to you to deduplicate text by line. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the Script House website!

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.

ThefastestmethodforlistconcatenationinPythondependsonlistsize:1)Forsmalllists,the operatorisefficient.2)Forlargerlists,list.extend()orlistcomprehensionisfaster,withextend()beingmorememory-efficientbymodifyinglistsin-place.

ToinsertelementsintoaPythonlist,useappend()toaddtotheend,insert()foraspecificposition,andextend()formultipleelements.1)Useappend()foraddingsingleitemstotheend.2)Useinsert()toaddataspecificindex,thoughit'sslowerforlargelists.3)Useextend()toaddmultiple

Pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)Theyarestoredincontiguousmemoryblocks,whichmayrequirereallocationwhenappendingitems,impactingperformance.2)Linkedlistswouldofferefficientinsertions/deletionsbutslowerindexedaccess,leadingPytho

Pythonoffersfourmainmethodstoremoveelementsfromalist:1)remove(value)removesthefirstoccurrenceofavalue,2)pop(index)removesandreturnsanelementataspecifiedindex,3)delstatementremoveselementsbyindexorslice,and4)clear()removesallitemsfromthelist.Eachmetho

Toresolvea"Permissiondenied"errorwhenrunningascript,followthesesteps:1)Checkandadjustthescript'spermissionsusingchmod xmyscript.shtomakeitexecutable.2)Ensurethescriptislocatedinadirectorywhereyouhavewritepermissions,suchasyourhomedirectory.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
