私信  •  关注

catasaurus

catasaurus 最近创建的主题
catasaurus 最近回复了
3 年前
回复了 catasaurus 创建的主题 » Python-Modify for循环变量

如果你想要一个简短的答案,这也很有效:

numbers = [3, 4, 6, 7]
[print(x, 5-x, 5+x, 5*x) for x in map(lambda x: x + 2, numbers)]

输出:

5 0 10 25
6 -1 11 30
8 -3 13 40
9 -4 14 45
3 年前
回复了 catasaurus 创建的主题 » python中的数字反转

如果一个数字是正的或负的,则反转一个数字不是切换。它把一小部分颠倒过来。每个数都是一对一的数,所以你只需要用一除以每个数。这应该做到:

import numpy as np
array = np.array([[-0.16759927, -0.04342834,  0.9848982 ],
       [-0.45425025,  0.47425876,  0.75414544],
       [ 0.14613204, -0.19651204,  0.96955064],
       [ 0.55392107,  0.80368964,  0.21738078],
       [-0.13777969, -0.14350102,  0.98001235],
       [-0.00225069,  0.00356328,  0.99999112]])
new_array = np.array([1/i for i in array])

价值 new_array :

array([[  -5.9666131 ,  -23.0264385 ,    1.01533336],
       [  -2.20142972,    2.10855357,    1.32600417],
       [   6.84312626,   -5.08874673,    1.03140564],
       [   1.80531136,    1.2442614 ,    4.60022271],
       [  -7.25796378,   -6.96859158,    1.0203953 ],
       [-444.30818993,  280.64030893,    1.00000888]])

但如果你只是想要你所描述的,这应该是可行的:

import numpy as np
array = np.array([[-0.16759927, -0.04342834,  0.9848982 ],
       [-0.45425025,  0.47425876,  0.75414544],
       [ 0.14613204, -0.19651204,  0.96955064],
       [ 0.55392107,  0.80368964,  0.21738078],
       [-0.13777969, -0.14350102,  0.98001235],
       [-0.00225069,  0.00356328,  0.99999112]])
new_array = -array

价值 新_阵列 :

array([[ 0.16759927,  0.04342834, -0.9848982 ],
       [ 0.45425025, -0.47425876, -0.75414544],
       [-0.14613204,  0.19651204, -0.96955064],
       [-0.55392107, -0.80368964, -0.21738078],
       [ 0.13777969,  0.14350102, -0.98001235],
       [ 0.00225069, -0.00356328, -0.99999112]])
3 年前
回复了 catasaurus 创建的主题 » 如何将字典中的一些键分组到python中的嵌套键?

这应该管用( info 是您在上面定义的列表):

for d in range(len(info)):
    info[d]['Applicant'] = [{'Applicant_Income.1': info[d]['Applicant_Income.1'], 'Applicant_Occupation.1': info[d]['Applicant_Occupation.1']}, {'Applicant_Income.2': info[d]['Applicant_Income.2'], 'Applicant_Occupation.2': info[d]['Applicant_Occupation.2']}]
    del info[d]['Applicant_Income.1']
    del info[d]['Applicant_Occupation.1']
    del info[d]['Applicant_Income.2']
    del info[d]['Applicant_Occupation.2']

输出:

[{'LoanID': '002',
  'LoanAmount': 10000,
  'Applicant': [{'Applicant_Income.1': 5000,
    'Applicant_Occupation.1': 'Manager'},
   {'Applicant_Income.2': 3000, 'Applicant_Occupation.2': 'Accountant'}]},
 {'LoanID': '002',
  'LoanAmount': 8000,
  'Applicant': [{'Applicant_Income.1': 6000,
    'Applicant_Occupation.1': 'Senior Manager'},
   {'Applicant_Income.2': 2000, 'Applicant_Occupation.2': 'Services'}]}]