from collections import defaultdict
results = [
(“pass”, “Taro”),
(“pass”, “Jiro”),
(“fail”, “Saburo”),
(“pass”, “Shiro”)]
stats = defaultdict(list)
for result, name in results:
stats[result].append(name)
print(dict(stats))
from collections import defaultdict
results = [
(“pass”, “Taro”),
(“pass”, “Jiro”),
(“fail”, “Saburo”),
(“pass”, “Shiro”)]
stats = defaultdict(list)
for result, name in results:
stats[result].append(name)
print(dict(stats))
from collections import Counter
results = [“pass”, “pass”, “fail”, “pass”, “fail”, “pass”]
stats = Counter(results)
print(dict(stats))
from collections import defaultdict
results = [“pass”, “pass”, “fail”, “pass”, “fail”, “pass”]
stats = defaultdict(int)
for result in results:
stats[result] += 1
print(dict(stats))
results = [“pass”, “pass”, “fail”, “pass”, “fail”, “pass”]
stats = {}
for result in results:
if result not in stats:
stats[result] = 0
stats[result] += 1
print(stats)
from pprint import pprint
scores = [
{“name”: “Taro”, “math”: 70, “english”: 82},
{“name”: “Jiro”, “math”: 67, “english”: 61},
{“name”: “Saburo”, “math”: 81, “english”: 58},
]
pprint(scores)
file_name = “names.txt”
with open(file_name, mode=”r”) as f:
names = f.read().splitlines()
print(names)
file_name = “names.txt”
names = [“Jiro”, “Saburo”, “Shiro”]
with open(file_name, mode=”a”) as f:
for name in names:
f.write(f”{name}\n”)
import os
file_name = “names.txt
try:
with open(file_name, mode=”x”) as f:
f.write(“Saburo\n”)
except FileExistsError:
print(“File exists!”)
file_name = “names.txt”
with open(file_name, mode=”w”) as f:
f.write(“Jiro\n”)
https://kuchikarasuni.com/blog/onamae-rs-server-ssh/
Pythonをレンタルサーバーで使う方法を現役エンジニアが解説【初心者向け】 | TechAcademyマガジン