这段代码中R双括号的Python等效项是什么?

时间:2019-05-09 16:28:21

标签: python r

我正在尝试将此R代码转换为Python。我不了解的部分是EventTime[[i]] = c(timeList,cutoff[i])中的双尖括号。 EventTime也为空。

n = 50
cutoff = runif(n,min = 2, max = n)
rvStore = rgeom(1000,prob=0.2)+runif(1000) 
rvPosn = 0
EventTime = list()
for(i in 1:n){
        rvPosn = rvPosn + 1
        intArrival = currentTime = rvStore[rvPosn] 
        timeList = c()
        while(currentTime < cutoff[i]){
          timeList = c(timeList,currentTime)
          rvPosn = rvPosn + 1
          intArrival = rvStore[rvPosn]
          currentTime = currentTime + intArrival
        }
        EventTime[[i]] = c(timeList,cutoff[i])
}

在Python中,这似乎是:

n = 50
cutoff = np.random.uniform(n, 2, n)
rvStore = np.random.geometric(0.2, size=1000) + np.random.uniform(size=1000)
rvPosn = 0
EventTime = []
for i in range(n):
    rvPosn = rvPosn + 1
    intArrival = currentTime = rvStore[rvPosn]
    timeList = []
    while(currentTime < cutoff[i]):
        timeList.append(currentTime)
        rvPosn = rvPosn + 1
        intArrival = rvStore[rvPosn]
        currentTime = currentTime + intArrival
    EventTime[i] = # What should this be??

3 个答案:

答案 0 :(得分:3)

首先,让我们介绍一下case (POST, "/${version}/${service}) => if ($version == 0 && $service.equalToIgnoreCase("userService")) process(user service) if ($version == 0 && $service.equalToIgnoreCase("bookService")) process(book service) 的术语:R是一个列表,这意味着要获取/修改特定条目,需要使用双括号表示法,请参见

EventTime

关于列表为空,我相信内部EventTime[1] # returns the sub-list consisting of the first entry EventTime[[1]] # actually returns the content of the first entry 将重新声明该列表。考虑以下示例:

EventTime[[i]] = ...

现在到test_list <- list() test_list[[2]] <- "Hello" test_list # [[1]] # NULL # # [[2]] # [1] "Hello" ,这是对您的代码进行了一些修改:

Python

返回

# import numpy as np

# Set seed for replicating purposes
np.random.seed(seed=123)

n = 50
cutoff = np.random.uniform(n, 2, n)
rvStore = np.random.geometric(0.2, size=1000) + np.random.uniform(size=1000)
rvPosn = -1    # Indexing starts at 0 in python, hence set to -1 here.
EventTime = []
for i in range(n):
    rvPosn += 1
    intArrival = currentTime = rvStore[rvPosn]
    timeList = list()
    while currentTime < cutoff[i]:
        timeList.append(currentTime)
        rvPosn += 1
        intArrival = rvStore[rvPosn]
        currentTime += intArrival
    timeList.append(cutoff[i])
    if EventTime:    # If EventTime is not an empty list
        EventTime.extend(timeList)
    else:
        EventTime = timeList

答案 1 :(得分:0)

r中的双括号[[]]用于从列表中子集化。由于r代码使用EventTime = list()将“ EventTime”建立为列表,因此您需要使用双括号来表示您正在更改列表元素,而不是其中的元素。

EventTime[[1]]是“ EventTime”列表中的第一个元素。

答案 2 :(得分:0)

在R中,您可以使用双括号将列表中的特定元素调用。

https://www.tutorialspoint.com/r/r_lists.htm

# Create a list containing strings, numbers, vectors and a logical
# values.
list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(list_data)

哪个给

[[1]]
[1] "Red"

[[2]]
[1] "Green"

[[3]]
[1] 21 32 11

[[4]]
[1] TRUE

[[5]]
[1] 51.23

[[6]]
[1] 119.1

因此在R中,调用list_data[[1]]将返回[1] "Red"

list_data[[3]](列表中的这些元素是向量,可以进一步子集化)

list_data[[3]][2]将返回32

我的Python语言不是我最好的语言,但我认为您可以编写

EventTime[i] = [timeList, cutoff[i]]