㈠ 请问两道关于python的题目 不知道为什么选这个
s1和s2指向同一个列表,修改s1[1]的内容会影响到s2[1]的内容,故选B
S原先长度为4,新增一个元素[5,6],该元素为一个列表,故长度为5,选C。
㈡ python 题目解答
m=['a','b','b','c','a','b','b','c']
dic={}
foriteminm:
ifitemnotindic.keys():
dic[item]=m.count(item)
print(dic)
㈢ Python题目,求解!
答案是2喔,get()是一个python获取字典的键的值函数,x.get(1)意思是在x字典中获取键值为1的值,就是2了
㈣ Python中基础练习题
法一:利用set()函数的去重功能,去重后再使用list()函数将集合转换为我们想要的列表
list1 = [11,22,33]
list2 = [22,33,44]
list3 = list(set(list1 + list2))
list3.sort()
print(list3)
-------------
法二:利用if和for,先遍历list1所有元素追加到list3中,然后遍历list2,条件判断list2中当前元素是否在list3中,如果不在则追加到list3中
list1 = [11,22,33]
list2 = [22,33,44]
list3 = []
for ele1 in list1:
list3.append(ele1)
for ele2 in list2:
if ele2 not in list3:
list3.append(ele2)
print(list3)
㈤ 计算机python题目,重谢
20
㈥ 求一份鱼C工作室,python 课后测试题及答案!!
1,使用getopt。getopt()优化当前的功能函数:
[html]
#!/usr/bin/python
# -*- coding: utf-8 -*-
#coding=utf-8
import os,sys
import getopt
print sys.argv
CDROW='/home/zhouqian/test'
def cdWalker(CDROW,cdfile):
result=[]
for root,dirs,files in os.walk(CDROW):
result.append("%s %s %s" %(root,dirs,files))
print root
open(cdfile,'w').write('\n'.join(result))
def usage():
print '''pycdc 使用方式:
python cdays-3-exercise-1.py -d cdc -k 中国火
#检索cdc中有没有中国火字样的目录,
'''
try:
opts,args=getopt.getopt(sys.argv[1:],'hd:e:k:')
except getopt.GetoptError:
usage()
sys.exit()
if len(opts)==0:
usage()
sys.exit()
c_path=''
name=''
for opt,arg in opts:
if opt in('-h','--help'):
usage()
sys.exit()
elif opt=='-e':
if os.path.exists(arg):#判断目标路径是否存在
# cdWalker(CDROW,arg)
print "记录光盘的位置是 %s" %arg
else:
print "不存在这样的目录"
elif opt=='-d':
c_path=arg
print c_path
cdWalker(CDROW,c_path)
elif opt=='-k':
if not c_path:
usage()
sys.exit()
else:
name=arg
for root,dirs,files in os.walk(c_path):
if root=='%s' %name:
print '您要找的文件在%s' %dirs
这是第一个题,大概做了2个小时吧,各种纠结啊,后面两个正在做。中间遇到的问题总结:
函式的利用,os.path.walk,python字符集,getopt模块的使用学习,os.path.exists()的利用,列表的对应关系等等
习题2 :关键词-----》序列号问题:
[html]
#!/usr/bin/python
#coding=utf-8
import sys
def collect(file):
result={}
for line in file.readlines():
left,right=line.split()
if result.has_key(right):
result[right].append(left)
else:
result[right]=[left]
return result
if __name__=="__main__":
print sys.argv
if len(sys.argv)==1:
print 'usage:\tpython value_keys.py test.txt'
else:
result=collect(open(sys.argv[1],'r'))
for (right,left) in result.items():
print "%d %s => %s" %(len(left),right,left)
结果显示:
[html]
root@zhou:/home/zhouqian/python# py value_keys.py test.txt
ssss
2 key3 => ['6', '33']
3 key2 => ['1', '2', '45']
3 key1 => ['4', '5', '13']
遇到的问题总结:
split的用法:line.split()就是分开出左右两边的值,在默认的情况下是以一个空格或者多个空格为分割符的,
has_key()的用法:是查看字典数据类型中有没有这么一个关键字。上面可知result={}是初始化了一个字典的数据类型。
字典的一些用法:怎么定义,怎么赋值:result[right]=[left]或者result[right]=left,遍历字典中所用
项,result.items(),遍历字典的key值:result.keys(),遍历字典的value值:result.values()
[html]
>>> dict={'chen':25,'zhou':24,'xiao':35}
>>> dict.values()
[25, 35, 24]
>>> dict.keys()
['chen', 'xiao', 'zhou']
>>> dict.items()
[('chen', 25), ('xiao', 35), ('zhou', 24)]
㈦ 几个Python题目
1.【选择】下面哪个选项不是定义列表的正确方式?(D)
D.myList4=2,3,4,5
答案解析:这是定义元组的方式,而不是列表
2.【选择】根据下面表达式,a的值是:(C)
C.loWo
3.【填空】请写出如何切片myList[]的倒数第3~5位。
myList[-3:-5]
4.【判断】列表内元素的下标是从0开始的。(√)
5.【选择】下列Python表达式可以将列表反向并改变原列表值的是:(D)
D.myList.reverse()
答案解析:切片会生成新的列表;reversed只是生成新的迭代器;只有list.reverse()会对原表的值进行改变
-----------
1.【判断】Python语句“x="a","b","c"”中,x是一个元组。(√)
2.【选择】执行下列Python语句会报错的是:(A)
A.myTuple[3]=30
答案解析:元组是immutable(不可变)的,所以不能改变元组的值
3.【填空】使用Python内置函数,计算元组myTuple的语句是:
sum(myTuple)
4.【选择】下列关于Python的描述错误的是:(C)
C.对元组内部元素进行排序使用的是sort()
答案解析:元组不可变,所以不能对元素进行排序
------------
1.【选择】下列关于Python中字符串说法错误的是:(D)
D.Python中字符类型是char,字符串的类型是str
答案解析:python字符和字符串类型都是str
3.【填空】请写出用空格“”合并字符串“Jane”、“Doe”的Python语句:
"Jane"+""+"Doe"
4.【选择】下列不是Python3中解决路径中特殊字符问题的选项是:(C)
C.s=u"D: est"
------------
1.【选择】下列不是序列的是:(C)
C.集合
3.【选择】下面哪些操作是序列都具有的?(D)
D.以上都是
-----
1.【判断】Python中符号{}仅用在集合这一数据类型中。(B.×)
3.【填空】语句set("datascience")的结果是:
集合{'d','a','t','s','c','i','e','n'}
4.【选择】下列关于集合的说法错误的是:(A)
A.集合具有互异性,定义集合时不允许出现相同的元素
答案解析:出现了重复也没事,会自动去重的。
7.【选择】在Python中对数据进行去重处理,一般会借助下列哪种数据类型?(C)
C.集合
---------
1.【填空】有两个列表a=["name","age","sex"],b=["jonh","23","M"],请用一个语句将这两个列表转换成字典,其中列表a中的元素为“键”,列表b中的元素为“值”。
dict(zip(a,b))
2.【填空】定义一个新字典如下,用print输出dict1的结果是:
{1:3,2:'a'}
4.【判断】下面对字典d的定义是正确的。(B.×)
答案解析:列表不能用做键。应该用元组('a','b')做键。
㈧ python的测试题
import random
import time
import pandas as pd
def listCreator(n):
raw_list = [random.randint(0, 99) for _ in range(n)]
return raw_list
def select_sort(raw_list):
length = len(raw_list)
for index in range(length):
for i in range(index, length):
if raw_list[index] > raw_list[i]:
raw_list[index], raw_list[i] = raw_list[i], raw_list[index]
return raw_list
def sortTimer():
size_list = [100, 1000, 10000]
timer_times = []
for _ in range(1, 11):
print("{} times".format(_))
timer_list = []
for i in size_list:
raw_list = listCreator(i)
start_time = time.clock()
select_sort(raw_list)
timer = time.clock() - start_time
timer_list.append(round(timer, 8))
timer_times.append(timer_list)
df = pd.DataFrame(timer_times, columns=size_list)
return df
def saveResults(df):
df.to_csv("./sortingTimes.txt", sep=' ', index=None, columns=None)
if __name__ == '__main__':
timer_times = sortTimer()
saveResults(timer_times)
㈨ python语言题目
这道题之前有网友问过...
这道题的核心是熟练运用dict()和字符串的操作方法。具体如下:
source code
如有帮助,请采纳!!!