小编lat*_*ian的帖子

在Python中从切片对象检索切片的长度

标题说明了自己,如何从对象中取出2

slice(0,2)
Run Code Online (Sandbox Code Playgroud)

该文档有些混乱,或者是错误的文档

https://docs.python.org/2/c-api/slice.html

特别是我不明白输出的含义是什么

slice(0,2).indices(0)  # (0, 0, 1)
slice(0,2).indices(10 ** 10)  # (0, 2, 1)
Run Code Online (Sandbox Code Playgroud)

一种可能的解决方法是使用slice对象对列表进行切片

a = [1,2,3,4,5]
len(a[slice(0,2)])  # 2
Run Code Online (Sandbox Code Playgroud)

但这对于任意大的切片都会失败。

谢谢,我在其他帖子中找不到答案。

python list slice

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

Mac终端奇怪行为(点击输入时没有换行,没有可见文字)

我在Mac OS X 10.11.3上我的终端看起来像这样:

[Fabian@MacBook-Pro] > 
[Fabian@MacBook-Pro] > pyspark
Python 2.7.11 (default, Jan 29 2016, 17:48:19) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
16/03/17 10:08:22 WARN NativeCodeLoader: Unable to load native-hadoop library     for your platform... using builtin-java classes where applicable
Welcome to

      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /__ / .__/\_,_/_/ /_/\_\   version 1.6.1
      /_/

Using Python version 2.7.11 (default, …
Run Code Online (Sandbox Code Playgroud)

macos bash terminal

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

如何在 Python 中访问聚合函数的值

我创建了一个数据帧并分组和聚合时间戳,为每个分组提供最小值和最大值,结果数据帧看起来像这个 DF 定义为病人 ID,时间戳我按病人 ID 对 DF 进行分组,然后我想获取最小值和最大值每个组的最大时间戳,我这样做了

bypatient_date = pd.DataFrame(byencounter.agg({'timestamp' : [np.min,np.max]})).reset_index())

  patient_id  timestamp            
              amin        amax
0         19  3396-08-21  3396-08-25
1         99  2723-09-27  2727-03-17
2       3014  2580-12-02  2581-05-01
3      24581  3399-07-19  3401-04-13
Run Code Online (Sandbox Code Playgroud)

我正在尝试找出每个患者 ID 的最小值和最大值之间的差异,但在尝试访问时间戳 amin 和时间戳 amax 中的值时遇到问题有没有办法在不循环但使用内置 pandas 或 numpy 的情况下执行此操作

python aggregate pandas

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

在R中将矩阵强制转换为整数矩阵的最快方法

我刚刚意识到如果你创建一个带有整数值的矩阵,它们就会被存储为数字.

a <- matrix(c(0,1,0,1), ncol=2)
class(a[1,]) # numeric
Run Code Online (Sandbox Code Playgroud)

整数矩阵需要一半的内存量(对于大尺寸).以下函数将所有值强制转换为整数:

forceMatrixToInteger <- function(m){
    apply (m, c (1, 2), function (x) {
         (as.integer(x))
    })
}

a <- forceMatrixToInteger(a)

class(a[1,]) # integer
Run Code Online (Sandbox Code Playgroud)

我想知道你是否可以想到任何其他方法来做到这一点,以及它是否会更快或更高效.

sessionInfo

R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.3 (El Capitan)
Run Code Online (Sandbox Code Playgroud)

编辑:第一次测试

我定义了一个函数,它执行Richard Scriven回答描述的函数,以及我定义的函数和测试速度.

exp_size <- 4
exp_reps <- 3 
mat <- matrix(sample(c(0,1), 10^exp_size, replace=TRUE),ncol=10^(exp_size/2))

fun1<-function(){
    mode(mat) <- 'integer'
}

time <- proc.time()
    for (i in 1:10^exp_reps){
    fun1()
}
time <- proc.time()-time
print('Results fun1:')
print(time)

 print(time) …
Run Code Online (Sandbox Code Playgroud)

performance r matrix

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

标签 统计

python ×2

aggregate ×1

bash ×1

list ×1

macos ×1

matrix ×1

pandas ×1

performance ×1

r ×1

slice ×1

terminal ×1