0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python

Last updated at Posted at 2024-01-21

#配列
##検索
[find] .find(検索する文字列, 開始位置, 終了位置)
[rfind]…後から
該当文字列が見つかれば0から始まる文字列1を返す
無ければ-1を返す

s = "abcabcabc"
index = s.find("b")

##カウント
[count()]

s = 'aaabbc'
print(s.count('b'))  #=> 2

##置換
[replace]

s = 'Today is Monday.'
ss = s.replace('Monday', 'Sunday')  #=> 'Today is Sunday.'
print(ss)
s2 = 'Hello Hello'
ss2 = s2.replace('Hello', 'Bye')  #=> 'Bye Bye' 第三引数を指定しなければすべて置換される
print(ss2)
s3 = 'World World'
ss3 = s3.replace('World', 'Hello', 1)  #=> 'Hello World' # 第三匹数で置換する個数を指定
print(ss3)

##大文字小文字
[upper()]
[lower()]
[swapcase()]
[capitalize()]

print('hello'.upper())  # => 'HELLO'
print('BIG'.lower())  # => 'big'
print(word.swapcase()) # => abcABC
print(word.capitalize()) # => Abcabc

##何番目か
[index()]

animals = ['dog', 'cat', 'elephant']
animals.index('elephant') # => 2

##結合
[extend]

list8 = [1, 2, 3]
list9 = [4, 5, 6]
list8.extend(list9)
print(list8)
#[1, 2, 3, 4, 5, 6]

[+]

list10 = list8 + list9
print(list10)
#[1, 2, 3, 4, 5, 6]

##要素の削除
[remove()]
引数に削除したい要素の値を指定
リスト内に存在しない値を指定するとエラーが発生する

mylist = [1,2,3,4,5]
mylist.remove(5)
print(mylist)  ## => [1, 2, 3, 4]

[del]
indexを指定して削除

animals = ['dog', 'cat', 'elephant']
del animals[1]
print(animals) # => ['dog', 'elephant']

##スライス
[:]

word = ['p', 'y', 't', 'h', 'o', 'n']
word[0:3] # => ['p', 'y', 't']
word[1:5:2] # => ['y', 'h', 'n']
word[::2] # => ['p', 't', 'o']
word[::-2] # => ['n', 'h', 'y']
word[::-1] # => ['n', 'o', 'h', 't', 'y', 'p'] #リストの要素が逆順になる

##並び替え
[sorted()]

X = [1, 3, 4, 2, 5]
sortX = sorted(X)
print(X) # => [1, 3, 4, 2, 5]
print(sortX) # => [1, 2, 3, 4, 5]

X.sort()
print(X) # => [1, 2, 3, 4, 5]
X.sort(reverse = True) # 降順
print(X) # => [5, 4, 3, 2, 1]

[reversed()]
逆順にする

X = [1, 3, 4, 2, 5]
reX = reversed(X)
print(reX) # => [5, 2, 4, 3, 1]
print(X[:-1]) # => [5, 2, 4, 3, 1]

##重複を取り除く
[set()]
一意な値のみを昇順に並べたlistを返す

l = [3, 3, 2, 1, 5, 1, 4, 2, 3]
l_unique = list(set(l))
print(l_unique)
# [1, 2, 3, 4, 5]

順番を変えたくない時はkeyを指定する

list = ['a', 'b', 'c', 'a']
sorted(set(list), key=list.index)
['a', 'b', 'c']

##重複した値を抽出

l = [3, 3, 2, 1, 5, 1, 4, 2, 3]
l_duplicate = [x for x in set(l) if l.count(x) > 1]
print(l_duplicate)
# [1, 2, 3]

##ZIP
2つ以上のリストの各要素の組(タプル)を作る組み込み関数

zip([1,2,3], ['a', 'b', 'c'])
# => [(1, 'a'), (2, 'b'), (3, 'c')]

#import math
[math.gcd(,)]
最大公約数

math.gcd(3, 15) # => 3

最小公倍数

lcm(a, b) = (a * b) / gcd(a, b)

[log(真数, 底)]

math.log(5)
# eを底とする
math.log10(5)
# 底が10

[pow]

math.pow(x, y) # => x の y 乗

[sqrt]

math.sqrt(x) # => x の平方根

["{:.xf}".format()]
四捨五入して小数点以下の桁数を指定

pi = 3.141592 
print('{:.1f}'.format(pi)) #小数点以下1位
print('{:.2f}'.format(pi)) #小数点以下2位
print('{:.3f}'.format(pi)) #小数点以下3位

#lambda

list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list_2 = filter(lambda x: x % 2 == 0, list_1)  # 偶数を抽出

list_s_1 = ['apple', 'grape', 'banana', 'orange']
list_s_2 = list(filter(lambda x: 'n' in x, list_s_1))  # 'n' を含む要素を抽出
print(list_s_2)  # ['banana', 'orange']

items = [1, 2, 3]
map(lambda x : x + 20, items) # => [21, 22, 23]
[x + 20 for x in items] # => [21, 22, 23]

#import re
##指定語句の置き換え(削除)
文章内の指定した語句をまとめて他の指定語句に置き換える

import re

text1 = "今日はとても暑い。今日はアイスを食べよう。"
text2 = "123456789"

text1 = re.sub('今日', '明日', text1)
# => "明日はとても暑い。明日はアイスを食べよう。"
text2 = re.sub(r'[3-5]', '', text2)
# => "126789" 
0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?