Py学习  »  Nin17  »  全部回复
回复总数  5

你可以通过列表理解和numpy来做到这一点:

import numpy as np
a = np.array([1500, 1520, 1540, 1590, 1590, 1600, 1600, 1560, 1560, 1560, 1580,
              1520, 1460, 1510, 1520, 1320, 1320, 1300, 1300, 1320, 1320, 1320, 1320,
              1300, 1300, 1340, 1300, 1300, 1300, 1400, 1480, 1360, 1420, 1480,
              1580, 1530, 1500, 1480, 1480, 1480, 1460, 1540, 1490, 1480, 1480,
              1520, 1500, 1460, 1480, 1480, 1500, 1500, 1600, 1540, 1480, 1460,
              1560, 1600, 1560, 1600, 1600, 1600, 1620, 1600, 1580, 1600, 1700,
              1620, 1620, 1620, 1700, 1700, 1680, 1640, 1620, 1670, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1670, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1600, 1680, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0], dtype=np.int64)

np.mean([i for i in a if i])

输出:

1508.4810126582279
3 年前
回复了 Nin17 创建的主题 » 程序:在python中查找word中的大写字母

你可以用 enumerate :

def capital_indexes():
    word =input("enter your word :")
    a = []
    for i, j in enumerate(word):
        if (j.isupper()):
            a.append(i)
            print(j)
    print(a)

capital_indexes()

输出(带有 heLLo 输入):

L
L
[2, 3]

你也可以用列表理解来浓缩这一点:

def capital_indexes():
    word =input("enter your word :")
    a = [i for i, j in enumerate(word) if j.isupper()]
    print(a)

capital_indexes()

输出(再次使用 你好 输入):

[2, 3]
3 年前
回复了 Nin17 创建的主题 » python中如何按某个重复索引拆分列表

你可以通过列表理解来实现:

a = [97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112]
[''.join(chr(j) for j in a[i:i+4]) for i in range(0, len(a), 4)]

输出:

['abcd', 'efgh', 'ijkl', 'mnop']
3 年前
回复了 Nin17 创建的主题 » 在Python中删除一些元素并展平数组

你可以通过列表理解来做到这一点:

import numpy as np
R=np.array([[1.05567452e+11, 1.51583103e+11, 5.66466172e+08],
       [6.94076420e+09, 1.96129124e+10, 1.11642674e+09],
       [1.88618492e+10, 1.73640817e+10, 4.84980874e+09]])

Remove = [(0, 1),(0,2)] 
b = [[j for i, j in enumerate(m) if (k, i) not in Remove] for k, m in enumerate(R)]
R1 = np.array([i for j in b for i in j]) #Flatten the resulting list

print(R1)

输出

array([1.05567452e+11, 6.94076420e+09, 1.96129124e+10, 1.11642674e+09,
       1.88618492e+10, 1.73640817e+10, 4.84980874e+09])
3 年前
回复了 Nin17 创建的主题 » python随机_样本最小值

你可以忽略它们:

n = 0
while n < 10:
    weights = np.random.random_sample(3)
    weights  = weights/ np.sum(weights)
    if any(i < 0.05 for i in weights):
        continue
    n += 1
    print (weights)