1、接收一個字符串,判斷其作爲python標識符是否合法
1)合法標識符規則:數字、下劃線、字母構成;避開關鍵字;不能用數字開頭;避開內置電池中的函數
2)樣例:非法(以字母開頭:1f)
a='1f'
a.isidentifier()
輸出:
False
樣例:合法(fhdaksh)
a='fhdaksh'
a.isidentifier()
輸出:
True
2、以單詞爲單位反轉字符串,將“ I am a student”反轉爲“I ma a tneduts”
[item[::-1] for item in 'I am a student'.split(' ')]
輸出:
['I', 'ma', 'a', 'tneduts']
3、詞頻統計wordcount,請計算 ['AI','Juliedu.com','python','AI','python']這個列表中每個單詞出現的次數
a=['AI','julyedu.com','python','AI','python']
print(a)
b=set(a)
for i in b:
c = a.count(i)
print(i,'出現次數',c)
輸出:
['AI', 'julyedu.com', 'python', 'AI', 'python']
python 出現次數 2
julyedu.com 出現次數 1
AI 出現次數 2
4、輸入一個字符串返回滿足以下條件的字符串
如果字符串長度 <3,,打印原字符串
否則:以‘ing’結尾的,在末尾加‘ly’打印;不以‘ing’結尾的,將‘ing’添加到末尾後打印
s = input ("請輸入:")
if len(s)>3:
if s.endswith("ing"):
s += "ly"
else:
s += "ing"
else:
pass
print (s)
輸出1:(非ing結尾)
請輸入:jdhda
jdhdaing
輸出2:(ing結尾)
請輸入:fighting
fightingly
輸出3:(小於3個字符長度)
請輸入:abc
abc
5、生成兩個0-100的隨機列表,隨機取樣,分別輸出兩次採樣的交集和並集,參與試用random下的sample方法
import random
a = random.sample(range(0,100),10)
b = random.sample(range(0,100),10)
print ("a序列:" ,a)
print ("b序列:" ,b)
Bingji = list(set(a)^set(b))
Jiaoji = list((set(a).union(set(b)))^(set(a)^set(b)))
print("並集爲:",Bingji)
print("交集爲:",Jiaoji)
輸出:
a序列: [41, 78, 43, 76, 7, 83, 8, 6, 73, 67]
b序列: [38, 50, 74, 3, 51, 26, 48, 76, 73, 78]
並集爲: [3, 67, 38, 6, 7, 74, 8, 41, 43, 48, 50, 51, 83, 26]
交集爲: [73, 76, 78]