ID非公開
ID非公開さん
2021/10/28 19:14
1回答
Pythonの質問です。以下の問題が学校で出された(一部改変)のですが、解法までの流れをお教えいただけるとありがたいです。
Pythonの質問です。以下の問題が学校で出された(一部改変)のですが、解法までの流れをお教えいただけるとありがたいです。 問: 文字列Aから単語carbonを検索して、個数と場所を出力するプログラムを作成しなさい。大文字で始まるCarbonも同時に検索できるようにすること。以下の例のように出力する。 例: carbonは15個見つかりました。 第1段落 1文目 1単語目 第1段落 4文目 1単語目 第1段落 7文目 1単語目 第2段落 1文目 1単語目 第3段落 1文目 4単語目 第3段落 1文目16単語目...(続く) A = """ Carbon is a chemical element with the symbol C and atomic number 6. It is nonmetallic and tetravalent—making four electrons available to form covalent chemical bonds. It belongs to group 14 of the periodic table. Carbon makes up only about 0.025 percent of Earth's crust. Three isotopes occur naturally, 12C and 13C being stable, while 14C is a radionuclide, decaying with a half-life of about 5,730 years. Carbon is one of the few elements known since antiquity. Carbon is the 15th most abundant element in the Earth's crust, and the fourth most abundant element in the universe by mass after hydrogen, helium, and oxygen. Carbon's abundance, its unique diversity of organic compounds, and its unusual ability to form polymers at the temperatures commonly encountered on Earth enables this element to serve as a common element of all known life. It is the second most abundant element in the human body by mass (about 18.5%) after oxygen. """
プログラミング・26閲覧・250
ベストアンサー
それぞれの区切り仕様は、以下と読み取ったコードです。 段落区切りは連続改行 '\n\n' 文区切りはピリオド+スペース '. ' 単語区切りはコンマ+スペース ', ' または スペース ' ' text = """ Carbon is a chemical element with the symbol C and atomic number 6. It is nonmetallic and tetravalent—making four electrons available to form covalent chemical bonds. It belongs to group 14 of the periodic table. Carbon makes up only about 0.025 percent of Earth's crust. Three isotopes occur naturally, 12C and 13C being stable, while 14C is a radionuclide, decaying with a half-life of about 5,730 years. Carbon is one of the few elements known since antiquity. Carbon is the 15th most abundant element in the Earth's crust, and the fourth most abundant element in the universe by mass after hydrogen, helium, and oxygen. Carbon's abundance, its unique diversity of organic compounds, and its unusual ability to form polymers at the temperatures commonly encountered on Earth enables this element to serve as a common element of all known life. It is the second most abundant element in the human body by mass (about 18.5%) after oxygen. """ import re # pgph: paragraph, st: sentence, wd: word words = [[[wd for wd in re.split(', | ', st)] for st in pgph.split('. ')] for pgph in text[1:-1].split('\n\n')] cnt = sum(1 for pg_ls in words for st_ls in pg_ls for wd in st_ls if wd == 'Carbon' or wd == 'carbon') print(f'carbonは{cnt}個見つかりました。') for i,pg_ls in enumerate(words, 1): for j,st_ls in enumerate(pg_ls, 1): for k,wd in enumerate(st_ls, 1): if wd == 'Carbon' or wd == 'carbon': print(f'第{i}段落 {j}文目 {k}単語目') carbonは4個見つかりました。 第1段落 1文目 1単語目 第1段落 4文目 1単語目 第1段落 6文目 1単語目 第2段落 1文目 1単語目
質問者からのお礼コメント
実際に入力して動かしてみたところ、完璧に例の通りに出力されて感動しています。 教えていただいた内容をちゃんと理解できるよう勉強したいと思います! 迅速で丁寧なご回答ありがとうございました!!
お礼日時:10/29 15:04