前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Python3】04、内置数据结构

【Python3】04、内置数据结构

作者头像
py3study
发布2020-01-03 11:25:08
2420
发布2020-01-03 11:25:08
举报
文章被收录于专栏:python3python3

1、把字符串形式的整数或浮点数转化为int或float, 不适用int和float函数

代码语言:javascript
复制
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 = 0

In [61]: sum2 = 0

In [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
代码语言:javascript
复制
#############
In [75]: mapping = {str(x):x for x in range(9)}

In [76]: mapping
Out[76]: {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8}

In [78]: s.partition('.')
Out[78]: ('123', '.', '456')

In [81]: s = "123"

In [82]: s.partition('.')
Out[82]: ('123', '', '')

In [83]: s = "12.3"

In [84]: s.partition('.')
Out[84]: ('12', '.', '3')

In [93]: s = "123.456"

In [94]: i, _, f = s.partition('.')

In [97]: ret = 0

In [98]: for idx, x in enumerate(i[::-1]):
    ...:     ret += mapping[x] * 10 ** idx
    ...:     

In [99]: ret
Out[99]: 123

In [100]: for idx, x in enumerate(f):
     ...:     ret += mapping[x] / 10 ** (idx+1)
     ...:     

In [101]: ret
Out[101]: 123.456        #结果可能会有精度的问题


###先全部当成整数来运算###
In [106]: ret = 0

In [107]: for idx, x in enumerate((i+f)[::-1]):
     ...:     ret += mapping[x] * 10 ** idx
     ...:     

In [108]: ret / 10 ** len(f)
Out[108]: 123.456             #精度损失会少一点

2、移除一个列表中的重复元素,并保持列表原来的顺序

代码语言:javascript
复制
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:         #O(n),效率不高
    ...:         l2.append(i)
    ...: print(l2)
    ...: 
[1, 3, 5, 7, 'a', 'b', 'ab']
代码语言:javascript
复制
###############
In [7]: l1 = [1, 3 ,5, 7, "a", "b", 5, 3, 1, "ab"]

In [8]: s = set()

In [9]: new_lst = []

In [10]: for x in l1:
    ...:     if x not in s:         #O(1),空间换时间
    ...:         new_lst.append(x)
    ...:     s.add(x)
    ...:     

In [12]: new_lst
Out[12]: [1, 3, 5, 7, 'a', 'b', 'ab']

3、统计文本中各单词出现的次数

代码语言:javascript
复制
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 = 1

In [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: 3
The Python count: 1
The I count: 1
The too count: 1
The python count: 2
The like count: 2
The Hello count: 1
The he count: 2
The world count: 2
代码语言:javascript
复制
########################
In [124]: s = "i am very very love python"

In [125]: counter = {}

In [126]: for word in s.split():
     ...:     if word not in counter.keys():
     ...:         counter[word] = 0
     ...:     counter[word] += 1
     ...: counter
     ...:  
Out[126]: {'am': 1, 'i': 1, 'love': 1, 'python': 1, 'very': 2}

#####更简洁的判断方法##########
counter[word] = counter.get(word, 0)+ 1

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 倍,如

 =5000。

代码语言:javascript
复制
############################
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档