相关疑难解决方法(0)

为什么python从字符串返回一个数字?

好吧,这让我感到困惑,我正在打印一个字符串及其返回的数字.我不明白它是如何从字符串返回数字的.

这是代码片段.

string = "String"
print int(string[0:min(5,len(string))],36)
Run Code Online (Sandbox Code Playgroud)

该片段的输出是

48417935
Run Code Online (Sandbox Code Playgroud)

我的朋友告诉我这与计算机生成字符串数字有关,但我很困惑.

有人可以请善意并解释为什么会这样吗?

python int casting calculus

1
推荐指数
1
解决办法
85
查看次数

for循环运行无限python

尝试找到问题需要5个小时,但无法确定循环for fn in L运行无限的原因.

L=[]
N=int(raw_input())
for i in range(0,N):
    L.append(list(raw_input().split()))
print L

for fn in L:
    if 'insert'==fn[0]:
        L.insert(int(fn[1]),int(fn[2]))
    elif 'append'==fn[0]:
        L.append(int(fn[1]))
    elif 'remove'==fn[0]:
        L.remove(int(fn[1]))
    elif 'pop'==fn[0]:
        L.pop(int(fn[1]))
    elif 'index'==fn[0]:
        L.index(int(fn[1])) 
    elif 'count'==fn[0]:
        L.count(int(fn[1]))
    elif 'sort'==fn[0]:
        L.sort()     
    elif 'reverse'==fn[0]:
        L.reverse() 
    else :
        print  L
Run Code Online (Sandbox Code Playgroud)

提供给列表的输入:

12
insert 0 5
insert 1 10
insert 0 6
print 
remove 6
append 9
append 1
sort 
print
pop
reverse
print
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
112
查看次数

加入特定索引的列表

我有一个如下列表:

ingredients = ['apple','cream','salt','sugar','cider']
Run Code Online (Sandbox Code Playgroud)

我想加入此列表以获取字符串,但我想从第二个索引加入到最后一个索引.

得到这个:"salt sugar cider" 列表的长度可能会有所不同.

是否可以使用join函数执行此操作,或者我必须通过循环元素来执行此操作?

python django list python-2.7

1
推荐指数
1
解决办法
2898
查看次数

Python - 根据数组值将数组拆分为多个数组

我有一个列表需要拆分成多个不同大小的列表.原始列表中的值随大小逐渐增加,直到分割点,其中值在继续增加之前下降.拆分后,值必须保持有序.

例如原始列表

[100, 564, 572, 578, 584, 590, 596, 602, 608, 614, 620, 625, 631, 70, 119, 
125, 130, 134, 139, 144, 149, 154, 159, 614, 669, 100, 136, 144, 149, 153, 
158, 163, 167, 173, 179, 62, 72, 78, 82, 87, 92, 97, 100, 107, 112, 117, 
124, 426, 100, 129, 135, 140, 145, 151]
Run Code Online (Sandbox Code Playgroud)

拆分后:

[100, 564, 572, 578, 584, 590, 596, 602, 608, 614, 620, 625, 631]
[70, 119, 125, 130, 134, 139, 144, 149, 154, 159, …
Run Code Online (Sandbox Code Playgroud)

python arrays split numpy

1
推荐指数
1
解决办法
2622
查看次数

python list操作来修改列表形状

在这种情况下,np是numpy.

z_mesh是形状41*401的数组.

我需要从[2*i,2*j + 2]元素和[2*i,2*j]元素中减去[2*i,2*j]个元素,形状为20*200的dz_theta_mat和dz_gamma_mat分别为[2*i + 2,2*j]个元素.

我的尝试没有为列表提供所需的形状.

我如何修改它以获得所需的结果?

print np.shape(z_mesh)
dz_gamma_mat = [z_mesh[2*i,2*j+2]-z_mesh[2*i,2*j] for i,j in itertools.product(range(20),range(199))]
print np.shape(fish_dz_gamma_mat)
dz_theta_mat = [z_mesh[2*i+2,2*j]-z_mesh[2*i,2*j] for i, j in itertools.product(range(19),range(200))]
print np.shape(fish_dz_theta_mat)

(41, 401)
(3980,)
(3800,)
Run Code Online (Sandbox Code Playgroud)

python numpy

1
推荐指数
1
解决办法
280
查看次数

Python脚本的可变参数数量

运行我的Python脚本的命令行是:

./parse_ms.py inputfile 3 2 2 2 
Run Code Online (Sandbox Code Playgroud)

参数是输入,数字3是我的研究样本的数量,每个样本有2个人.

在脚本中,我指出参数如下:

inputfile = open(sys.argv[1], "r")
nsam = int(sys.argv[2])
nind1 = int(sys.argv[3])
nind2 = int(sys.argv[4])
nind3 = int(sys.argv[5])
Run Code Online (Sandbox Code Playgroud)

但是,样本数可能会有所不同.我可以有:

./parse_ms.py input 4 6 8 2 20
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我有4个样本,每个样本分别有6,8,2和20个人.

添加另一个sys.argv样本的所有内容似乎效率低下.有没有办法让这更通用?也就是说,如果我写入nsam等于5,则自动地,Python除了每个样本中的个体之外的五个数字.

python input

1
推荐指数
1
解决办法
2085
查看次数

使用 numpy 对特定范围内的数组值求和

因此,我尝试使用以下方法获取文本文件中特定范围值的总和:

np.sum(d[a:b])
Run Code Online (Sandbox Code Playgroud)

我正在使用一个包含 10000 个条目的文本文件。我知道我们总是从零开始。所以我的范围很大,即;指数200-555(包括200和555)。我只是尝试在小范围内测试求和:

In [17]: np.sum(d[1:4])
Out[17]: 50.164228
Run Code Online (Sandbox Code Playgroud)

但上面的代码从第二个块(Python 标记为数字 1)到第三个块求和。数字是;(0-> 13.024)、1-> 17.4529、2-> 16.9382、3-> 15.7731、(4-> 11.7589)、5-> 14.5178。

零仅供参考,它忽略了 4th-> 11.7589。为什么?

python numpy scientific-computing python-3.x

1
推荐指数
1
解决办法
5500
查看次数

Python向后遍历索引变量

我在Python 2.7中有一段非常简单的代码,我必须使用a while loop来向后遍历索引.我正在打印出它向后的打印件,但是我while loop不会在最后停止因此产生超出范围的错误,我不知道为什么.我试图解决它但失败了.

这是我的代码:

fruit = 'banana'
print len(fruit)

index = 0
while index <= len(fruit):
    letter = fruit[index - 1]
    print letter
    index = index - 1
Run Code Online (Sandbox Code Playgroud)

我认为是怎么回事是我初始化VAR index0,然后问蟒蛇用var工作fruit而指数小于或等于果实的大小.问题是当索引变为0时,我也尝试使用<但我编写代码的方式似乎仍然超过0,但我不确定.

python while-loop python-2.7

0
推荐指数
1
解决办法
788
查看次数

Python中的Str Argument

代码

def main(bar):
    bar = str(bar)
    print bar
main(sys.argv[1:])
Run Code Online (Sandbox Code Playgroud)

版画

['bar']
Run Code Online (Sandbox Code Playgroud)

而不仅仅是

bar
Run Code Online (Sandbox Code Playgroud)

.我该怎么做才能使输入参数完全符合字符串?谢谢.(Python 2.5.2)

python

0
推荐指数
1
解决办法
147
查看次数

如何在Python中拆分字符串列表

说我有一个像这样的字符串列表:

list = ["Jan 1", "John Smith", "Jan 2", "Bobby Johnson"]
Run Code Online (Sandbox Code Playgroud)

如何将它们分成两个单独的列表?我的老师提到了关于索引的一些内容,但没有很好地解释它

li1 = ["Jan 1", "John Smith"]
li2 = ["Jan 2", "Bobby Johnson"]
Run Code Online (Sandbox Code Playgroud)

python list python-3.x

0
推荐指数
1
解决办法
81
查看次数