LoginSignup
0
1

More than 3 years have passed since last update.

【python】listの中身を左にローテートするプログラム

Posted at

【python】listの中身を左にローテートするプログラム

自分用のメモです。

▼設問

  • listの中身を指定された整数分、左側に回転させる。

URL

▼sample input

a=[1,2,3,4,5]
d=4

▼sample output

[5, 1, 2, 3, 4]

▼my answer

def rotLeft(a, d):
    c = a[d:]
    d = a[0:d]
    return c+d

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    nd = input().split()
    n = int(nd[0])
    d = int(nd[1])
    a = list(map(int, input().rstrip().split()))
    result = rotLeft(a, d)
    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')
    fptr.close()


ハマったこと

extendメソッドでlistを結合したところ、戻り値がNoneになった。

a=[1,2,3,4,5]
c =a[4:]
d =a[0:4]

print(c.extend(d))

#Noneになる。

ググった限り、extendメソッドでも結合できそうだったのに、、

理由がわかりましたら教えていただけると嬉しいです。

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1