python测试题-数据存储模型的应用

1.如何实现 “1,2,3” 变成 [‘1’,’2’,’3’] ? 如何实现[‘1’,’2’,’3’]变成[1,2,3] ?(代码题)

# 第一个问题
str1 = "1,2,3"
list1 = [i for i in str1.split(",")]
print(list1)

# 第二个问题
list2 = ['1', '2', '3']
list3 = [int(i) for i in list1]
print(list3)

 2.      12345 能组成多少个互不相同且无重复的三位数(代码题)

count = 0
for i in range(1, 6):
    for k in range(1, 6):
        for j in range(1, 6):
            if i != k and i != j and k != j:
                count += 1
                print(str(i) + str(j) + str(k))
print("共组成%d个不重复的三位数" % count)

 3.比较: a = [1,2,3] 和 b = [(1),(2),(3) ] 以及 c = [(1,),(2,),(3,) ] 的区别?(简答题)

       a和b都是列表,c是元祖列表

4.两个列表 colors=['balck',"white"]  sizes=["S","M","L"],请严格按照顺序输出如下结果

:[('balck', 'S'), ('white', 'S'), ('balck', 'M'), ('white', 'M'), ('balck', 'L'), ('white', 'L')] (按照尺码逆序排列)(代码题)

两种方法

# 方法一
colors = ['balck', "white"]
sizes = ["S", "M", "L"]
list1 = [(c, s) for s in sizes for c in colors]
print(list1)


# 方法二
colors = ['balck', "white"]
sizes = ["S", "M", "L"]
list1 = list()
for s in sizes:
    for c in colors:
        list1.append((c, s))
print(list1)

5.有两个6面骰子,同时抛掷100000次,求两个骰子点数只和所占百分比,如下:

(注意:字典顺序可以不一致,由于是概率性问题,字典的值会有浮动)(代码题)

{2: '2.78%', 3: '5.59%', 4: '8.33%', 5: '10.92%', 6: '13.93%', 7: '16.68%', 8: '13.94%', 9: '11.13%', 10: '8.44%', 11: '5.53%', 12: '2.72%'}

import random

list1 = list()
dict1 = dict()
for i in range(100000):
    a = random.randint(1, 6)
    b = random.randint(1, 6)
    list1.append(a + b)
for data in set(list1):
    dict1[data] = "{:.2%}".format(list1.count(data) / 100000)
print(dict1)

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦途的测开笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值