在C编程语言中,我经常做以下事情:
while ((c = getch()) != EOF) {
/* do something with c */
}
Run Code Online (Sandbox Code Playgroud)
在Python中,我没有发现任何类似的东西,因为我不允许在evaluate表达式中设置变量.我通常最终必须设置两次评估表达式!
c = sys.stdin.read(1)
while not (c == EOF):
# Do something with c
c = sys.stdin.read(1)
Run Code Online (Sandbox Code Playgroud)
在我试图找到更好的方法时,我找到了一种只需要设置和评估表达式一次的方法,但这变得更加丑陋......
while True:
c = sys.stdin.read(1)
if (c == EOF): break
# do stuff with c
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经针对我的一些情况采用了以下方法,但这对于常规while循环来说远非最佳...:
class ConditionalFileObjectReader:
def __init__(self,fobj, filterfunc):
self.filterfunc = filterfunc
self.fobj = fobj
def __iter__(self):
return self
def next(self):
c = self.fobj.read(1)
if self.filterfunc(c): raise StopIteration
return c
for c in ConditionalFileObjectReader(sys.stdin,lambda c: …Run Code Online (Sandbox Code Playgroud) 我过来了,其中"not None"同时等于True和False.
>>> not None
True
>>> not None == True
True
>>> not None == False
True
Run Code Online (Sandbox Code Playgroud)
起初我预计这将是因为运算符的顺序,但是在测试类似的表达式时:
>>> not False
True
>>> not False == False
False
>>> not False == True
True
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么会这样吗?
我正在编写一个通用的应用程序代理.
我想将它用作透明代理,我的原始计划是使用带有REDIRECT规则的iptables转发到我的应用程序代理的所有连接.
这里的问题当然是我的应用程序代理丢失了有关预期目标的信息.
是否可以查询iptables以检索最初预期的收件人?任何其他可能解决这个问题的方法也很感激!
我正在编写一个脚本来收集各种网络统计信息.我要做的是从netstat -i命令生成一些delta数据.
我正在使用以下bash代码收集所需的数据:
declare -a array
n=0
netstat -i | tail -n +3 | while read LINE; do
echo "Setting array[$n] to $LINE"
array[$n]=$LINE
echo "array now have ${#array[@]} entries"
let n=$n+1
done
echo "array now have ${#array[@]} entries"
Run Code Online (Sandbox Code Playgroud)
此命令的输出是:
Setting array[0] to eth0 1500 0 4946794 0 0 0 2522971 0 0 0 BMRU
array now have 1 entries
Setting array[1] to lo 16436 0 25059 0 0 0 25059 0 0 0 LRU
array now have 2 entries …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行以下操作:
CPU_COUNT=$(cat /proc/stat | grep -E "^cpu[[:digit:]]+ " | wc -l)
let CPU_COUNT=CPU_COUNT-1
for core in {0..$CPU_COUNT}; do
echo $core
done
Run Code Online (Sandbox Code Playgroud)
在具有4个内核的系统上,我希望bash脚本循环4次,将核心从0递增到3.
然而,我收到的输出是:
{0..3}
Run Code Online (Sandbox Code Playgroud)
我正在做的事情显然是错误的,但我如何让它按预期工作?
我正在开发一个使用马尔可夫链的应用程序.
此代码的示例如下:
chain = MarkovChain(order=1)
train_seq = ["","hello","this","is","a","beautiful","world"]
for i, word in enum(train_seq):
chain.train(previous_state=train_seq[i-1],next_state=word)
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是迭代train_seq,但保留N个最后元素.
for states in unknown(train_seq,order=1):
# states should be a list of states, with states[-1] the newest word,
# and states[:-1] should be the previous occurrences of the iteration.
chain.train(*states)
Run Code Online (Sandbox Code Playgroud)
希望我的问题的描述足够明确
我正在编码一个N阶马尔可夫链.
它是这样的:
class Chain:
def __init__(self, order):
self.order = order
self.state_table = {}
def train(self, next_state, *prev_states):
if len(prev_states) != self.order: raise ValueError("prev_states does not match chain order")
if prev_states in self.state_table:
if next_state in self.state_table[prev_states]:
self.state_table[prev_states][next_state] += 1
else:
self.state_table[prev_states][next_state] = 0
else:
self.state_table[prev_states] = {next_state: 0}
Run Code Online (Sandbox Code Playgroud)
不幸的是,列表和元组是不可用的,我不能将它们用作dicts中的关键字......我希望能很好地解释我的问题,以便你理解我想要实现的目标.
如何为字典关键字使用多个值的任何好主意?
后续问题:
我不知道元组是可以清洗的.但是哈希的熵似乎很低.元组是否可能发生哈希冲突?!
我遇到了一个问题,我必须通过代理日志来查看用户是否访问过网站列表.
我写了一个小脚本来读取所有代理日志,将访问过的主机与列表进行匹配:
for proxyfile in proxyfiles:
for line in proxyfile.readlines():
if line[4] in hosts_list:
print line
Run Code Online (Sandbox Code Playgroud)
hosts_file很大,我们说的是~10000个主机,我注意到搜索时间比预期的要长.
我写了一个小测试:
import random, time
test_list = [x for x in range(10000)]
test_dict = dict(zip(test_list, [True for x in range(10000)]))
def test(test_obj):
s_time = time.time()
for i in range(10000):
random.randint(0,10000) in test_obj
d_time = time.time() - s_time
return d_time
print "list:", test(test_list)
print "dict:",test(test_dict)
Run Code Online (Sandbox Code Playgroud)
结果如下:
list: 5.58524107933
dict: 0.195574045181
Run Code Online (Sandbox Code Playgroud)
所以,对我的问题.是否可以以更方便的方式执行此搜索?创建列表的字典似乎是一个黑客,因为我想搜索它们的键而不是它包含的值.
我在python中编写了一个服务器,在C中编写了客户端.
python服务器向C客户端发送"1 1000 \n".
收到此字符串的C函数应将其解析为两个long int.
void receive_job() {
char tmp_buffer[256];
long start, stop;
recv(sock, tmp_buffer, 255, 0);
/*
here I wonder how I can parse tmp_buffer to set start and stop values.
*/
}
Run Code Online (Sandbox Code Playgroud)
我不是很精通C,所以我很感激对此发表评论.
python ×5
bash ×2
c ×2
arrays ×1
dictionary ×1
for-loop ×1
int ×1
iptables ×1
iterator ×1
linux ×1
optimization ×1
parsing ×1
python-2.2 ×1
search ×1
string ×1