【Python3】04、內建資料結構

來源:互聯網
上載者:User

標籤:python



1、把字串形式的整數或浮點數轉化為int或float, 不適用int和float函數

In [57]: str1 = "2468.1357"In [58]: d1 = {"0":0, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "    ...: 9":9}    In [59]: int1, float1 = str1.split(".")In [60]: sum1 = 0In [61]: sum2 = 0In [62]: for k, v in enumerate(int1):    ...:     sum1 += d1[v] * 10 ** (len(int1) - k - 1)    ...: for i, j in enumerate(float1):    ...:     sum2 += d1[j] * 10 ** (-(i + 1))    ...: print(sum1 + sum2)    ...: 2468.1357#看到同學給的思路

2、移除一個列表中的重複元素,並保持列表原來的順序

In [33]: l1 = [1, 3, 5, 7, "a", 7, 3, 1, "a", "b", "ab"]In [34]: l2 = []In [35]: for i in l1:    ...:     if i not in l2:    ...:         l2.append(i)    ...: print(l2)    ...: [1, 3, 5, 7, ‘a‘, ‘b‘, ‘ab‘]

3、統計文本中各單詞出現的次數

In [170]: str1 = ‘‘‘Hello world  I like Python i like python too he he python i     ...:  i world‘‘‘In [171]: l1 = str1.split()In [172]: j = 1In [173]: d1 = {}In [174]: for x in l1:     ...:     if x not in d1:     ...:         d1[x] = j     ...:     else:     ...:         d1[x] += 1     ...: print(d1)     ...: for k in d1:     ...:     print("The {} count: {}".format(k, d1[k]))     ...:     {‘i‘: 3, ‘Python‘: 1, ‘I‘: 1, ‘too‘: 1, ‘python‘: 2, ‘like‘: 2, ‘Hello‘: 1, ‘he‘: 2, ‘world‘: 2}The i count: 3The Python count: 1The I count: 1The too count: 1The python count: 2The like count: 2The Hello count: 1The he count: 2The world count: 2#文本的話,現在還沒學到io,不知道是不是用字串代替;還有就是單詞的順序保證不了,不知道有什麼好方法

4、把1~4000 之間的任意整數轉化為羅馬數字

羅馬數字是阿拉伯數字傳入之前使用的一種數位。羅馬數字採用七個羅馬字母作數字:

Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)。

記數的方法:

  1.    相同的數字連寫,所表示的數等於這些數字相加得到的數,如 Ⅲ=3;

  2.    小的數字在大的數位右邊,所表示的數等於這些數字相加得到的數,如 Ⅷ=8、Ⅻ=12;

  3.    小的數字(限於 Ⅰ、X 和 C)在大的數位左邊,所表示的數等於大數減小數得到的數,如 Ⅳ=4、Ⅸ=9;

  4.    在一個數的上面畫一條橫線,表示這個數增值 1,000 倍,如650) this.width=650;" title="" height="15" width="14" src="http://g.hiphotos.baidu.com/baike/s%3D15/sign=4412a1aab8096b6385195a550d33e4bf/9358d109b3de9c829f0c9f0a6a81800a18d843cd.jpg" style="border:0px;" /> =5000。

  5. d1 = {‘1‘:‘I‘, ‘5‘:‘V‘, ‘10‘:‘X‘, ‘50‘:‘L‘, ‘100‘:‘C‘, ‘500‘:‘D‘}#沒有思路,,,,



【Python3】04、內建資料結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.