[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
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. ** **
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
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]
.
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 ...
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.
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 ~
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 of
1,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