可能重复:
如何使用Python从列表中随机选择项目?
我有两个数组pool_list_X,pool_list_Y.两者都有一个numpy数组作为列表中的元素.所以基本上
pool_list_x[0] = [1 2 3 4] # a multidimensional numpy array.
Run Code Online (Sandbox Code Playgroud)
并且pool_list_x的每个元素都在pool_list_y中具有对应的元素
which is to say, that pool_list_x[i] corresponds to pool_list_y[i]
Run Code Online (Sandbox Code Playgroud)
现在.如果我必须从list_x中随机选择10个元素(因此相应的元素为list_y).我该怎么做呢.我可以想到一种非常天真的方式......随机生成数字.和东西..但这不是很有效..什么是pythonic方式来做到这一点.谢谢
我有一个字符串:
" This is such an nice artwork"
Run Code Online (Sandbox Code Playgroud)
我有一个tag_list ["art","paint"]
基本上,我想写一个函数,接受这个字符串和taglist作为输入,并返回单词"artwork",因为艺术作品包含在taglist中的单词art.
我如何最有效地做到这一点?
我希望这在速度方面是有效的
def prefix_match(string, taglist):
# do something here
return word_in string
Run Code Online (Sandbox Code Playgroud) 我有一个字符串形式的日期时间字符串:
2011-10-23T08:00:00-07:00
Run Code Online (Sandbox Code Playgroud)
我如何解析此字符串作为日期时间对象.
我做了以下阅读文档:
date = datetime.strptime(data[4],"%Y-%m-%d%Z")
Run Code Online (Sandbox Code Playgroud)
我得到了错误
ValueError: time data '2011-10-23T08:00:00-07:00' does not match format '%Y-%m-%d%Z'
Run Code Online (Sandbox Code Playgroud)
这很清楚.
但我不知道如何阅读这种格式.
有什么建议.谢谢
编辑:另外,我必须补充,我关心的只是日期部分
我的输入数据是hdfs.我只是想做wordcount但是有一些细微的差别.数据采用json格式.所以每一行数据都是:
{"author":"foo", "text": "hello"}
{"author":"foo123", "text": "hello world"}
{"author":"foo234", "text": "hello this world"}
Run Code Online (Sandbox Code Playgroud)
我只想在"文本"部分做单词.
我该怎么做呢?
到目前为止,我尝试了以下变体:
public static class TokenCounterMapper
extends Mapper<Object, Text, Text, IntWritable> {
private static final Log log = LogFactory.getLog(TokenCounterMapper.class);
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
try {
JSONObject jsn = new JSONObject(value.toString());
//StringTokenizer itr = new StringTokenizer(value.toString());
String text = (String) jsn.get("text");
log.info("Logging data");
log.info(text);
StringTokenizer …Run Code Online (Sandbox Code Playgroud) 一个非常菜鸟的问题..
我想替换所有出现的"." 在一个空格的字符串..
所以这就是我的尝试
String s = "1.2.3.4";
System.out.println(s);
s = s.replaceAll(".", " ");
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)
但第二次印刷是空白的?
我错过了什么?
我有一个时间datetime对象..它有日期和时间..
所以例如
d = (2011,11,1,8,11,22) (24 hour time time format)
Run Code Online (Sandbox Code Playgroud)
但这张时间戳是在山地标准时间..(亚利桑那州.凤凰城)
现在我想在EST中转换这个时间...
现在这只是时间增量调整..
但是,这也是夏令时问题.
我想知道是否有一种内置的方法来处理夏令时调整时区.
尝试提交请求时出现此错误。
Method Not Allowed
The method is not allowed for the requested URL.
Run Code Online (Sandbox Code Playgroud)
这是我的烧瓶代码。
@app.route("/")
def hello():
return render_template("index.html")
@app.route("/", methods=['POST','GET'])
def get_form():
query = request.form["search"]
print query
Run Code Online (Sandbox Code Playgroud)
还有我的index.html
<body>
<div id="wrap">
<form action="/" autocomplete="on" method="POST">
<input id="search" name="search" type="text" placeholder="How are you feeling?">
<input id="search_submit" value="Send" type="submit">
</form>
</div>
<script src="js/index.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
编辑..我完整的烧瓶代码:
from flask import Flask,request,session,redirect,render_template,url_for
import flask
print flask.__version__
app = Flask(__name__)
@app.route("/")
def entry():
return render_template("index.html")
@app.route("/data", methods=['POST'])
def entry_post():
query = request.form["search"]
print query
return …Run Code Online (Sandbox Code Playgroud) 看看下面的代码:
Long minima = -9223372036854775808L;
Long anotherminima = -9223372036854775808L;
System.out.println(minima==anotherminima); //evaluates to false
System.out.println(Long.MIN_VALUE);
Long another= 1L;
Long one = 1L;
System.out.println(another == one); //evaluates to true
Run Code Online (Sandbox Code Playgroud)
我无法理解这种行为..?我希望第一个eval也是真的..这就是我所期待的......
所以,我有一个看起来像的分类器
clf = VotingClassifier(estimators=[
('nn', MLPClassifier()),
('gboost', GradientBoostingClassifier()),
('lr', LogisticRegression()),
], voting='soft')
Run Code Online (Sandbox Code Playgroud)
我想基本上调整每个估算器的超参数.
有没有办法调整分类器的这些"组合"?谢谢
python machine-learning scikit-learn hyperparameters grid-search
我有一串如下所示的文本
1. Foobar { abc } ( Explanation - Foo { cde } - Count - 5301435 ) 2. Foobaz { memo } ( Explanation - baz {bleh } - Count - 13946664 )
我想提取
上面字符串中的abcand 。memo我的正则表达式基本上如下[链接
此处]
{(?:[^{}])*}
但它捕获了所有内容(returns abc, cde, memo, bleh)