小编Shi*_*ifu的帖子

蟒蛇n克,四,五,六克?

我正在寻找一种将文本分成n-gram的方法.通常我会做类似的事情:

import nltk
from nltk import bigrams
string = "I really like python, it's pretty awesome."
string_bigrams = bigrams(string)
print string_bigrams
Run Code Online (Sandbox Code Playgroud)

我知道nltk只提供bigrams和trigrams,但有没有办法将我的文本分成4克,5克甚至100克?

谢谢!

python string nltk n-gram

115
推荐指数
7
解决办法
12万
查看次数

保持if语句中的值

我正在编写一个代码,用于翻译单词中的每个单词,在字典中查找它们,然后将字典值附加到计数器中.但是,如果我打印计数器,我只从if语句中获取最后一个数字,如果有的话.如果我将打印计数器放在循环中,那么我会得到每个单词的所有数字,但没有总值.我的代码如下:

dictionary = {word:2, other:5, string:10}
words = "this is a string of words you see and other things"
if word in dictionary.keys():
   number = dictionary[word]
   counter += number
   print counter
Run Code Online (Sandbox Code Playgroud)

我的例子会给我:

[10]
[5]
Run Code Online (Sandbox Code Playgroud)

虽然我想要15,最好在循环之外,就像现实生活中的代码一样,单词不是单个字符串,而是许多正在循环的字符串.谁能帮我这个?

python if-statement

2
推荐指数
1
解决办法
77
查看次数

将整数列表转换为字符串

我有以下列表的列表:

foo=[21, 38, 38, 56, 23, 19, 11, 15, 19, 13, 20, 6, 0, 8, 0, 10, 11, 0, 11, 8, 12, 5]
Run Code Online (Sandbox Code Playgroud)

我想把它转换成类似的东西:

bar=21, 38, 38, 56, 23, 19, 11, 15, 19, 13, 20, 6, 0, 8, 0, 10, 11, 0, 11, 8, 12, 5
Run Code Online (Sandbox Code Playgroud)

该怎么做?我试过bar=''.join(foo)但这给了我一个错误信息.

python

1
推荐指数
2
解决办法
118
查看次数

将整数转换为负数

我正在尝试编写一些代码,如果发生某种情况,特定的数字必须更改为 - 该数字.到目前为止,我有以下代码:

x=6

for words in foo:
   if "bar" in words:
      crazy_function(x)
   else:
      pass
Run Code Online (Sandbox Code Playgroud)

如果单词"bar"在单词中,则x将需要为-6,如果不是单词,则需要以+6表示.在某些情况下,x = -6,在这种情况下,如果bar是单词,则需要变为正数.我需要用实际工作的东西替换"crazy_function()".

python

1
推荐指数
2
解决办法
151
查看次数

Python,根据最大值查找变量/键

我有几个值,像这样:

value_a = 5
value_b = 10
value_c = 20
Run Code Online (Sandbox Code Playgroud)

我想找到最大的值并打印值的NAME.通常我会用

val = [value_a, value_b, value_c]
print max (val)
Run Code Online (Sandbox Code Playgroud)

但这只给了我价值,而不是名字.

python dictionary list python-2.7

1
推荐指数
1
解决办法
537
查看次数

将行拆分为类别和文本

我的列表看起来像这样:

foo = ["neg * , This is a sentence","pos * , This is another sentence"]
Run Code Online (Sandbox Code Playgroud)

我需要将句子分成一个值,即一个值将成为类别,neg或者pos一个句子.我试过了:

for text in foo:
    text = text.split("*")
    for a,b in text:
        cat=a
        text=b
Run Code Online (Sandbox Code Playgroud)

但是我得到了"太多的价值来打开包装",任何人都有一个想法?

python split

0
推荐指数
1
解决办法
85
查看次数

将python列表中的所有数字转换为1或0

我有一个整数列表,它看起来像这样:

[10, 8, 4, 4, 13, 1, 1, 1, 1, 6, 1, 2, 1, 1, 0, 1, 5, 1, 5, 5, 2, 1, 0, 0, 4]
Run Code Online (Sandbox Code Playgroud)

我需要这个列表,每0,它保持0和每个更高的数字为1.因此将上面的列表转换为:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)

我试着使用以下代码:

for numbers in list:
   if number==0:
      number=0
   if number>1:
      number=1
Run Code Online (Sandbox Code Playgroud)

但这给了我:

 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
Run Code Online (Sandbox Code Playgroud)

python

-5
推荐指数
3
解决办法
3890
查看次数

标签 统计

python ×7

dictionary ×1

if-statement ×1

list ×1

n-gram ×1

nltk ×1

python-2.7 ×1

split ×1

string ×1