小编use*_*440的帖子

Ruby - 以毫秒为单位的纪元时间到本地时间字符串

我有以下 UNIX 纪元时间戳 - 1475001600029

我需要将其转换local为格式的日期时间 -2016/09/28 00:01:13.536

我尝试过的事情

irb(main):005:0> Time.at(1475001600079)
=> 48710-12-11 00:11:19 +0530    # gives a weird value for year


irb(main):010:0> Time.strptime(1475001600079.to_s, '%Q')
NoMethodError: undefined method `strptime' for Time:Class
        from (irb):10
        from C:/RailsInstaller/Ruby2.2.0/bin/irb:11:in `<main>'
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

RUby 版本是 - ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32]

ruby

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

在R中使用ggplot2覆盖两个图

有两个数据帧- df1&df2从中我需要使用GGPLOT2和显示在同一图上以创建线图.

df1

 x   y 
2   30
4   25
6   20
8   15
Run Code Online (Sandbox Code Playgroud)

df2

 x   y 
2   12
4   16
6   20
8   24
Run Code Online (Sandbox Code Playgroud)

用于绘制第一个df,

p1 <- ggplot( df1, aes(x = df1[,1] , y = log(df1[,2]) ) )
p1 <- p1 + geom_line(aes(y = log(df1[,2])))
p1
Run Code Online (Sandbox Code Playgroud)

我们如何在数据帧的相同图形上获得相同的图表叠加 df2

r ggplot2

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

时间戳格式 - 从1/1000秒到1/100秒

需要将时间戳以1/1000秒分辨率转换为1/100分辨率.我可以to_char(timestamp, text)为此目的使用 格式化功能,但需要帮助text在这里使用.

输入表(注意 - 此处的时间戳存储为varchar)

+-------------------------+
|       ms1000_val        |
+-------------------------+
| 2017/02/20 08:27:17.899 |
| 2017/02/20 08:23:43.894 |
| 2017/02/20 08:24:41.894 |
| 2017/02/20 08:28:09.899 |
+-------------------------+
Run Code Online (Sandbox Code Playgroud)

输出表

+------------------------+
|       ms100_val        |
+------------------------+
| 2017/02/20 08:27:17.89 |
| 2017/02/20 08:23:43.89 |
| 2017/02/20 08:24:41.89 |
| 2017/02/20 08:28:09.89 |
+------------------------+
Run Code Online (Sandbox Code Playgroud)

sql postgresql

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

Ruby - 匹配字符串中的所有模式

需要帮助匹配字符串中的所有模式.

s1 = " countries like US, Japan, Korea, Vietnam, Germany"
s2 = " countries like Holland, China, Korea, Thailand, UK"
Run Code Online (Sandbox Code Playgroud)

从上面的字符串需要匹配特定的亚洲国家

regex = /(Japan)|(Korea)|(Vietnam)|(Cambodia)|(China)|(Thailand)/


irb(main):157:0> s1.match(regex)
=> #<MatchData "Japan" 1:"Japan" 2:nil 3:nil 4:nil>
Run Code Online (Sandbox Code Playgroud)

有没有办法获得以下输出?

s1.some_regex_operation # => Japan, Korea, Vietnam
s2.some_regex_operation # => China, Korea, Thailand
Run Code Online (Sandbox Code Playgroud)

ruby regex string

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

Elasticsearch没有索引DB中名为"type"的字段

使用单表继承,面临在表中索引名为"type"的特定列的问题.该表有两个感兴趣的列(以及其他),"product_name"和"type"."product_name"字段已正确编入索引,"类型"字段根本没有编入索引 - 有关故障排除的任何想法吗?使用elasticsearch-ruby gem和Ruby on Rails.

ruby-on-rails elasticsearch

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

使用dplyr按组计算的百分比

使用df如下数据框

df <- data.frame(colors = c("red", "blue", "green", "red", "red" , "blue"))
Run Code Online (Sandbox Code Playgroud)

我可以使用dplyr找出每种颜色的计数,如下所示

df %>%
  group_by(color) %>%
    summarise(count = n())
Run Code Online (Sandbox Code Playgroud)

而不是计数我需要找到每种颜色的百分比计数 - 如何使用dplyr去做?

r dplyr

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

从chrome扩展调用后端API

我需要从开发 chrome 页面操作扩展对 \xe2\x80\x98 http://localhost:3000/posts \xe2\x80\x99 进行 GET http 调用。通过 GET 操作,我获得了需要在 popup.html 中显示的帖子列表。在 \xe2\x80\x98content.js\xe2\x80\x99 中,我从当前选项卡中获取主题列表,以作为 GET 请求中的查询参数传递。

\n\n
    \n
  1. 如何从 Chrome 扩展发出 GET 请求?- 有关来自 chrome 扩展的 http 请求的教程链接(如果可用)将会有所帮助。
  2. \n
  3. 应该从哪里发出 get 请求 - content.js、popup.js 还是 background.js?
  4. \n
\n

google-chrome-extension

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

熊猫-读取以字符串形式存储在内存中的csv到数据帧

逗号分隔的文本存储在如下所示的var中

data = """
Class,Name,Long,Lat
A,ABC11,139.6295542,35.61144069
A,ABC20,139.630596,35.61045559
A,ABC03,139.6300307,35.61327781
B,ABC54,139.7787818,35.68847945
B,ABC05,139.7814447,35.6816882
B,ABC06,139.7788191,35.681865
B,ABC24,139.7790396,35.67781697
"""
Run Code Online (Sandbox Code Playgroud)

有没有一种快速的方法可以将其读入pandas数据框,而无需存储到文件并使用pd.read_csv()。我来自这里R,它提供了一个很好的方法来像下面这样。

text <- "
State,District,County,Num Voters,Total Votes in State,Votes for None,Candidate Name,Party,Votes Scored
CA,San Diego,Delmar,190962,48026634,2511,A1,IND,949
CA,San Diego,Delmar,190962,48026634,2511,A2,RP(K),44815
"
df <- read.table(textConnection(text), sep = ",", header = TRUE)
Run Code Online (Sandbox Code Playgroud)

python pandas

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

python dicts列表-将每个键值转换为单独的dict

具有如下所示的列表,其中包含一个或多个字典

l = [{'b': 'h', 'c': (1,2)}, {'d': [0, 1], 'e': {'f': 2, 'g': 'i'} } ]

需要将每个键值对提取为一个单独的字典

预期输出

[{'b': 'h'}, {'c': (1,2)}, {'d': [0, 1]}, {'e': {'f': 2, 'g': 'i'} } ]

我一直试图通过列表理解来做到这一点 - 外部理解可能类似于[ {k,v} for k, v in ??- 需要一些帮助来获得内部理解。

python list-comprehension

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

python list of dicts - 提取列表的键值

有一个像下面这样的字典列表

a = [{'a': 1, 'b': [{'c': [1, 2, 3], 'd': 4}, {'c': [5, 6, 1], 'd': 7}]}, {'a': 2, 'b': [{'c': [2, 3, 4], 'd': 5}, {'c': [7, 6, 1], 'd': 8}]}]
Run Code Online (Sandbox Code Playgroud)

需要将键的值提取b到另一个列表中

我可以做到以下几点

b = []
for item in a:
  b+= item['b']
Run Code Online (Sandbox Code Playgroud)

[{'c': [1, 2, 3], 'd': 4}, {'c': [5, 6, 1], 'd': 7}, {'c': [2, 3, 4], 'd': 5}, {'c': [7, 6, 1], 'd': 8}]很好。

我正在尝试学习列表理解,并想知道如何做到这一点。我尝试了以下 [ item['b'] for item in a]。但是,这给出了如下列表的列表。 …

python list-comprehension

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