我目前正在尝试检查某个元素是否属于列表,如果是,则返回1,否则返回0。
我一直在做这个循环但是因为矩阵会非常大(这里只提供了一个简单的例子)我想知道是否有一种有效的方法来做到这一点。
import requests
headers = {
'User-Agent': 'Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/45.0.2454.101 Safari\/537.36'
}
select_shop = 'Electroshop'
url = 'http://www.dealwebsite.co/' + select_shop
r = requests.get(url, headers=headers, timeout=3)
check = r.url.split('/')
if len(check) != 5:
print 'No deals on today'
exit()
else:
print 'Firesale Deals on NOW!'
,结果是
rm(list=ls()) # clear memory
names <- c("a","b","c","d","e","f","g","h","i","j","k50","l50","m50","n50","o50")
proba <- c(1,1,1,1,1,1,1,1,1,1,0.5,0.5,0.5,0.5,0.5)
T1_temp <- sample(names,4,prob=proba)
# Loop to check which element of T1_temp is included in names
T1 <- c()
for (i in 1:length(names)){
if (is.element(names[i],T1_temp)){T1[i]=1}
else{T1[i]=0}
}
答案 0 :(得分:3)
我们可以尝试%in%
as.integer(names %in% T1_temp)
或match
+(!is.na(match(names,T1_temp)))