質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

751閲覧

python3 メッセージの受け渡しと振り分けの判定

Untitled_Sample

総合スコア192

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/01/04 18:00

編集2018/01/04 18:01

文章からメッセージの振り分け。

入力データ
1kara2he xxxx1
3kara1he xxxx2
2kara1he xxxx3
3kara1he xxxx4

python

1#l=[] 2#[l.append(input().split()) for _ in range(4)] 3完成するデータ 4l=[ 5['1kara2he', 'xxxx1'], 6['3kara1he', 'xxxx2'], 7['2kara1he', 'xxxx3'], 8['3kara1he', 'xxxx4'] 9] 10 11import re 12[r.append(re.findall(r'(\d+|\D+)',i[0])) for i in l] 13[print(i) for i in r] 14 15#処理済みデータ 16""" 17['1', 'kara', '2', 'he'] 18['3', 'kara', '1', 'he'] 19['2', 'kara', '1', 'he'] 20['3', 'kara', '1', 'he'] 21"""

この時に

python

1 2 3A1=[]#A1は仮の配列名で作成済みの配列 4A2=[]# 々 5A3=[]# 々 6for i in r: 7 if i[3]=='he': #heは送るの意味 8 if int(i[0])==1 and int(i[2])==2:#ここを自動化したい 9 A1.append(l[0][1])#送信者 10 A2.append(l[0][1])#受信者 11print(A1,A2)#確認

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

コード大幅に変えてしまいましたけど、こういうことがしたかったのでしょうか?

python

1import re 2 3def mail(m): 4 mat = re.search(r'(.+?)kara(.+?)he',m[0]) 5 sender = mat.group(1) 6 receiver = mat.group(2) 7 return (sender,receiver,m[1]) 8 9N = 3 # ここで人数変更可能に 10people = [{'sent':[],'received':[]} for _ in range(N)] 11 12def exchanger(sen,rec,mes): 13 global people 14 try: 15 s,r = map(lambda p: int(p)-1,(sen,rec)) 16 people[s]['sent'].append(mes) 17 people[r]['received'].append(mes) 18 except Exception as e: 19 print('message '+"'"+mes+"'"+' couldn\'t be sent : '+str(e)+'\n') 20 21messages = [ 22 ['1kara2he', 'xxxx1'], 23 ['3kara1he', 'xxxx2'], 24 ['2kara1he', 'xxxx3'], 25 ['3kara1he', 'xxxx4'] 26] 27 28for m in messages: 29 exchanger(*mail(m)) 30 31for i,person in enumerate(people): 32 print('''No. {0}: 33sent : {1} 34received : {2} 35'''.format(i+1,person['sent'],person['received']))

出力

No. 1: sent : ['xxxx1'] received : ['xxxx2', 'xxxx3', 'xxxx4'] No. 2: sent : ['xxxx3'] received : ['xxxx1'] No. 3: sent : ['xxxx2', 'xxxx4'] received : []

投稿2018/01/04 19:13

編集2018/01/04 19:28
namnium1125

総合スコア2043

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問