"Python3" 04, built-in data structure

Source: Internet
Author: User



1. Convert integer or floating-point numbers in string form to int or float, not int and float functions

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# See the students give ideas 

2. Remove duplicate elements from a list and keep the list in its original order

in [[]: L1 = [1, 3, 5, 7, "a", 7, 3, 1, "A", "B", "AB"]in [[+]: L2 = []in]: For i in L1: ...: If I not in L2: ...: l2.append (i) ...: print (L2) ...: [1, 3, 5, 7, ' A ', ' B ', ' AB ']

3, Number of occurrences of each word in the statistical text

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# text words, Now I have not learned Io, do not know whether to use string substitution, there is the order of the word can not guarantee, do not know what a good way

4. Convert any integer between 1~4000 to Roman numerals

Roman Numerals are Arabic numerals a digital used before passing in. Roman numerals are numbered using seven Roman letters:

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

Methods of Counting:

  1. The same number ligatures, the number represented is equal to the number of these numbers added, such as ⅲ=3;

  2. The small number is on the right side of the large number, and the number represented is equal to the number added to the number, such as ⅷ=8, ⅻ=12;

  3. The small numbers (limited to Ⅰ, X, and C) are on the left side of the large number, and the number represented is equal to the number of large numbers reduced, such as ⅳ=4, ⅸ=9;

  4. Draw a horizontal line on top of a number, indicating that the number adds up to 1,000 times, such as 650) this.width=650; "title=" "height=" "width=" "src=" http://g.hiphotos.baidu.com/ Baike/s%3d15/sign=4412a1aab8096b6385195a550d33e4bf/9358d109b3de9c829f0c9f0a6a81800a18d843cd.jpg "style=" border : 0px; "/> = 5000."

  5. D1 = {' 1 ': ' I ', ' 5 ': ' V ', ' Ten ': ' X ', ' + ': ' L ', ' + ': ' C ', ' + ': ' D '} #没有思路,,,,



"Python3" 04, built-in data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.