小编Zlo*_*Zlo的帖子

NLTK将实体识别命名为Python列表

我使用NLTK ne_chunk从文本中提取命名实体:

my_sent = "WASHINGTON -- In the wake of a string of abuses by New York police officers in the 1990s, Loretta E. Lynch, the top federal prosecutor in Brooklyn, spoke forcefully about the pain of a broken trust that African-Americans felt and said the responsibility for repairing generations of miscommunication and mistrust fell to law enforcement."


nltk.ne_chunk(my_sent, binary=True)
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何将这些实体保存到列表中?例如 -

print Entity_list
('WASHINGTON', 'New York', 'Loretta', 'Brooklyn', 'African')
Run Code Online (Sandbox Code Playgroud)

谢谢.

python nlp named-entity-recognition nltk

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

as.Date来自'YYYY.mm'格式

我有一个数据框,其中日期存储为double例如,1993.09 1993.10 1993.11 1993.12

我想将其转换为日期格式'%Y %m %d'(总是天数1).

据我所知,as.Date()想要一个字符串输入.但是,由于某些原因,当我将日期转换为字符串后sapply(dates, as.character),零点会消失,从而有效地将10月转换为1月,从而导致每年有两个Januaries.

dates
1993.07 1993.08 1993.09 1993.10 1993.11 1993.12
sapply(dates, as.character)
sub("[.]", " ", dates)
"1993 07" "1993 08" "1993 09" "1993 1"  "1993 11" "1993 12"
Run Code Online (Sandbox Code Playgroud)

是否有更直接的方式来转换日期?或者我在哪里陷入困境?

dput:

c(1993.01, 1993.02, 1993.03, 1993.04, 1993.05, 1993.06, 1993.07, 
1993.08, 1993.09, 1993.1, 1993.11, 1993.12)
Run Code Online (Sandbox Code Playgroud)

r date as.date

7
推荐指数
2
解决办法
1475
查看次数

通过子字符串检查列表中的元素

我有一个urls(unicode)列表,并且有很多重复.例如,网址http://www.myurlnumber1.comhttp://www.myurlnumber1.com/foo+%bar%baz%qux指向同一个地方.

所以我需要清除所有这些重复项.

我的第一个想法是检查元素的子字符串是否在列表中,如下所示:

for url in list:
    if url[:30] not in list:
        print(url)
Run Code Online (Sandbox Code Playgroud)

但是,它尝试将字面url[:30]元素作为文字元素并显然返回所有这些元素,因为没有完全匹配的元素url[:30].

有没有简单的方法来解决这个问题?

编辑:

网址中的主机和路径通常保持不变,但参数不同.出于我的目的,具有相同主机名和路径但不同参数的URL仍然是相同的URL并构成重复.

python list

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

布尔矩阵形式Python的列表字典

我有一个列表的字典,例如,

dictionary_test = {'A': ['hello', 'byebye', 'howdy'], 'B': ['bonjour', 'hello', 'ciao'], 'C': ['ciao', 'hello', 'byebye']}
Run Code Online (Sandbox Code Playgroud)

我想将其转换为布尔从属关系矩阵以进行进一步分析.最好是dict keys作为列名,列表项作为行名:

         A    B    C
  hello  1    1    1
 byebye  1    0    1
  howdy  1    0    0
bonjour  0    1    0
   ciao  0    1    1
Run Code Online (Sandbox Code Playgroud)

是否可以在Python中进行(最好是我可以将矩阵写入.csv文件)?我想这是我必须要做的事numpy,对吗?

另一个问题是字典的大小是未知的(键的数量和列表中元素的数量都不同).

python dictionary numpy matrix

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

合并具有相同列数的数据帧

我的第一个数据框(一个较大的数据框)有10行13列.第二个具有4行13列(所有除了MonthNA秒.

当我尝试rbind他们时,我得到下一个错误:

Error in rbind(deparse.level, ...) : replacement has length zero
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚是什么问题,因为它们都有相同的列名.

dput:

    dput(sample.df)
structure(list(Month = structure(c(8674, 8552, 8401, 8491, 8521, 
8460, 8644, 8432, 8705, 8582), class = "Date"), Intention_CDU = c(211L, 
240L, 246L, 232L, 261L, 222L, 234L, 223L, 249L, 241L), Intention_SPD = structure(list(
    Intention_SPD = c(296L, 290L, 304L, 274L, 238L, 276L, 284L, 
    323L, 324L, 291L)), .Names = "Intention_SPD", row.names = c(9L, 
6L, 1L, 4L, 5L, 3L, 8L, 2L, 10L, 7L), class = "data.frame"), …
Run Code Online (Sandbox Code Playgroud)

r rbind

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

比较列表的Python字典中的值

我有一个列表的字典,数字作为键,字符串列表作为值.例如,

my_dict = {
    1: ['bush', 'barck obama', 'general motors corporation'],
    2: ['george bush', 'obama'],
    3: ['general motors', 'george w. bush']
}
Run Code Online (Sandbox Code Playgroud)

我想要的是比较每个列表中的每个项目(对于每个键),如果该项目是另一个项目的子字符串 - 将其更改为更长的项目.所以,这是一种非常糟糕的共识解决方案.

无法真正地围绕着如何做到这一点.这是我的想法的伪代码:

for key, value in dict:
    for item in value:
        if item is substring of other item in any other key, value:
            item = other item
Run Code Online (Sandbox Code Playgroud)

所以我的词典最终会看起来像这样:

my_dict = {
    1: ['george w. bush', 'barck obama', 'general motors corporation'],
    2: ['george w. bush', 'barck obama'],
    3: ['general motors corporation', 'george w. bush']
}
Run Code Online (Sandbox Code Playgroud)

对不起,如果我没有表达出明显的问题.

python dictionary

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

dplyr 按字符串汇总

我有一个包含数字和字符串值的数据框,例如:

 mydf <- data.frame(id = c(1, 2, 1, 2, 3, 4),
               value = c(32, 12, 43, 6, 50, 20),
               text = c('A', 'B', 'A', 'B', 'C', 'D'))
Run Code Online (Sandbox Code Playgroud)

id变量的值总是对应于text变量,例如,id == 1永远是text == 'A'

现在,我想通过id(或通过text,因为它是同一件事)总结这个数据框:

mydf %>%
  group_by(id) %>%
  summarize(mean_value = mean(value))
Run Code Online (Sandbox Code Playgroud)

这很好用,但我也需要text变量,因为我不想进行文本分析。

但是,当我添加text到 dplyr 管道时:

mydf %>%
  group_by(id) %>%
  summarize(mean_value = mean(value),
  text = text)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误:期望一个值

由于textforid始终相同,是否可以将其附加到汇总的数据帧中?

r dplyr

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

在Python中正则表达式之后/之前的所有内容

我有多个字符串实例与下一个结构:

RT @username: Tweet text
Run Code Online (Sandbox Code Playgroud)

我需要捕获用户名(以后构建一个网络).到目前为止我有这个:

re.findall('\@(.*)') 
Run Code Online (Sandbox Code Playgroud)

哪个应该在"@"之后获得所有内容,但是我很难弄清楚如何获得所有内容(不包括)':'.

python regex

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

从python字符串中删除\ xa0,\ n,\ t

我有一个列表项,已将其转换为字符串:

[u'\n                      Door:\xa0Novum          \t      ']
Run Code Online (Sandbox Code Playgroud)

我需要删除所有内容,以便我离开

Door:Novum
Run Code Online (Sandbox Code Playgroud)

我尝试了各种方法:

string = string.replace("\xa0", "")
string.rstrip('\n')
string.translate(string.maketrans("\n\t", ""))
Run Code Online (Sandbox Code Playgroud)

我显然做错了,但无法弄清楚是什么

python string unicode

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

从Python中的字符串中提取信息

我的.csv数据如下:

June 8, 2009 Monday
June 8, 2009 Monday
June 6, 2009 Saturday
June 6, 2009 Saturday Correction Appended
June 6, 2009 Saturday
June 6, 2009 Saturday
June 6, 2009 Saturday

etc...
Run Code Online (Sandbox Code Playgroud)

数据跨越10年.我需要将月份和年份分开(并且不关心日期和日期).

单个月我有下一行代码:

for row in reader:
    date = row[1]
    month = date.partition(' ')[0]
    print month 
Run Code Online (Sandbox Code Playgroud)

但是我无法弄清楚如何从字符串中提取数字年份?我必须使用正则表达式吗?

python

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

标签 统计

python ×7

r ×3

dictionary ×2

as.date ×1

date ×1

dplyr ×1

list ×1

matrix ×1

named-entity-recognition ×1

nlp ×1

nltk ×1

numpy ×1

rbind ×1

regex ×1

string ×1

unicode ×1