swap elements of list in recursive call python - python

I want to make simple function swap random element in list.
but it doesn't work in recursive call.
in first recursive call, element swapping work,
but nested recursive call(or nested recursive call in first recursive call) doesn't work.
I don't know why only swap in first recursive call works.
below are result.
Thank you all.
def change(lst):
if len(lst)>4:
a, b = np.random.randint(0, len(lst)), np.random.randint(0, len(lst))
print(lst)
lst[a], lst[b] = lst[b], lst[a]
print(lst)
mid = int(len(lst)/2)
change(lst[:mid])
change(lst[mid:])
k = list(range(0, 20))
change(k)
print(k)
`
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 19, 2, 0, 4, 5, 6, 7, 8, 9]
[3, 19, 2, 0, 4]
[3, 0, 2, 19, 4]
[5, 6, 7, 8, 9]
[5, 6, 8, 7, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[10, 11, 12, 13, 14]
[10, 14, 12, 13, 11]
[15, 16, 17, 18, 1]
[15, 16, 17, 18, 1]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1] <= result.

The problem is that in your recursive calls:
change(lst[:mid])
change(lst[mid:])
you use a slicing operator. The slicing operator constructs a new list, so your changes are made on a new list and are not reflected on the original list (since it is a copy).
What you can do is use indices instead:
def change(lst,frm=0,to=None):
if to is None: # set the default to the end of the list
to = len(lst)
if to-frm > 4:
a, b = np.random.randint(frm,to), np.random.randint(frm,to)
print(lst)
lst[a], lst[b] = lst[b], lst[a]
print(lst)
mid = (frm+to)//2
change(lst,frm,mid)
change(lst,mid,to)
Then we obtain:
>>> k = list(range(0, 20))
>>> change(k)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
>>> print(k)
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]

That's because you create copies of the original list by lst[:mid], lst[mid:]. A solution is to pass to change() the same list and (separately) the range to process.

Related

matplotlib quiver() displaying double arrows

The code below is producing double arrows. This is most noticeable in the center and along the bottom row.
Am I missing something or is this a bug of some sort? The Googlebox has yielded nothing helpful.
X = [[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22]]
Y = [[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2],
[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3],
[ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4],
[ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5],
[ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6],
[ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7],
[ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10],
[11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11],
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12],
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12],
[13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13],
[14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14],
[15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15],
[16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16],
[17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17],
[18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18],
[19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20],
[21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21],
[22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22],
[23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23]]
U = [[ 5.91106782e-01, 6.22366562e-01, 6.49723913e-01,
6.58423221e-01, 6.34788082e-01, 5.64001424e-01,
4.29552877e-01, 2.26181450e-01, -1.45749370e-02,
-2.33836090e-01, -3.97062982e-01, -5.07288787e-01,
-5.80344621e-01, -6.28652118e-01, -6.56798746e-01,
-6.59563028e-01, -6.18178096e-01, -5.00722193e-01,
-2.93582966e-01, -5.16521582e-02, 1.45799368e-01,
2.69658133e-01, 3.26500153e-01, 3.26768709e-01],
[ 5.77152607e-01, 5.82746773e-01, 5.76183972e-01,
5.43266956e-01, 4.66312191e-01, 3.24661378e-01,
1.07385088e-01, -1.54908642e-01, -3.91341641e-01,
-5.56849441e-01, -6.55288824e-01, -7.07393105e-01,
-7.30393353e-01, -7.34785105e-01, -7.24202206e-01,
-6.92703347e-01, -6.16664334e-01, -4.51489078e-01,
-1.96144234e-01, 3.89095593e-02, 1.80222779e-01,
2.38107484e-01, 2.33390450e-01, 1.75753839e-01],
[ 5.55201554e-01, 5.36916267e-01, 4.95331092e-01,
4.14297782e-01, 2.72441471e-01, 5.30267270e-02,
-2.23958353e-01, -4.85686407e-01, -6.72063827e-01,
-7.80930667e-01, -8.34052999e-01, -8.49154520e-01,
-8.36753045e-01, -8.02912499e-01, -7.48984464e-01,
-6.66861836e-01, -5.28032125e-01, -2.83307017e-01,
2.22999217e-02, 2.17035214e-01, 2.78107345e-01,
2.57213982e-01, 1.80414526e-01, 5.58040480e-02],
[ 5.30514869e-01, 4.88045325e-01, 4.08507605e-01,
2.73305715e-01, 6.32261747e-02, -2.15499555e-01,
-5.00468423e-01, -7.16424820e-01, -8.46362641e-01,
-9.11763344e-01, -9.32463829e-01, -9.17913774e-01,
-8.71699781e-01, -7.94959450e-01, -6.84618047e-01,
-5.27977847e-01, -3.00601184e-01, 3.24725025e-03,
2.84572480e-01, 4.15678719e-01, 4.08817699e-01,
3.17903989e-01, 1.68926905e-01, -2.47734503e-02],
[ 5.02548139e-01, 4.33179379e-01, 3.10308116e-01,
1.15832201e-01, -1.53873192e-01, -4.53740654e-01,
-7.02597717e-01, -8.60992569e-01, -9.43881451e-01,
-9.75369646e-01, -9.68057700e-01, -9.24950942e-01,
-8.45413757e-01, -7.27664488e-01, -5.66705523e-01,
-3.53336189e-01, -8.54415234e-02, 2.02537484e-01,
4.30399404e-01, 5.35069052e-01, 5.14397103e-01,
3.91608994e-01, 1.87450108e-01, -6.90256334e-02],
[ 4.65201575e-01, 3.63695264e-01, 1.90407737e-01,
-6.27986111e-02, -3.66107539e-01, -6.43527995e-01,
-8.36008710e-01, -9.43058071e-01, -9.89396085e-01,
-9.93329532e-01, -9.61559805e-01, -8.93929627e-01,
-7.88224158e-01, -6.41454775e-01, -4.49066265e-01,
-2.09439702e-01, 6.10838713e-02, 3.17493844e-01,
5.06540308e-01, 5.97919655e-01, 5.83094325e-01,
4.57218933e-01, 2.21129548e-01, -8.56001665e-02],
[ 4.07605489e-01, 2.67483222e-01, 4.05687053e-02,
-2.54052862e-01, -5.49121397e-01, -7.74824152e-01,
-9.12683654e-01, -9.80602627e-01, -9.99708951e-01,
-9.81982007e-01, -9.30930539e-01, -8.45440583e-01,
-7.22549396e-01, -5.57873670e-01, -3.47081454e-01,
-9.50528305e-02, 1.70316784e-01, 4.02869279e-01,
5.66564552e-01, 6.47406192e-01, 6.37165072e-01,
5.16241932e-01, 2.63738110e-01, -8.10456144e-02],
[ 3.16982526e-01, 1.36224084e-01, -1.31317283e-01,
-4.28869103e-01, -6.80165940e-01, -8.50494619e-01,
-9.46930042e-01, -9.87780174e-01, -9.87413002e-01,
-9.53497923e-01, -8.88237499e-01, -7.90275139e-01,
-6.55616984e-01, -4.77864944e-01, -2.52458286e-01,
9.81222811e-03, 2.71633461e-01, 4.87237886e-01,
6.32706314e-01, 7.04459838e-01, 6.96319353e-01,
5.81475147e-01, 3.21122831e-01, -5.36729232e-02],
[ 1.89851174e-01, -1.85511352e-02, -2.92612142e-01,
-5.56206782e-01, -7.55569048e-01, -8.83561174e-01,
-9.52641527e-01, -9.75602026e-01, -9.61454612e-01,
-9.15325308e-01, -8.38796591e-01, -7.30116952e-01,
-5.83859882e-01, -3.91508312e-01, -1.49024508e-01,
1.25224816e-01, 3.84250312e-01, 5.84434393e-01,
7.13492616e-01, 7.76382012e-01, 7.69585148e-01,
6.63495229e-01, 4.05530281e-01, 8.00655892e-03],
[ 4.93393011e-02, -1.59077638e-01, -4.07446556e-01,
-6.27159359e-01, -7.86840781e-01, -8.87988595e-01,
-9.40149757e-01, -9.51231448e-01, -9.26802697e-01,
-8.70250066e-01, -7.82391229e-01, -6.60618012e-01,
-4.97916636e-01, -2.84557428e-01, -2.01313492e-02,
2.65144709e-01, 5.15478531e-01, 6.95092413e-01,
8.05054050e-01, 8.57357453e-01, 8.51220724e-01,
7.58939184e-01, 5.22126402e-01, 1.20841224e-01],
[-6.08161695e-02, -2.48600589e-01, -4.63433277e-01,
-6.50364713e-01, -7.86735731e-01, -8.73048406e-01,
-9.15192518e-01, -9.18037801e-01, -8.84901395e-01,
-8.17468539e-01, -7.15217876e-01, -5.74265632e-01,
-3.86590763e-01, -1.44298331e-01, 1.42640838e-01,
4.27841719e-01, 6.54596037e-01, 8.04151106e-01,
8.90457558e-01, 9.29136623e-01, 9.21288722e-01,
8.45538031e-01, 6.51467861e-01, 2.89354206e-01],
[-1.11855856e-01, -2.76262916e-01, -4.66494984e-01,
-6.36195917e-01, -7.62707033e-01, -8.42791922e-01,
-8.79618811e-01, -8.76344309e-01, -8.34510258e-01,
-7.53704299e-01, -6.31437753e-01, -4.62760383e-01,
-2.41283252e-01, 3.19831190e-02, 3.28392442e-01,
5.90402526e-01, 7.76991386e-01, 8.90488023e-01,
9.51679598e-01, 9.75365138e-01, 9.63233139e-01,
9.01562554e-01, 7.57051594e-01, 4.78793525e-01],
[-1.02763752e-01, -2.49208456e-01, -4.25698141e-01,
-5.90445663e-01, -7.17037110e-01, -7.97411218e-01,
-8.32440059e-01, -8.24121526e-01, -7.72259450e-01,
-6.73888301e-01, -5.24476588e-01, -3.20200623e-01,
-6.29603397e-02, 2.27897175e-01, 5.06403936e-01,
7.23466314e-01, 8.64328407e-01, 9.44675955e-01,
9.84485357e-01, 9.95247332e-01, 9.78583745e-01,
9.26839981e-01, 8.22017325e-01, 6.32198023e-01],
[-4.76297792e-02, -1.80235400e-01, -3.48835361e-01,
-5.15498990e-01, -6.48860867e-01, -7.34607790e-01,
-7.70573611e-01, -7.57371172e-01, -6.92888223e-01,
-5.71674412e-01, -3.88941513e-01, -1.47948226e-01,
1.31928638e-01, 4.12166625e-01, 6.47847253e-01,
8.14343231e-01, 9.16397002e-01, 9.71963635e-01,
9.96490008e-01, 9.98129614e-01, 9.78410399e-01,
9.33624383e-01, 8.55392368e-01, 7.29066548e-01],
[ 3.44453327e-02, -8.45500183e-02, -2.44567678e-01,
-4.13390951e-01, -5.55986626e-01, -6.50278759e-01,
-6.88905719e-01, -6.69887784e-01, -5.89049719e-01,
-4.40034407e-01, -2.23153205e-01, 4.24368019e-02,
3.17706147e-01, 5.60575371e-01, 7.45372080e-01,
8.69074149e-01, 9.43218293e-01, 9.82237250e-01,
9.96859104e-01, 9.92775230e-01, 9.71548772e-01,
9.31926675e-01, 8.70952635e-01, 7.84147337e-01],
[ 1.24531829e-01, 2.14951720e-02, -1.24142269e-01,
-2.88285785e-01, -4.36332137e-01, -5.38600278e-01,
-5.79503157e-01, -5.52137571e-01, -4.50556205e-01,
-2.72020914e-01, -3.08257107e-02, 2.33881288e-01,
4.76396240e-01, 6.69624907e-01, 8.08474102e-01,
9.00042790e-01, 9.55065954e-01, 9.83283570e-01,
9.91629328e-01, 9.84084754e-01, 9.62320925e-01,
9.26691302e-01, 8.77415997e-01, 8.15482847e-01],
[ 2.08997618e-01, 1.24559020e-01, 8.48684407e-04,
-1.46598137e-01, -2.88661918e-01, -3.91618296e-01,
-4.29961569e-01, -3.89309376e-01, -2.63943288e-01,
-6.30136416e-02, 1.78668276e-01, 4.12249396e-01,
6.04758495e-01, 7.48714864e-01, 8.50152685e-01,
9.17783980e-01, 9.59112226e-01, 9.79804775e-01,
9.83901943e-01, 9.74114572e-01, 9.52212374e-01,
9.19706766e-01, 8.78912089e-01, 8.33903997e-01],
[ 2.82249691e-01, 2.18162184e-01, 1.22747194e-01,
5.37550368e-03, -1.12692673e-01, -1.99604825e-01,
-2.22963823e-01, -1.62177004e-01, -1.79584651e-02,
1.81399471e-01, 3.89284568e-01, 5.68595908e-01,
7.06857534e-01, 8.07907267e-01, 8.79716132e-01,
9.28814854e-01, 9.59433518e-01, 9.74181247e-01,
9.74926863e-01, 9.63345918e-01, 9.41247109e-01,
9.11068122e-01, 8.76727946e-01, 8.44239721e-01],
[ 3.46009504e-01, 3.03334542e-01, 2.40609727e-01,
1.64792336e-01, 9.10650256e-02, 4.46918062e-02,
5.47680721e-02, 1.36260477e-01, 2.74393229e-01,
4.32734135e-01, 5.78349795e-01, 6.96327399e-01,
7.86097123e-01, 8.52735284e-01, 9.01501863e-01,
9.35893510e-01, 9.57589073e-01, 9.67107013e-01,
9.64733890e-01, 9.51264330e-01, 9.28488288e-01,
8.99692809e-01, 8.70252069e-01, 8.47561147e-01],
[ 4.06955125e-01, 3.86836289e-01, 3.60008319e-01,
3.33800629e-01, 3.19793639e-01, 3.32294319e-01,
3.80910459e-01, 4.60529276e-01, 5.54085368e-01,
6.45433976e-01, 7.25560042e-01, 7.91585217e-01,
8.44113209e-01, 8.85113897e-01, 9.16480279e-01,
9.39239758e-01, 9.53365969e-01, 9.58045905e-01,
9.52421824e-01, 9.36509635e-01, 9.12084965e-01,
8.83417478e-01, 8.57475919e-01, 8.42768229e-01],
[ 4.75102487e-01, 4.79444876e-01, 4.91103049e-01,
5.17028392e-01, 5.61580652e-01, 6.19364889e-01,
6.75394533e-01, 7.20462603e-01, 7.57762648e-01,
7.92301360e-01, 8.25331968e-01, 8.55937836e-01,
8.83019438e-01, 9.05962858e-01, 9.24460078e-01,
9.38025098e-01, 9.45636106e-01, 9.45638264e-01,
9.36251692e-01, 9.16741704e-01, 8.89028586e-01,
8.58882429e-01, 8.35359426e-01, 8.27878351e-01],
[ 5.62693753e-01, 5.93592734e-01, 6.42845488e-01,
7.09881932e-01, 7.82737342e-01, 8.39735853e-01,
8.68496463e-01, 8.76464004e-01, 8.77560033e-01,
8.80145097e-01, 8.86486243e-01, 8.95793855e-01,
9.06385360e-01, 9.16670844e-01, 9.25375659e-01,
9.31289596e-01, 9.32857010e-01, 9.27783629e-01,
9.13322352e-01, 8.87929386e-01, 8.54220866e-01,
8.20792210e-01, 7.99707058e-01, 8.00713493e-01],
[ 6.75223956e-01, 7.28299137e-01, 7.98132045e-01,
8.70828514e-01, 9.28100696e-01, 9.58357163e-01,
9.63664076e-01, 9.54511170e-01, 9.41109289e-01,
9.29563623e-01, 9.22166117e-01, 9.18823138e-01,
9.18280235e-01, 9.18945445e-01, 9.19301773e-01,
9.17852346e-01, 9.12687865e-01, 9.00755102e-01,
8.77918913e-01, 8.41778780e-01, 7.97438172e-01,
7.59412209e-01, 7.43766496e-01, 7.58426057e-01],
[ 7.48028585e-01, 8.00967465e-01, 8.60639839e-01,
9.17314355e-01, 9.62374525e-01, 9.89077372e-01,
9.96239777e-01, 9.88737117e-01, 9.73767736e-01,
9.57273913e-01, 9.42647501e-01, 9.30980153e-01,
9.21800247e-01, 9.13829662e-01, 9.05516974e-01,
8.95103386e-01, 8.80063037e-01, 8.55819040e-01,
8.15680432e-01, 7.57286053e-01, 6.94667063e-01,
6.54674360e-01, 6.55242773e-01, 6.95951505e-01],
[ 5.88142228e-01, 6.56524283e-01, 7.39388262e-01,
8.26540072e-01, 9.05382649e-01, 9.63084972e-01,
9.93428420e-01, 9.99593348e-01, 9.90003898e-01,
9.72940215e-01, 9.53944608e-01, 9.35599844e-01,
9.18217361e-01, 9.00665492e-01, 8.80954002e-01,
8.56210996e-01, 8.21553193e-01, 7.67628947e-01,
6.82523479e-01, 5.74145986e-01, 4.89341942e-01,
4.69017002e-01, 5.14459717e-01, 6.05048760e-01]]
V = [[-7.64071106e-01, -7.61939824e-01, -7.56999777e-01,
fig, ax = plt.subplots(figsize=(6, 6), dpi=300);
ax.quiver(X, Y, U, V, pivot='middle');
ax.set_aspect('equal');
Output:
I've tried the above. I expect single arrows as usual.
I don't know what additional details to add to get rid of the
Didn't you ask for exactly that, repeating values 1 and 12?
#(...)
X = [[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
#(...)
Y = [[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
#(...)
U = [[ 5.91106782e-01, 6.22366562e-01, 6.49723913e-01, (...)
[ 5.77152607e-01, 5.82746773e-01, 5.76183972e-01, (...)
You even have a quadruple arrow at coordinate (1,1). Just pair X and Y, and see that X=1 Y=1 occurs 4 times! But values of U in those 4 occurences of X=1 Y=1 are all different. So those 4 arrows that starts for (1,1) are not exactly identical.
So does X=11 Y=1.
X=2 Y=1 occurs twice.
Etc.
You said "especially in the center". No, not really. It occurs on line Y=1, on column X=1, and, indeed on line Y=12 and column X=11 that both pass not far from the center.

Prevent IPython from displaying long lists one element per line

In Jupyter notebooks, or in IPython, long lists are displayed one element per line. How do I display them on a single line? I don't mind if the line wraps.
In the following example, I'd like the 3rd list to be shown as a "row", not as a "column".
In [1]: [list(range(n)) for n in range(10,40,10)]
Out[1]:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29]]
The output I am looking for is the following or similar:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]]
My goal is to make the output easier to read for humans.
I would simply use
A = [list(range(n)) for n in range(10,40,10)]
for i in A:
print(i)

Update list in python

I'm a fresher in python. I have some troubles with list.
After creating the list, i update it. it's right when I print each element of list in loop wrong , but wrong when I return list: all elements of list are same
def shift(array,n):
for i in range(n):
array.append(array[0])
array.pop(0)
return array
def shiftKey(array):
array_shift = []
for i in range(16):
array_shift.append([])
array_shift[0] = [shift(array[0],1),shift(array[1],1)]
print (array_shift[0])
for j in range(1,16):
if j == 1 or j == 8 or j == 15:
a1 = shift(array_shift[j-1][0],1)
a2 = shift(array_shift[j-1][1],1)
array_shift[j] = [a1,a2]
print (array_shift[j])
else:
a1 = shift(array_shift[j-1][0],2)
a2 = shift(array_shift[j-1][1],2)
array_shift[j] = [a1,a2]
print (array_shift[j])
return array_shift
shiftKey([[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]])
and this's the result expected
[[[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1], [2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]] [[3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 1, 2], [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 1, 2]] [[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2,
3, 4], [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4]] [[7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6]] [[9, 10, 11, 12, 13, 14, 15,
16, 1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3,
4, 5, 6, 7, 8]] [[11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10], [11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]] [[13,
14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]] [[15, 16, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14], [15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14]] [[16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]] [[2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1], [2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 1]] [[4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1, 2, 3]] [[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5]] [[8, 9, 10,
11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13,
14, 15, 16, 1, 2, 3, 4, 5, 6, 7]] [[10, 11, 12, 13, 14, 15, 16, 1, 2,
3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6,
7, 8, 9]] [[12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]] [[13, 14, 15,
16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12]]]
but it's print like below when i print list
[[[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
[[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
[[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12]]]
You need to use deepcopy functionality so you pass a copy of list instead of reference of the list.
Also, you can use the following easy function for rotation:
def shift(input_list, rotate):
return input_list[rotate:] + input_list[:rotate]
I will suggest you read about defaultlist.
I corrected my code and gave an alternate solution, please try this one.
import copy
def shift(array,n):
for i in range(n):
array.append(array[0])
array.pop(0)
return array
def shiftKey(array):
array_shift = []
array_shift.append([shift(array[0],1),shift(array[1],1)])
print (array_shift[0])
for j in range(1,16):
if j == 1 or j == 8 or j == 15:
a1 = shift(copy.deepcopy(array_shift[j-1][0]),1)
a2 = shift(copy.deepcopy(array_shift[j-1][1]),1)
array_shift.append([a1,a2])
print (array_shift[j])
else:
a1 = shift(copy.deepcopy(array_shift[j-1][0]),2)
a2 = shift(copy.deepcopy(array_shift[j-1][1]),2)
array_shift.append([a1,a2])
print (array_shift[j])
return array_shift
shiftKey([[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]])

Matplotlib color assignment error?

I'm trying to make a colorful scatter plot based on data in an array:
plt.scatter(150, 93, c=y_pred)
Here, y_pred is:
array([ 5, 6, 8, 16, 21, 12, 12, 13, 6, 6, 17, 11, 6, 12, 12, 23, 6,
6, 15, 6, 6, 6, 6, 6, 6, 23, 22, 6, 12, 17, 6, 20, 0, 6,
6, 12, 12, 0, 6, 6, 6, 6, 6, 6, 5, 17, 6, 6, 11, 10, 13,
6, 22, 24, 23, 6, 6, 13, 6, 6, 6, 12, 9, 15, 13, 14, 6, 18,
1, 6, 9, 6, 6, 11, 6, 5, 16, 9, 23, 2, 14, 24, 9, 5, 9,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 6, 19, 6, 23, 3,
20, 10, 4, 8, 9, 6, 6, 9, 22, 23, 6, 6, 11, 6, 6, 6, 22,
24, 14, 4, 7, 12, 6, 19, 6, 12, 3, 22, 6, 11, 6, 21, 23, 4,
6, 6, 6, 4, 10, 22, 15, 6, 6, 18, 6, 14, 4, 5], dtype=int32)
This gives me an error:
ValueError: Invalid RGBA argument: 17
I don't understand why. The same solution works for others. Could you help me understand the error?
You only add one scatter point at x=150 ; y=93 but you try to assign 150 colors for this one value.
plt.scatter(150, 93)
It works if you pass in x and y that have the same shape as c:
plt.scatter(np.random.random(150), np.random.random(150), c=y_pred)

Adding each element in an array by each element in the same array in python

I need to add each element in an array by each element in the same array and then add the results to a new array. I have tried
my_list = []
for filename in [my_file]:
with open(filename) as f:
my_list += [int(i) for line in f for i in line.split()]
answer = []
for elem in my_list:
answer += [elem + elem]
print answer
When my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], the program prints [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] which is incorrect. How can I fix this?
You can try this:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
answer = [i+a for i in my_list for a in my_list]
Output:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Categories