你如何修复以下代码?
我想获得i mod 5 == 1的元素片.
data = "8|9|8|9|8|9|8|9|9|8|9|8|9|8|9|8"
arra = map(int,data.split("|"))
sums += [sum(arra[i % 5==1:(i + 4) % 5==1]) // Problem here
for i in range(0, len(arra), 4)]
Run Code Online (Sandbox Code Playgroud) 你怎么修复代码?
我试图拥有i % 3 == 1并且i != 16没有成功
data = "8|9|8|9|8|9|8|9|9|8|9|8|9|8|9|8"
arra = map(int,data.split("|"))
arra = sum(arra[1::3 and != 16]) for i in range(0, len(arra), 16)]
|
|---// Problem here
Run Code Online (Sandbox Code Playgroud) 谢谢你嘎嘎指出一个一个!
以下代码是我第一次使用Optparse编写代码.
如何在Optparse获取帮助时解决以下错误?
#!/usr/bin/env python
import sys
import os
from optparse import OptionParser
e = sys.argv[1]
b = sys.argv[2]
no = sys.argv[3]
def set_figu(figu):
sum = 777
return sum
def main():
usage = "Usage: negative_bin_base.py <eksponentti> <siirre> <figu>"
parser = OptionParser(usage)
parser.add_option("-h", "--help", dest="help",
help="get synopsis of parameters")
# print the output of the work-horse
print set_figu(no)
(options, args) = parser.parse_args()
if len(args) < 4:
parser.error("incorrect number of arguments")
if options.informative:
print "reading %s..." % options.help
if __name__ == "__main__": …Run Code Online (Sandbox Code Playgroud) 我将这个字符串列表转换为浮点列表时遇到了一些困难.我尝试了这两种方式,每种都返回了不同的错误.
import csv
import math
unemp_reader = csv.reader(open('unemp.csv', 'rU'))
unemp_lines = list(unemp_reader)
for rows in unemp_lines: #tried this way, but error tells me indices must be integers
i = 1
for i in rows:
a = map(float, unemp_lines[i])
float_list.append(a)
print float_list
for row in unemp_lines: #tried this way but the list returned is empty
y = row[1].split(",")[1:-1]
float_list = [float(i) for i in y if i]
print float_list
Run Code Online (Sandbox Code Playgroud) 码
use List::MoreUtils 'pairwise'; # http://stackoverflow.com/a/1865966/54964
my @offset = (0.28)x scalar(@x); # http://www.perlmonks.org/?node_id=110603
my @x = pairwise { $a + $b } @x, @offset;
Run Code Online (Sandbox Code Playgroud)
我希望通过默认工具找到更好的方法.
有没有更好的方法在Perl中添加数组?
最低代码
#!/usr/local/bin/perl
use strict;
use warnings;
use Math::Geometry::Planar qw(SegmentLineIntersection);
sub x_intercepts {
my $xa = @{ $_[0] };
my @xcross = ();
my @x_axis = ( [0, 2000], [1, 2000] );
foreach my $i (0 .. 1) {
my $xc = SegmentLineIntersection([ @$xa[$i, $i + 1], @x_axis ]);
push @xcross, $xc;
}
return \@xcross;
}
my @xs = qw/22.595451 20.089094 17.380813 15.091260 12.477935 10.054821 7.270003 4.804673 4.728526 4.619254 4.526920 4.418416 4.321419 4.219890 4.123336 4.009777 3.912648 3.804183 3.705847 3.597756 3.512301 3.393413 …Run Code Online (Sandbox Code Playgroud) 简化代码所在的两个参数age和gender; 但是,我只想通过gender或选择案件age; 我在想如何一次又一次地重载getIDs(age)和getIDs(gender)不重复相同的代码; 假设您有50个参数等; 我尝试过,getIDs(age, "")但这不是一个好主意
getIDs <- function(age, gender) {
# https://stackoverflow.com/a/40330110/54964
ageIDs <- c(1,2,3)
genderIDs # dummy code here to indicate that do not use genderIDs if gender ""
intersect(ageIDs, genderIDs)
}
Run Code Online (Sandbox Code Playgroud)
主要数据
ID,Age,Gender
100,69,male
101,75,female
102,84,female
103,,male
104,66,female
Run Code Online (Sandbox Code Playgroud)
数据2
DF <- structure(list(ID = 100:104, Age = c(69L, 75L, 84L, NA, 66L), Gender =
c("male", "female", "female", "male", "female")), .Names = c("ID", "Age",
"Gender"), …Run Code Online (Sandbox Code Playgroud) Git说要使用
git add
Run Code Online (Sandbox Code Playgroud)
将未跟踪的文件添加到我的仓库.
我跑得不成功
git add shells/zsh/keyboard_configs/
git add shells/zsh/references/
git add shells/zsh/keyboard_configs/*
git add shells/zsh/references/*
git add shells/zsh/keyboard_configs/*<TAB>
git add shells/zsh/references/*<TAB>
git add .
Run Code Online (Sandbox Code Playgroud)
当我看到它时,我仍然得到文件未跟踪
git status
Run Code Online (Sandbox Code Playgroud)
你怎么能在长路径git添加文件?
我跑
$git status ~/bin
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# shells/zsh/keyboard_configs/
# shells/zsh/references/
nothing added to commit but untracked files present (use "git add" to track)
$git add shells/zsh/keyboard_configs ~/bin
$git add shells/zsh/references ~/bin
Run Code Online (Sandbox Code Playgroud)
以下是出乎意料的.我希望我添加了要跟踪的文件夹及其内容. …
我的数据
466.67
465.56
464.44
463.33
462.22
461.11
460.00
458.89
...
Run Code Online (Sandbox Code Playgroud)
我在Python中运行
sum(/tmp/1,0)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误.
如何通过Python计算值的总和?
我不理解qwPerl中的角色,我在调试此线程中的代码时理解这一点.它的手册在这里非常有限:qw/string/.
我试图理解下面的代码出了什么问题我正在对数据进行简单的操作(乘以一(1.0)).
原始代码,是现实案例中情况的简化版本
use v5.16;
use Math::Geometry::Planar qw(SegmentLineIntersection);
sub x_intercepts {
my ($points) = @_;
die 'Must pass at least 2 points' unless @$points >= 2;
my @intercepts;
my @x_axis = ( [0, 2000], [1, 2000] );
foreach my $i (0 .. $#$points - 1) {
my $intersect = SegmentLineIntersection([@$points[$i,$i+1], @x_axis]);
push @intercepts, $intersect if $intersect;
}
return \@intercepts;
}
my @xs = qw/22.595451 20.089094 17.380813 15.091260 12.477935 10.054821 7.270003 4.804673 4.728526 …Run Code Online (Sandbox Code Playgroud) MATLAB
x = 0:pi/100:2*pi;
y = sin(x);
plot(y)
Run Code Online (Sandbox Code Playgroud)
我认为range()不能在这里工作,因为它只接受结束参数作为整数但我需要浮点数(0.0; 2*pi).Python 2.7.11+中的伪代码
import math
x = pseudoRange(0.0, 2*math.pi, math.pi/100);
Run Code Online (Sandbox Code Playgroud)
ceil()对于整数而言,采取是不实际的.
你怎么能在Python中使用三角函数范围的浮点数?