程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python classic exercises (II)

編輯:Python

Author's brief introduction : Hello, I'm honker707, You can call me honker, Nova project Season 3 python Track Top1
Personal home page :

python Classic exercises

  • Exercises 1 : Output Dictionary key
    • Title Description
    • Question answer
  • Exercise 2 : Output character string with odd position
    • Title Description
    • Question answer
  • Conclusion

Exercises 1 : Output Dictionary key

Title Description

Here is a dictionary a, Such as a = {“honker”: 707, “hacker”: 707, “ker”: 707}, Output Dictionary a Of key, With ’,‘ Connect , Such as ‘honker’,‘hacker’,‘ker’. requirement key In ascending dictionary order
for example :a = {“honker”: 707, “hacker”: 707, “ker”: 707}, The output :honker,hacker,ker

Question answer

Use... In the dictionary keys() Method take out all keys , And then use join Method splicing ,sort() Sort by sorting method

a = {
"honker": 707, "hacker": 707, "ker": 707}
keys = a.keys()
k_res = [k for k in keys]
print(','.join(str(k) for k in sorted(k_res)))

Here we need to pay attention to , If you take out the key directly, you will return a dict_keys Type data
Need to traverse the extraction key

a = {
"honker": 707, "hacker": 707, "ker": 707}
keys = a.keys()
print(keys)


The improvement code is as follows :

a = {
"honker": 707, "hacker": 707, "ker": 707}
keys = a.keys()
k_res = [k for k in keys]
print(','.join(str(k) for k in sorted(k_res)))

The operation results are as follows :

Exercise 2 : Output character string with odd position

Title Description

Give you a string a, Output a A string of characters with odd positions in ( The location number is from 1 Start ).
for example :a=‘honker’
The output :hne

Question answer

Use the string slice index to get values ( The step size is set to 2 String that can take odd digits )

a = "honker"
print(a[::2])

The operation results are as follows :

Conclusion

Recommend a simulated interview 、 Brush question artifact website
Click the link to register
1、 Algorithm (398 topic ): Interview must brush 100 topic 、 Introduction to algorithm 、 Interview frequency list
2、SQL piece (82 topic ): Quick start 、SQL Will know 、SQL Advanced challenges 、 The real question of the interview
3、 The real question of the written test of Dachang : Bytes to beat 、 Meituan 、 Baidu 、 tencent …


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved