我在numpy中使用where函数来查找字符串数组中的单字母字符串.例如:我将寻找'U'在['B' 'U' 'A' 'M' 'R' 'O']和获得的指标'U'.
letter = 'U'
row = ['B', 'U', 'A', 'M', 'R', 'O']
letter_found = np.where(row == letter)
Run Code Online (Sandbox Code Playgroud)
但是,当我在寻找字符串数组中不存在的字母时,我得到一个空元组,如下所示:
(array([], dtype=int64),)
Run Code Online (Sandbox Code Playgroud)
我需要能够检测到它何时找不到我在数组中寻找的字母.
我尝试过以下方法:
if not letter_found:
print 'not found'
Run Code Online (Sandbox Code Playgroud)
但这不起作用.如何检测tuplewhere函数返回的numpy是空的?是因为我的一个变量可能是错误的类型?我是新手python和编程.
我pika.BlockingConnection在消费者中使用,为每条消息执行一些任务.我还添加了信号处理功能,以便消费者在完成所有任务后正常死亡.
在处理消息并收到信号时,我只是"signal received"从函数中获取,但代码不会退出.所以,我决定检查在回调函数结束时收到的信号.问题是,我检查信号的次数是多少,因为此代码中还有更多的功能.是否有更好的处理信号的方法而不会过度使用东西?
import signal
import sys
import pika
from time import sleep
received_signal = False
all_over = False
def signal_handler(signal, frame):
global received_signal
print "signal received"
received_signal = True
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
mq_connection = pika.BlockingConnection(pika.ConnectionParameters(my_mq_server, virtual_host='test'))
mq_channel = mq_connection.channel()
def callback(ch, method, properties, body):
if received_signal:
print "Exiting, as a kill signal is already received"
exit(0)
print body
sleep(50)
mq_channel.basic_ack(delivery_tag=method.delivery_tag)
print "Message consumption complete"
if received_signal:
print "Exiting, as a kill signal is already …Run Code Online (Sandbox Code Playgroud) 我刚开始使用熊猫来分析地下水井数据.
我在文本文件中的数据看起来像(site_no,date,well_level):
485438103132901 19800417 -7.1
485438103132901 19800506 -6.8
483622101085001 19790910 -6.7
485438103132901 19790731 -6.2
483845101112801 19801111 -5.37
484123101124601 19801111 -5.3
485438103132901 19770706 -4.98
Run Code Online (Sandbox Code Playgroud)
我希望输出的平均井水平以5年为增量并且计数:
site_no avg 1960-end1964 count avg 1965-end1969 count avg 1970-end1974 count
Run Code Online (Sandbox Code Playgroud)
我正在阅读数据:
names = ['site_no','date','wtr_lvl']
df = pd.read_csv('D:\info.txt', sep='\t',names=names)
Run Code Online (Sandbox Code Playgroud)
我可以通过以下网站找到整体平均值:
avg = df.groupby(['site_no'])['wtr_lvl'].mean().reset_index()
Run Code Online (Sandbox Code Playgroud)
我的原始垃圾箱尝试使用:
a1 = df[df.date > 19600000]
a2 = a1[a1.date < 19650000]
avga2 = a2.groupby(['site_no'])['wtr_lvl'].mean()
Run Code Online (Sandbox Code Playgroud)
我的问题:如何根据需要加入结果?我尝试了合并,加入和追加,但它们不允许空数据帧(这种情况发生).此外,我确信有一种简单的方法可以按日期对数据进行分区.谢谢.
我正在通过上传读取csv文件并尝试将所有值存储在列表中
def upload(request):
paramFile = request.FILES['file'].read()
data = csv.DictReader(paramFile)
list1 = []
for row in data:
list1.append(row)
print list1
Run Code Online (Sandbox Code Playgroud)
FILE.CSV
12345,abcdef
Run Code Online (Sandbox Code Playgroud)
产量
[{'1': '', None: ['']}, {'1': '2'}]
Run Code Online (Sandbox Code Playgroud)
我想附加所有值 list1
我试图json从twitter上载这个字典:
{"created_at":"Thu Jul 10 20:02:00 +0000 2014","id":487325888950710272,"id_str":"487325888950710272","text":"\u5f81\u9678\u300c\u5de6\u8155\u306e\u7fa9\u624b\u306f\u30db\u30ed\u3060\u300d","source":"\u003ca href=\"http:\/\/twittbot.net\/\" rel=\"nofollow\"\u003etwittbot.net\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1429838018,"id_str":"1429838018","name":"\u3053\u3093\u306a\uff30\uff30\u306f\u5acc\u3060\u3002","screen_name":"iyada_pp","location":"\u516c\u5b89\u5c40\u306e\u3069\u3053\u304b\u3002","url":null,"description":"\u3010\u3053\u3093\u306aPSYCHO-PASS\u306f\u5acc\u3060\u306a\u3011\u3068\u3044\u3046\u304f\u3060\u3089\u306a\u3044\u5984\u60f3bot\u3067\u3059\u3002\u30ad\u30e3\u30e9\u5d29\u58ca\u304c\u6fc0\u3057\u3044\u306e\u3067\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002","protected":false,"followers_count":99,"friends_count":98,"listed_count":5,"created_at":"Wed May 15 07:52:33 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":12584,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/3661872276\/ab7201283dac5dc1789bb6dfa9b6abe4_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/3661872276\/ab7201283dac5dc1789bb6dfa9b6abe4_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"ja"}
Run Code Online (Sandbox Code Playgroud)
我做json.load()了那个dict.但我在下面得到了错误消息:
NameError: name 'false' is not defined
Run Code Online (Sandbox Code Playgroud)
发生了什么?
我试图了解这些函数之间的区别(如果有的话):
numpy.random.rand()
numpy.random.random()
numpy.random.uniform()
Run Code Online (Sandbox Code Playgroud)
它们似乎从均匀分布中产生随机样本.那么,在函数中没有任何参数,有什么区别吗?
我有一个简单的数学任务,我在执行时遇到问题,涉及随机导入.这个想法是有10个随机生成的问题的测验.我使用random.randint函数得到的数字范围为(0,12),工作正常.下一点选择一个随机运算符我遇到了''+',' - ','*','/']的问题.
我在学校里有更复杂的编码,但这是我的实践,我需要的是能够随机创建问题并提出问题,同时也能够自己回答它以确定给出的答案是否正确.这是我的代码:
import random
ops = ['+', '-', '*', '/']
num1 = random.randint(0,12)
num2 = random.randint(0,10)
operation = random.choice(ops)
print(num1)
print(num2)
print(operation)
maths = num1, operation, num2
print(maths)
Run Code Online (Sandbox Code Playgroud)
截至目前,我的输出有点搞砸了.例如:
3
6
*
(3, '*', 6)
Run Code Online (Sandbox Code Playgroud)
显然,它无法确定(3,'*',6)的答案.我将把这个操作变成我的其他程序中的子程序,但它需要先工作!
并且请原谅我,如果它做得不好,这是我在学校完成的任务的快速再现,而且我在这方面也是相当新的,知识有限.提前致谢!
我正在尝试编译摩西(机器翻译工具)。我编译时指定了boost的位置如下。
./bjam --with-boost=/home/xhotsuki/bin/boost_1_56_0 -j8
Run Code Online (Sandbox Code Playgroud)
但是我遇到了很多这样的错误。
...failed gcc.link mert/bin/gcc-4.4.6/release/debug-symbols-on/link-static/threading-multi/timer_test...
...skipped <pmert/bin/gcc-4.4.6/release/debug-symbols-on/link-static/threading-multi>timer_test.passed for lack of <pmert/bin/gcc-4.4.6/release/debug-symbols-on/link-static/threading-multi>timer_test...
gcc.link mert/bin/gcc-4.4.6/release/debug-symbols-on/link-static/threading-multi/util_test
mert/bin/gcc-4.4.6/release/debug-symbols-on/link-static/threading-multi/UtilTest.o: In function 'main':<br>
/home/xhotsuki/bin/boost_1_56_0/include/boost/test/unit_test.hpp:59: undefined reference to `boost::unit_test::unit_test_main(bool (*)(), int, char**)'
Run Code Online (Sandbox Code Playgroud)
出了什么问题?
当启动策略设置为创建线程时std::launch::async,cppreference上给出的描述是
启动新线程以异步执行任务
如果我有一些任意的功能
double Foo(double i)
{
return i * 5.0;
}
Run Code Online (Sandbox Code Playgroud)
我async这样打电话
std::vector<double> values{5.0, 2.3, 7.1, 4.8, 1.5};
std::vector<std::future<double>> answers;
for (double value : values)
{
answers.push_back(std::async(std::launch::async,
Foo,
value));
}
Run Code Online (Sandbox Code Playgroud)
当我这样打电话时std::accumulate:
double total = std::accumulate(begin(answers),
end(answers),
0.0,
[](double x, std::future<double>& t){return x + t.get();});
Run Code Online (Sandbox Code Playgroud)
什么时候每个线程开始执行?他们一加入就开始了answers吗?或者他们等到他们get被调用?如果是这样,我是否只是强制它们按顺序执行,因为它们get是按执行顺序调用的accumulate?换句话说,我是否只是浪费时间设置这些未来,然后强迫它们同步运行?
注意
该函数Foo只是一些例子,我正在使用的实际函数做了更多工作.
我有一个具有一个固定大小的数组的一类Double,例如
Private m_values(8) as Double
Run Code Online (Sandbox Code Playgroud)
数组Let和Get方法的正确语法是什么?
Public Property Let Values (RHS(8) as Double)
m_values = RHS
End Property
Public Property Get Values() as Double
Values = m_values
End Property
Run Code Online (Sandbox Code Playgroud)
我不清楚语法的具体部分:
一个.在Let方法中,RHS(8) as Double传递8的数组的正确方法是Double什么?
湾 我可以使用赋值将一个数组复制到另一个数组吗?(例如m_values = values)
c.对于该Get方法,声明函数是正确的as Double还是应该是这样的as Double(8)?