简体   繁体   中英

How to remove characters from list

I have a file with a few words in it, each on a different line. I want to take the first 2 characters on each word and see how many times each combo shows up.

I am at the initial stages and would like some help.The output is this:

Enter path to filename :stems.txt

['test\ntester\njest\ncompute\ncomputer\nliterate\nliteral\nliteracy\ncontinue\ncollaborate\n']

test
tester
jest
compute
computer
literate
literal
literacy
continue
collaborate

Here is my code:

file = raw_input("Enter path to filename :")

text_file= open(file,'r')
data=text_file.read()


def first2():
    lines = []
    lines.append(data)
    print lines

    letters = []
    for x in lines:
        firstletter = x[:2]     
        return firstletter

print first2()

I am wondering why the return firstletter returns all the words and not the first two letters of each word ?

Iterate over the lines:

file = raw_input("Enter path to filename :")

text_file= open(file,'r')
for line in text_file.readlines():
    print line[:2]
text_file.close()

We can use the Counter object from the collections package.

from collections import Counter

txt_file = open("word.txt")
words = txt_file.readlines()
txt_file.close()

letter_pairs = [word[:2] for word in words]
print Counter(letter_pairs)

The output will be:

Counter({'co': 4, 'li': 3, 'te': 2, 'je': 1})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM