将列表分为两个列表,一个从最小到最大,另一个从最大到最小

时间:2018-04-18 03:08:01

标签: python

我有两个列表,如下所示;

phi= [0,pi/4, pi/2, 3*pi/4, pi, 5*pi/4, 3*pi/2, 7*pi/4, 2*pi]
t = [1,1.25,1.50,1.25,1,0.75,0.5,0.5,0.75]

我从计算t列表中得到phi。所以phi = 0给了我t = 1等等。我想要两个来自t的列表和两个来自phi的列表。第一个列表将从t的最小值(在倒数第二个位置)开始到最大值tphi列表将是与这些新phi值相关联的t值。 t值的第二个列表将从最大t值开始,并以最小值结束。第二个phi列表应该是与此phi值列表关联的t值。有没有办法对此进行编码?

期望的输出:

t1 = [0.5,0.75,1,1.25,1.5];    phi1 = [7*pi/4, 2*pi, 0, pi/4, pi/2]   
t2 = [1.50,1.25,1,0.75,0.5,];  phi2 = [pi/2, 3*pi/4, pi, 5*pi/4, 3*pi/2]        

2 个答案:

答案 0 :(得分:0)

你的phi列表令人困惑,而且没有正确的python对象形式,更正它,如果我正确,那么你想要这样的东西,

[(0.5, 0.7853981633974483), (0.5, 5), (0.75, 3), (0.75, 3.141592653589793), (1, 0), (1, 0.7853981633974483), (1.25, 0.7853981633974483), (1.25, 3), (1.5, 1.5707963267948966)]
#First list will start from the minimum value of t(at second last position) to maximum value of t.The phi list will be the phi values associated with those new t values


[(1.5, 1.5707963267948966), (1.25, 3), (1.25, 0.7853981633974483), (1, 0.7853981633974483), (1, 0), (0.75, 3.141592653589793), (0.75, 3), (0.5, 5), (0.5, 0.7853981633974483)]
#Second list of t values will start from the maximum t value and end at the minimum. second phi list should be the phi values associated with this t value lists.

输出:

keyArray = Array(chosenObject!.keys)

如果修改后的列表与您的列表不一致,您可以更改您的phi列表。

答案 1 :(得分:0)

从以下数据开始:

phi= ['0','pi/4', 'pi/2','3*pi/4', 'pi','5*pi/4','3*pi/2','7*pi/4', '2*pi']
t = [1,1.25,1.50,1.25,1,0.75,0.5,0.5,0.75]

您可以获取最大值和最小值的索引numpy.argmax()numpy.argmin(),它返回给定列表或数组中最大值或最小值的索引(请注意,如果是多次出现时,它返回 first 极值的索引。因此:

max_index = np.argmax(t)
min_index = np.argmin(t)
# the +1 in some indexes is to have pi/2 and 3pi/2 in both phi1 and phi2
phi1 = phi[min_index:]+phi[:max_index+1]
t1 = t[min_index:]+t[:max_index+1]
phi2 = phi[max_index:min_index+1]
t2 = t[max_index:min_index+1]
# Out: t1, phi1
# [0.5, 0.5, 0.75, 1, 1.25, 1.5] ['3*pi/2', '7*pi/4', '2*pi', '0', 'pi/4', 'pi/2']
# t2,phi2
# [1.5, 1.25, 1, 0.75, 0.5] ['pi/2', '3*pi/4', 'pi', '5*pi/4', '3*pi/2']

使用numpy可能过度,在这种情况下,可以使用内置函数定义max_indexmin_index

max_val = max(t)
min_val = min(t)
max_index = t.index(max_val)
min_index = t.index(min_val)

检索相同的索引,因此输出相同。