简体   繁体   中英

Count only letters abcdefghijklmnopqrstuvwxyz in a list

How to count only the letters abcdefghijklmnopqrstuvwxyz in a list in alphabetical order without Counter ?

For example, input:

['Bro', 'lo', '27', 'b']

Output

[['b', 1], ['l', 1], ['o', 2], ['r', 1]]

You can use a list comprehension with str.count and star.islower just use str.join first and sort it:

>>> data = ['Bro', 'lo', '27', 'b']
>>> combined = sorted(''.join(data))
>>> [[l, combined.count(l)] for l in combined if l.islower()]
[['r', 1], ['o', 2], ['l', 1], ['b', 1]]

Try this:

from collections import defaultdict
import string

my_count = defaultdict(int)
my_list = ['Bro', 'lo', '27', 'b']

for list_item in my_list:
    for char in list_item:
        if char in string.ascii_lowercase:
            my_count[char] += 1

print(sorted([(k,v) for k,v in my_count.items()]))

If you don't want to use Counter() you can loop over all the letters and count them. You can leverage .islower() to test if your character is a lowercase letter and get() to start the counts at 0:

l = ['Bro', 'lo', '27', 'b']

counts = dict()
for word in l:
    for letter in word:
        if letter.islower():
            counts[letter] = counts.get(letter, 0) + 1

print(list(counts.items()))
# [('r', 1), ('o', 2), ('l', 1), ('b', 1)]

This will be O(n) where n is the number of characters.

Try this

letters = 'abcdefghijklmnopqrstuvwxyz'
my_string = "".join(['Bro', 'lo', '27', 'b'])
my_list = list(filter(lambda c: c in letters, set(my_string)))
my_list.sort()

result = {}
for i in my_list:
    result[i] = my_string.count(i)

There're many ways of doing this without importing anything. If you without.count().

my_list = ['Bro', 'lo', '27', 'b']
letters = "abcdefghijklmnopqrstuvwxyz"
output = []

for letter in letters:
    count = 0

    for item in ''.join(my_list):
        if item == letter:
            count += 1

    if count > 0:
        output.append([letter,count])

print(output)

Do it without an increment counter variable.

my_list = ['Bro', 'lo', '27', 'b']
letters = "abcdefghijklmnopqrstuvwxyz"

output = [[letter,''.join(my_list).count(letter)] for letter in letters if ''.join(my_list).count(letter)>0]

print(output)

The following snippet does exactly as asked, but in javascript.

Edit: Yes, this uses a counter in the Map

 function CountLetters(words){ var min = "a".charCodeAt(0); var max = "z".charCodeAt(0); var letters = new Map(); for(let word of words){ for(let letter of word){ let charCode = letter.charCodeAt(0); var boundsOk = charCode >= min && charCode <= max; if(;boundsOk){ continue. } if(.letters,has(letter)){ letters;set(letter.0), } letters.set(letter; letters.get(letter) + 1). } } return Array,from(letters).sort(function(ab){ return a[0];charCodeAt(0) - b[0].charCodeAt(0) }), } console,log(CountLetters(['Bro', 'lo'; '27', 'b']));

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