小编alv*_*vas的帖子

将列表中的项连接到字符串

有没有更简单的方法将列表中的字符串项连接成一个字符串?

我可以使用该str.join()功能加入列表中的项目吗?

例如,这是输入['this','is','a','sentence'],这是所需的输出this-is-a-sentence

sentence = ['this','is','a','sentence']
sent_str = ""
for i in sentence:
    sent_str += str(i) + "-"
sent_str = sent_str[:-1]
print sent_str
Run Code Online (Sandbox Code Playgroud)

python string join list concatenation

478
推荐指数
7
解决办法
99万
查看次数

如何在string.replace中输入正则表达式?

我需要一些关于声明正则表达式的帮助.我的输入如下:

this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>. 
and there are many other lines in the txt files
with<[3> such tags </[3>
Run Code Online (Sandbox Code Playgroud)

所需的输出是:

this is a paragraph with in between and then there are cases ... where the number ranges from 1-100. 
and there are many other lines in the txt files
with such tags
Run Code Online (Sandbox Code Playgroud)

我试过这个:

#!/usr/bin/python
import os, sys, re, glob
for infile in glob.glob(os.path.join(os.getcwd(), '*.txt')):
    for …
Run Code Online (Sandbox Code Playgroud)

python regex string replace

272
推荐指数
6
解决办法
29万
查看次数

如何在Python中实现Softmax函数

Udacity的深度学习类中,y_i的softmax只是指数除以整个Y向量的指数之和:

在此输入图像描述

哪里S(y_i)是的SOFTMAX功能y_i,并e为指数和j是否定的.输入向量Y中的列数.

我尝试过以下方法:

import numpy as np

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()

scores = [3.0, 1.0, 0.2]
print(softmax(scores))
Run Code Online (Sandbox Code Playgroud)

返回:

[ 0.8360188   0.11314284  0.05083836]
Run Code Online (Sandbox Code Playgroud)

但建议的解决方案是:

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    return np.exp(x) / np.sum(np.exp(x), axis=0)
Run Code Online (Sandbox Code Playgroud)

它产生与第一个实现相同的输出,即使第一个实现显式获取每列和最大值的差异,然后除以总和.

有人可以用数学方式显示原因吗?一个是正​​确的而另一个是错的吗?

实现在代码和时间复杂性方面是否相似?哪个更有效率?

python numpy machine-learning logistic-regression softmax

219
推荐指数
10
解决办法
19万
查看次数

如何使用值声明ArrayList?

Java中的ArrayList或List声明质疑并回答了如何声明一个空,ArrayList但是如何声明一个带有值的ArrayList?

我尝试了以下但它返回语法错误:

import java.io.IOException;
import java.util.ArrayList;

public class test {
    public static void main(String[] args) throws IOException {
        ArrayList<String> x = new ArrayList<String>();
        x = ['xyz', 'abc'];
    }
}
Run Code Online (Sandbox Code Playgroud)

java initialization arraylist declare

189
推荐指数
6
解决办法
45万
查看次数

`sorted(list)`vs`list.sort()`有什么区别?

list.sort()对列表进行排序并保存已排序的列表,同时sorted(list)返回列表的已排序副本,而不更改原始列表.

  • 但什么时候使用哪个?
  • 哪个更快?又快多少?
  • 可以在之后检索列表的原始位置list.sort()吗?

python sorting copy list in-place

165
推荐指数
5
解决办法
9万
查看次数

如何按值对计数器进行排序? - 蟒蛇

除了对反向列表理解进行列表理解之外,还有一种pythonic方法可以按值对Counter进行排序吗?如果是这样,它比这更快:

>>> from collections import Counter
>>> x = Counter({'a':5, 'b':3, 'c':7})
>>> sorted(x)
['a', 'b', 'c']
>>> sorted(x.items())
[('a', 5), ('b', 3), ('c', 7)]
>>> [(l,k) for k,l in sorted([(j,i) for i,j in x.items()])]
[('b', 3), ('a', 5), ('c', 7)]
>>> [(l,k) for k,l in sorted([(j,i) for i,j in x.items()], reverse=True)]
[('c', 7), ('a', 5), ('b', 3)
Run Code Online (Sandbox Code Playgroud)

python sorting collections counter

107
推荐指数
4
解决办法
9万
查看次数

如何使用nltk或python删除停用词

所以我有一个数据集,我想删除使用的停止词

stopwords.words('english')
Run Code Online (Sandbox Code Playgroud)

我正在努力如何在我的代码中使用它只是简单地取出这些单词.我已经有了这个数据集中的单词列表,我正在努力的部分是与此列表进行比较并删除停用词.任何帮助表示赞赏.

python nltk stop-words

98
推荐指数
7
解决办法
16万
查看次数

如何删除字符串中的前导和尾随零?蟒蛇

我有几个像这样的字母数字字符串

listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', '000alphanumeric']
Run Code Online (Sandbox Code Playgroud)

删除尾随零的所需输出将是:

listOfNum = ['000231512-n','1209123100000-n','alphanumeric', '000alphanumeric']
Run Code Online (Sandbox Code Playgroud)

前导尾随零的所需输出为:

listOfNum = ['231512-n','1209123100000-n00000','alphanumeric0000', 'alphanumeric']
Run Code Online (Sandbox Code Playgroud)

删除前导零和尾随零的欲望输出将是:

listOfNum = ['231512-n','1209123100000-n00000','alphanumeric0000', 'alphanumeric']
Run Code Online (Sandbox Code Playgroud)

现在我一直按照以下方式进行,如果有以下情况,请建议更好的方法:

listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', \
'000alphanumeric']
trailingremoved = []
leadingremoved = []
bothremoved = []

# Remove trailing
for i in listOfNum:
  while i[-1] == "0":
    i = i[:-1]
  trailingremoved.append(i)

# Remove leading
for i in listOfNum:
  while i[0] == "0":
    i = i[1:]
  leadingremoved.append(i)

# Remove both
for i in listOfNum:
  while i[0] == "0":
    i …
Run Code Online (Sandbox Code Playgroud)

python string trailing chomp leading-zero

91
推荐指数
3
解决办法
14万
查看次数

使用NLTK创建新的语料库

我估计通常我的标题的答案是去阅读文档,但我浏览了NLTK书,但它没有给出答案.我是python的新手.

我有一堆.txt文件,我希望能够使用NLTK为语料库提供的语料库功能nltk_data.

我已经尝试PlaintextCorpusReader但是我无法进一步:

>>>import nltk
>>>from nltk.corpus import PlaintextCorpusReader
>>>corpus_root = './'
>>>newcorpus = PlaintextCorpusReader(corpus_root, '.*')
>>>newcorpus.words()
Run Code Online (Sandbox Code Playgroud)

如何newcorpus使用punkt 对句子进行分段?我尝试使用punkt函数,但punkt函数无法读取PlaintextCorpusReader类?

你能否告诉我如何将分段数据写入文本文件?

编辑: 这个问题有一次赏金,它现在有第二个赏金.请参阅赏金框中的文字.

python nlp corpus nltk

80
推荐指数
3
解决办法
7万
查看次数

74
推荐指数
4
解决办法
4万
查看次数