【Python】rangeとenumerate rangeをfor文のイテラブルとして使用した時 enumerateをfor文のイテラブルとして使用した時 それぞれの違いについて見ていこう。 ソースコード range_enumerate · ki-hi-ro/python@8b3c8c0 for i in range(10): print(i) for index, value in enumerate(['a', 'b', 'c', 'd']): print(f"Index: {index}, Value: {value}") range(10)を使用した場合と、文字列のリストに対してenumerateを使用した場合のソースコード。 出力結果 0 1 2 3 4 5 6 7 8 9 Ind… Python
【Python】mapはfor文で使用すると消費される リストの中身を一括で型変換できるmap。 そのmapオブジェクトをfor文のイテラブルとして使用すると消費されてしまう。 どういうことか?見ていこう。 ソースコード map.py · ki-hi-ro/python@92037c1 def print_ints(int_iterable): for x in int_iterable: print(x) print("消費された後のリスト:", list(int_iterable)) str_list = ['1', '2', '3', '4', '5'] int_map = map(int, str_list) print_ints(int_map) int_list = [int(x… Python
【Python】pop リストの要素を削除することのできるpopメソッドについて見ていこう。 サンプルコード pop · ki-hi-ro/python@63f6f86 sample_list = [1, 2, 3, 4, 5] # 何も指定しない場合、最後の要素が削除される popped_element = sample_list.pop() print("Popped element:", popped_element) print("List after popping the last element:", sample_list) # インデックスを指定して要素を削除 popped_element_at_index = sample_list.pop(1)… Python
【Python】集合の対称差演算子 集合A ^ 集合Bで、集合Aと集合Bの差の集合ができる。 ソースコード symmetric_difference.py · ki-hi-ro/python@2f5ca39 set_a = {1, 2, 3, 4, 5} set_b = {2, 3} symmetric_diff = set_a ^ set_b print("Symmetric difference between set_a and set_b:", symmetric_diff) symmetric_diffに集合の対称差が入る。 実行結果 Symmetric difference between set_a and set_b: {1, 4, 5} set_aとset_bの対称差は… Python
【Python】リスト2つを比較して、もう一方のリストに存在しない要素のリストを作成する a_listとb_listがある。 a_listにあって、b_listにない要素のリストを作成する。 ソースコード not_in_others_list.py · ki-hi-ro/python@f07e63c a_list = ['apple', 'banana', 'cherry', 'date'] b_list = ['banana', 'date', 'fig', 'grape'] not_in_b_list = [item for item in a_list if item not in b_list] print("Items in a_list not in b_list:", not_in_b_list) no… Python
【Python】indexメソッド indexメソッドは、リストやタプルの中で、指定した要素が最初に現れる位置(インデックス)を返すメソッド。 サンプルコード python/index.py at main · ki-hi-ro/python some_list = [1, 2, 3, 4, 5] index = some_list.index(3) print(f"The index of 3 in the list is: {index}") some_tuple = (10, 20, 30, 40, 50) index = some_tuple.index(40) print(f"The index of 40 in the tuple is: {index}") 以下のコ… Python プログラミング学習
【Python】現在の座標をzipで更新する 二次元配列の座標空間で、現在地からある量だけ移動するというプログラムを作成した。 from pprint import pprint two_d_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] pprint(two_d_list, width=20) my_position = (2, 2) # (row, column) print(two_d_list[my_position[0]][my_position[1]]) move_direction = (0, -1) # (row_change, column_change) my_moved_po… Python
splitは、空白区切りの文字列をリストにすることができる。 print('apple banana'.split()) python/split.py at main · ki-hi-ro/python 出力結果 ['apple', 'banana'] Python
縦の出力を横にする 最終的に以下の形にしたい。 --- 文字列 --- 初期状態 python/vertical-to-horizontal.py at e39fcd46e15772908b2ab387be7bf7a81565e701 · ki-hi-ro/python my_str = 'sanjusangendo' for _ in range(len(my_str)): print('-') print(my_str) for _ in range(len(my_str)): print('-') 出力結果 - - - - - - - - - - - - - sanjusangendo - - - - - - - - - - - - - 意図した出力になった pyt… Python プログラミング学習
sorted関数で、アナグラム判定 アナグラムとは、ある文字列が別の文字列の並べ替えということ。 ソースコード python/anagram.py at 39878c3c122d01bea78aa5276fb307879e29c975 · ki-hi-ro/python def is_anagram(a, b): if sorted(a) == sorted(b): print(a + 'は、' + b + 'のアナグラム') else: print(a + 'は、' + b + 'のアナグラムではない') is_anagram('listen', 'silent') is_anagram('cat', 'cats') 出力結果 listenは、silentのアナグラム catは、cat… Python プログラミング学習