LoginSignup
0
1

More than 5 years have passed since last update.

メモ:文字列同士を上から重ねたいとき。

Posted at

タイトルはテキトーにつけてます。
自分専用(かもしれない)競プロノウハウメモ

###..##....#............
+
..####...#######...##.##
↓
#######..#######...##.##

ってことをしたいとき実装パターンはどのようなことがある?

標準入力

input.txt
5 5
#....
##...
###..
....#
##...

Pythonでの実装

example.py
import numpy as np
s=input().split(" ")
x=int(s[0])
y=int(s[1])
shlist=[]
for i in range(x):
    shlist.extend(list(input()))
#この時点ですべての情報をInputした

sharray=np.array(shlist).reshape(x,y)
"""
sharray=
[['#','.','.','.','.'],
 ['#','#','.','.','.'],
...
 ['#','#','.','.','.']]
"""

#行列同士の足し算をnumpyでできる&True+False=Trueを利用

boolarray=sharray=='#' #booltype化
"""
boolarray=
[[True,False,False,False,False],
 [True,True,False,False,False],
...
 [True,True,False,False,False]]
"""
answer=np.sum(boolarray,axis=0)
"""
answer=
[True,True,True,False,True]
"""
#あとは表示だけ
for c in answer:
    if c:
        print('#',end='')
    else:
        print('.',end='')



標準出力

a.out
###.#

ちなみに

example_2.py
import numpy as np
s=input().split(" ")
x=int(s[0])
y=int(s[1])
sl=[]
for i in range(x):
    sl.extend(list(input()))
#この時点ですべての情報をInputした

a=np.sum(np.array(sl).reshape(x,y)=='#',axis=0)

#あとは表示だけ
for c in a:
    if c:
        print('#',end='')
    else:
        print('.',end='')

縮めると結構短くなる
C++でも作りたいので、それはまた明日にでも・・・(- _ - )...zzz

参考にさせて頂きました

http://y0m0r.hateblo.jp/entry/20130601/1370077334
list(str) で1文字ずつのリストに分割可能

0
1
7

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