Python inexperienced person tries to knock 100 language processing 00-04

[Addition] Click here for 05-06 Python inexperienced person tries to knock 100 language processing 05-06 https://qiita.com/earlgrey914/items/e772f1b7e5efea114e1d Click here for 07-09 Python inexperienced person tries to knock 100 language processing 07-09 https://qiita.com/earlgrey914/items/a7b6781037bc0844744b


Click here to try


100 language processing knock 2015
https://repository.kulib.kyoto-u.ac.jp/dspace/handle/2433/245698

** Author profile (important) ** No basic learning experience in Python. A level where you can copy and move the sample lying on the net. I've touched Java a little in college exercises. Incumbent is not a programmer.

** (Important) This article does not write the correct and beautiful answer, but records the process of how a person at the above level arrived at the answer. ** **

00. Reverse order of strings

Get a string in which the characters of the string "stressed" are arranged in reverse (from the end to the beginning).

There was an answer on the page that was hit by googled with "Python string inversion". https://qiita.com/Hawk84/items/ecd0c7239e490ea22308

enshu0.py


str = "stressed"
str = str[::-1]
print(str)
dessert

I don't know what [:: -1] means.

~ 5 minutes googled ~ Apparently, python has a function called slice, which cuts out strings and lists. If you write [0: 3] **, it will return 3 characters ** from the 1st (0) to 3rd (3) of the character string. It's really hard to understand here. ** Don't you think that if you normally start from 0 and count up to 3, it will return the 4 characters 0,1,2,3? (Complaining) **

so,

If you want to specify from the beginning to the end, you can write [:]. Slices are written as [from here: to here], but in the case of the beginning and end, they can be omitted, so they can be written as [:].

so,

If you add [-1] at the end, it will reverse the character string and return it. So if you write [:: -1], it will reverse from the beginning to the end and return it.


Reference URL
https://www.sejuku.net/blog/44850

01. "Patatokukashi"

Take out the 1st, 3rd, 5th, and 7th characters of the character string "Patatokukashi" and get the concatenated character string.

enshu01.py


str = "Patatoku Kashii"
str = str[::2]
print(str)
Police car

Since I was googled about slices earlier, I quickly realized that I could use slices as well. The description of [:: 2] seems to mean [from the beginning to the end: the first character number is 1, and the first character + 2 characters are cut off]. Therefore, the 1st, 3rd, 5th, and 7th characters from the beginning to the end can be extracted. If you want to take out a "taxi", you can use the first letter as ta, so the following is OK.

enshu01_2.py


str = "Patatoku Kashii"
str = str[1::2]
print(str)
taxi

This means [from the second character (1): to the end: the first character number is 1, and the first character + 2 characters are cut off].

02. "Police car" + "Taxi" = "Patatokukashi"

Get the character string "Patatokukashi" by alternately connecting the characters "Police car" + "Taxi" from the beginning.

I stumbled. With Java knowledge, you can write a for statement like this, but is that so good?

dousshiyou.java


String patoka = "Police car";
String takushi = "taxi";
int n = patoka.length();

String answer = "";

for (int i = 0; i < n; i++){
 answer = answer +The i-th character, which is a slice of a police car character by character
 answer = answer +The i-th character sliced from a taxi character by character
}

Probably not ... I don't know if you can write a for document like for (int i = 0; i <n; i ++) in Python. The questioner probably has a different answer. (This java is written only by transcribing the image in the brain, so please do not mess with the notation etc.)

~ 10 minutes googled ~ As a result, I didn't get a good answer. When I wrote the for statement properly, I found the following

tekitou.py


str_p = "Police car"

for i in str_p:
    print(i)
Pacific League
To
Mosquito
-

Somehow, each character is output, right?

Also, it seems that you can process two lists at the same time by using something called zip ().
Reference
https://uxmilk.jp/13726

Then is this all right?

enshu02.py


str_p = "Police car"
str_t = "taxi"
answer = ""

for p,t in zip(str_p,str_t):
    answer = answer + p
    answer = answer + t
    
print(answer)
Patatoku Kashii

I wonder what about. Well, I was able to do it. Answers will be matched later ...

03. Pi

Break down the sentence "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics." Into words, and create a list of the number of characters (in the alphabet) of each word in order of appearance.

This looks easy. I know you can list strings separated by arbitrary characters with split ().

lisitka.py


str = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
list = str.split()
print(list)
['Now', 'I', 'need', 'a', 'drink,', 'alcoholic', 'of', 'course,', 'after', 'the', 'heavy', 'lectures', 'involving', 'quantum', 'mechanics.']

Then you just have to get the length of the string in the list.

koredeiinoka.py


s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
list = s.split()

answer = ""

for i in list:
    answer = answer + str(len(i))

print(answer)
3141692753589710

This time, I stopped naming the variable that stores the string str. I learned to use str () when converting from a number to a string, but I'm wearing it. By the way, I want to disappoint because the method of string concatenation called ʻanswer = answer + str (len (i))` is not good ...

So, if the answer is correct ** Something ... different, isn't it? ** **

Even though the question is "Create a list", it is returned as a character string, 3.141592 ... Not really. It is ** 3.141692 **. ** Apparently in the first list, the commas have to be removed. ** ** ↓ There is a comma in it.

['Now', 'I', 'need', 'a', 'drink,', 'alcoholic', 'of', 'course,', 'after', 'the', 'heavy', 'lectures', 'involving', 'quantum', 'mechanics.']

For the time being, remove the comma with replace (",", "")
Reference URL
http://python-remrin.hatenadiary.jp/entry/2017/04/24/174405

jokyo.py


s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
s = s.replace(",","")
print(s)
Now I need a drink alcoholic of course after the heavy lectures involving quantum mechanics.

Should I also remove the period at the end of this? If so, do replace twice.

datoshitara.py


s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
s = s.replace(",", "").replace(".", "")
print(s)
Now I need a drink alcoholic of course after the heavy lectures involving quantum mechanics

Alright, this is fine.

enshu03.py


s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
s = s.replace(",", "").replace(".", "")

list = s.split()
#['Now', 'I', 'need', 'a', 'drink', 'alcoholic', 'of', 'course', 'after', 'the', 'heavy', 'lectures', 'involving', 'quantum', 'mechanics']

answer_list = []
for i in list:
    answer_list.append(len(i))
print(answer_list)
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]

Yay.

04. Element symbol

Break down the sentence "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can." Into words 1, 5, 6, 7, 8, 9, 15, 16, 19 The first word is the first character, the other words are the first two characters, and the associative array (dictionary type or map type) from the extracted character string to the word position (what number of words from the beginning) is created. Create it.

I don't know what you're already saying. You can use split () to break it down into words. Character extraction can probably be done by slicing.

Create an associative array (dictionary type or map type) from the extracted character string to the position of the word (the number of the word from the beginning).

I don't know what this is saying.

~ 3 minutes googled ~ https://web-camp.io/magazine/archives/14815

Ahhhhh. It's like a key-value store. I understand completely () I'm not sure, but ↓ If this happens, it's okay.

{key1:value1,key2:value2,key3:value3}

So the answer is

{1:H,2:He,3:Li,4:Be,5:B,・ ・ ・}

I don't know if it looks like this.

enshu04.py


s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."

#Divide by word
list = s.split()

#A list that returns only one letter if the occurrence number of this word matches the number in this list
ichimoziList = [1, 5, 6, 7, 8, 9, 15, 16, 19]

#Dictionary type
dic = {}

counter = 1
for i in list:
    if counter in ichimoziList:
        dic[counter] = i[:1]
    else:
        dic[counter] = i[:2]
    counter+= 1
    
print(dic)
{1: 'H', 2: 'He', 3: 'Li', 4: 'Be', 5: 'B', 6: 'C', 7: 'N', 8: 'O', 9: 'F', 10: 'Ne', 11: 'Na', 12: 'Mi', 13: 'Al', 14: 'Si', 15: 'P', 16: 'S', 17: 'Cl', 18: 'Ar', 19: 'K', 20: 'Ca'}

No! !! It's done. ** I thought, but the dictionary type is used in the order of {1:'H', ...}, so is it correct? ** ** I wonder if this is ... {'H': 1, ...} I wonder which one is fine (nose hoge)

Here, the ʻifstatement first appears. Make a list of1,5,6,7,8,9,15,16,19` and say that the list contains the numerical value of the order of the words to be processed. I wrote a conditional branch.

I'm not used to using the Python for statement, so I've removed the counter. It may be a little more refreshing.

Also, "add one by one in the iterative process" seems to be written as counter + =! instead of counter ++. Hey.

It's been long, so I'll continue in another article! !! !! ** It took 3 hours so far! !! !! !! !! !! !! !! !! !! !! (important)**

Recommended Posts

Python inexperienced person tries to knock 100 language processing 14-16
Python inexperienced person tries to knock 100 language processing 07-09
Python inexperienced person tries to knock 100 language processing 05-06
Python inexperienced person tries to knock 100 language processing 00-04
100 Language Processing with Python Knock 2015
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
100 Language Processing Knock with Python (Chapter 3)
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
100 Language Processing Knock Chapter 1 by Python
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
100 Language Processing Knock (2020): 28
100 Language Processing Knock with Python (Chapter 2, Part 2)
100 Language Processing Knock with Python (Chapter 2, Part 1)
100 language processing knock 2020 [00 ~ 39 answer]
100 language processing knock 2020 [00-79 answer]
100 language processing knock 2020 [00 ~ 69 answer]
100 Language Processing Knock 2020 Chapter 1
100 Amateur Language Processing Knock: 17
Python: Natural language processing
100 Language Processing Knock-52: Stemming
100 Language Processing Knock Chapter 1
100 Amateur Language Processing Knock: 07
100 Language Processing Knock 2020 Chapter 3
100 Language Processing Knock 2020 Chapter 2
100 Amateur Language Processing Knock: 09
100 Language Processing Knock-53: Tokenization
100 Amateur Language Processing Knock: 97
100 language processing knock 2020 [00 ~ 59 answer]
100 language processing knock-92 (using Gensim): application to analogy data
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
100 Language Processing Knock-51: Word Clipping
100 Language Processing Knock-57: Dependency Analysis
100 language processing knock-50: sentence break
100 Language Processing Knock-25: Template Extraction
100 Language Processing Knock-87: Word Similarity
I tried 100 language processing knock 2020
100 language processing knock-56: co-reference analysis
Solving 100 Language Processing Knock 2020 (01. "Patatokukashi")
100 Amateur Language Processing Knock: Summary
100 Language Processing Knock-43: Extracted clauses containing nouns related to clauses containing verbs
[Python] Try to classify ramen shops by natural language processing
100 language processing knock-42: Display of the phrase of the person concerned and the person concerned
Leave the troublesome processing to Python
100 Language Processing Knock 2020 Chapter 2: UNIX Commands
100 Language Processing Knock 2015 Chapter 5 Dependency Analysis (40-49)
100 Language Processing Knock 2020 Chapter 4: Morphological Analysis
100 language processing knock-76 (using scikit-learn): labeling
Introduction to Protobuf-c (C language ⇔ Python)
100 Language Processing Knock: Chapter 1 Preparatory Movement
100 Language Processing Knock Chapter 4: Morphological Analysis
100 Language Processing Knock 2020 Chapter 10: Machine Translation (90-98)
100 Language Processing Knock 2020 Chapter 5: Dependency Analysis
100 Language Processing Knock-28: MediaWiki Markup Removal
100 Language Processing Knock 2020 Chapter 7: Word Vector
100 Language Processing Knock 2020 Chapter 8: Neural Net