Python 3.3.2 - 创建单词长度列表

时间:2013-08-23 23:48:44

标签: python python-3.x count

我有一串带标点符号的单词,比如说......

string = 'Did the quick brown fox *really* jump over the fence?'

我已经过滤掉标点符号,现在就是:

'Did the quick brown fox really jump over the fence'

我把它分成了一个列表。

list = string.split()

现在,使用list,我现在需要将每个单词的长度计算到一个列表中,列表的长度是最长的单词。列表的设置如下:

lengthList = [1_letter_words, 2_letter_words, 3_letter_words, ...]

因此,对于string,它将是:

lengthList = [0, 0, 4, 2, 3, 1]

不幸的是,我在做这件事时遇到了麻烦。任何人都可以提供任何帮助吗?

谢谢。

5 个答案:

答案 0 :(得分:1)

我不想在没有给你正确答案的情况下讨好你(当然,但肯定不是),所以如果你不关心良好的编码实践,请跳过去。

不要使用liststring等变量名称,因为 - 在list的情况下 - 这就是您正在制作的类型的名称。事实上,这就是你如何制作你正在制作的类型的空实例:

something=list()       # this is an empty list!

这会使引用list[2]或类似的东西变得混乱。所以你显然没有遇到任何错误,但为了便于阅读,请尝试提出有意义的变量名称。

好的,我已经完成了我的咆哮,你正在寻找的代码是

st='Did the quick brown fox really jump over the fence'.split()
c=[len(i) for i in st] # gives [3, 3, 5, 5, 3, 6, 4, 4, 3, 5]
counts=[0]*max(c)      # gives [0, 0, 0, 0, 0, 0]
for i in range(len(c)):
  counts[c[i]-1]+=1    # adds 1 to each index of c[i] (we subtract 1 because of 0-based indices)
print(counts)          # gives us the answer: [0, 0, 4, 2, 3, 1]

我做了一些比你提出的挑战更先进的步骤,以阻止你在作业中使用它,如果这恰好是你的目标。这个解决方案中使用的一些工具至少稍微比你正在使用的工具更进一步,但如果你只是学习Python以获得理解代码的奖励,那么我希望这将是最有启发性的,也许让你想到一些你可以用Python简洁地做的极其酷的东西。尽管如此,让我们来看看它:

我将假设st任务足够清楚,我们不需要讨论它,但请注意,我在分配时将其拆分。我只是懒惰,我们可以分两步完成,但这不是问题的关键所以让我们继续前进。

c=[len(i) for i in st]

只是表示“我们称之为i的每个元素,在st中,在列表中返回len(i),并将该列表c” 。这可能看起来令人生畏,但列表理解实际上并不是那么糟糕,而且你可以看到它们在编码方面节省了相当多的时间。实际上,这是一个相当适度的实现。

counts=[0]*max(c)

表示要在每个空格中制作一个包含0的列表,并使其重复多次max c。所以这将是最长的单词,在这种情况下是6个字母的单词'really',并使列表中的6个元素变长。这将确保我们有一个列表,其中包含我们遇到的每个长度单词的空格。

for i in range(len(c)):
  counts[c[i]-1]+=1

哦,小伙子,现在我们正在做饭。看到我们正在遍历列表c,因此我们通过它获得的每个项目将是相应单词的长度:

  • 第一个元素为3,对应Did
  • 第二个元素为3,对应the
  • ...
  • 最后一个元素为5,对应fence

这就是c[i]的含义,但counts[c[i]-1]是什么?好counts会在您找到的每个长度上添加1,因此当它有3个字符长的单词时,它会将1添加到bin中。 c[i]会在第一个元素上给你3,但由于列表是0索引的(列表从0开始并从那里开始),你需要补偿 - 因此-1。所以我们看到counts[c[i]-1]现在它更有意义了,对吧?

counts[c[i]-1] # this means counts[3-1] which means go find the bin corresponding to counts[2]

# ---v   this one
[0,0,0,0,0]

+=1只是意味着“将已添加的内容添加1”。

Python会愉快地遍历它并给你答案。

答案 1 :(得分:1)

from collections import Counter
Data = 'Did the quick brown fox really jump over the fence'
Freq = Counter([len(words) for words in Data.split()])
print ([Freq[Num] if Num in Freq else 0 for Num in range(1, max(Freq)+1) ])

<强>输出

[0, 0, 4, 2, 3, 1]

易于理解的版本

from collections import Counter
Data = 'Did the quick brown fox really jump over the fence'
Freq = Counter([len(words) for words in Data.split()])
Result = []
for Num in range(1, max(Freq)+1):
    if Num in Freq:
        Result.append(Freq[Num])
    else:
        Result.append(0)
print (Result)

答案 2 :(得分:0)

类似的东西:

>>> words = [len(word) for word in "this is a sentence".split()]
>>> words.sort()
>>> words
[1, 2, 4, 8]

答案 3 :(得分:0)

string = 'Did the quick brown fox really jump over the fence'
L = string.split()
D = {}    
res = []

for w in L: #store words lenght as keys and repetition as values 
    if len(w) in D:
        D[len(w)] += 1 #increase by one if had seen word with same lenght.
    else:
        D[len(w)]  = 1 #initialize hash with value one if had not seen word with that length  before. 

res = [D.get(x, 0) for x in range(1, max(D.keys()) + 1)]

print(res)
#[0, 0, 4, 2, 3, 1]

答案 4 :(得分:0)

original_str = "This is a demonstration"
num_words_list = list(original_str.split())
num_words_list = [len(i)for i in num_words_list]