def quick_sort_list(x):
if len(x)<2:
return x
else:
mid = x[0]
less = [value for value in x[1:] if value <= mid]
greater = [value for value in x[1:] if value > mid]
output = quick_sort_list(less)+[mid]+quick_sort_list(greater)
return output
quick_sort_list([14,6,7,4,9,30])
def quick_sort_list(x):
if len(x)<2:
return x
else:
mid = x[0]
less = [value for value in x[1:] if value[1] <= mid[1]]
greater = [value for value in x[1:] if value[1] > mid[1]]
output = quick_sort_list(less)+[mid]+quick_sort_list(greater)
return output
course = [['语文',85],['数学',90],['英语',94],['历史',87],['体育',85],['音乐',98]]
quick_sort_list(course)
word_string = '''Last week I went to the theatre.
I had a very good seat.
The play was very interesting.
I did not enjoy it.
A young man and a young woman were sitting behind me.
They were talking loudly.
I got very angry.
I could not hear the actors.
I turned round.
I looked at the man and the woman angrily.
They did not pay any attention.
In the end, I could not bear it.
I turned round again.
"I can't hear a word!" I said angrily.
It's none of your business," the young man said rudely.
"This is a private conversation!"
'''
在python中除了可以用双引号和单引号来定义字符串以外,还可以用三引号来定义这种大段的字符串。
word_string
'Last week I went to the theatre.\nI had a very good seat. \nThe play was very interesting. \nI did not enjoy it.\nA young man and a young woman were sitting behind me. \nThey were talking loudly. \nI got very angry. \nI could not hear the actors.\nI turned round. \nI looked at the man and the woman angrily.\nThey did not pay any attention. \nIn the end, I could not bear it.\nI turned round again.\n"I can\'t hear a word!" I said angrily.\nIt\'s none of your business," the young man said rudely.\n"This is a private conversation!"\n'
word_freq_list = []
for word in text_list:
word_list = [word[0] for word in word_freq_list]
freq_list = [word[1] for word in word_freq_list]
if word not in word_list:
word_freq_list.append([word,1])
else:
num = word_list.index(word)
freq_list[num] = freq_list[num] + 1
word_freq_list[num][1] = freq_list[num]