python自测题(4月2号)

  • 1.如何实现 “1,2,3” 变成 [‘1’,’2’,’3’] ? 如何实现[‘1’,’2’,’3’]变成[1,2,3] ?(代码题)
a="1,2,3"
b= a.split(",")
print(b)

print(type(b[1]))
print(",".join(b))
c = list()

for i in ['1','2','3']:
    c.append(int(i))
print(c)
  • 2,1、2、3、4、5 能组成多少个互不相同且无重复的三位数(代码题)
sum = 0
for i in range(1,6):
    for j in range(1,6):
        for k in range(1,6):
            if i!= k and i != j and k != j:
                sum += 1
                print(i,j,k)
print(sum)
  • 3.比较: a = [1,2,3] 和 b = [(1),(2),(3) ] 以及 c = [(1,),(2,),(3,) ] 的区别?(简答题)
a=[1,2,3]
print(type(a))
b = [(1),(2),(3)]
print(type(b))
print(type(b[1]))

c = [(1,),(2,),(3,)]
print(type(c[1]))
  • 运行结果




  • 4.两个列表 colors=[‘balck’,“white”] sizes=[“S”,“M”,“L”],请严格按照顺序输出如下结果
    :[(‘balck’, ‘S’), (‘white’, ‘S’), (‘balck’, ‘M’), (‘white’, ‘M’), (‘balck’, ‘L’), (‘white’, ‘L’)] (按照尺码逆序排列)(代码题)
    方法一:
colors = [ 'black',"white"]
sizes = [ "s","M","L"]
my_lis = list()
for i in sizes:
    for k in colors:
        my_lis.append((k,i))
print(my_lis)

方法二:

colors = [ 'black',"white"]
sizes = [ "s","M","L"]
print([(k,i) for i in sizes for k in colors])
  • 5.请写出列表,字典,元组,字符串常用的5个方法,并附带中文说明(简答题)
    • 1列表:
      append(data) 关键词:追加
      insert(idx,data)关键词:插入
      extend(model)关键词:追加全部
      remove(data)关键词:删除
      pop(idx)关键词:获取删除
    • 2,字典
      pop(key)关键词:删除获取
      popitem()关键词:删除
      clear()关键词:清空
      update(dict)关键词:更新数据
      get(key)关键词:获取
    • 3,元组
      index(data)关键词:查询位置
      count(data)关键词:统计数量
    • 4,字符串
      islower()关键词:是否全小写
      isupper()关键词:是否全大写
      isdigit()关键词:是否全数字
      startswith(str)关键词:判定前缀
      title()关键词:单词首字母大写其他小写
  • 6.有两个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
result = list()
for i in range(100000):
    #骰子
    a = random.randint(1,6)
    #骰子
    b = random.randint(1,6)
    result.append(a+b)
result_dict = dict()
#把列表转化为集合
for i in set(result):
    result_dict[i] = "{:.2%}".format(result.count(i) / 100000)
print(result_dict)

你可能感兴趣的:(python,自测题)