1.在己有列表city_list = ["北京" ,"上海","深圳"."长沙"],通过下标获取到"长沙“(两种方式)
city_list = ["北京", "上海", "深圳", "长沙"]
print(city_list[3])
print(city_list[-1])
2.己知一个数字列表num _list = [19,89, 90, 600, 1,11,23,64],使用循环输出里面所有奇数元素
i = 0
num_list = [19, 89, 90, 600, 1, 11, 23, 64]
while i < len(num_list):
if num_list[i] % 2 != 0:
print(num_list[i], end=',')
i += 1
3.有一个数字列表num_list = [19,89, 90, 600,1,11,23,64],获取这个列表中的最大值
# .sort(reverse=False) 升序
# .sort(reverse=True) 降序
num_list = [19, 89, 90, 600, 1, 11, 23, 64]
num_list.sort()
print(num_list[-1])
4.找出同时买iPhone11和112的人
iphone11=['jack", 'rain';'shanshan';'tom']
iphone12=['jack', 'rain', 'tom', 'huhu']
?
list = []
iphone11 = ['jack', 'rain', 'shanshan', 'tom']
iphone12 = ['jack', 'rain', 'tom', 'huhu']
for i in iphone11:
for j in iphone12:
if i == j:
list.append(i)
print(list)
?
5.实现输入一个字符串,分别将大写字母、小写字母、数字、其他字符存到列表里面并且输出列表
#第一
a = b = c = d = 0
list1 = []
list2 = []
list3 = []
list4 = []
str = input('请输入你的字符串:')
for sum in str:
if sum.isupper():
list1.append(sum)
a += 1
elif sum.islower():
list2.append(sum)
b += 1
elif sum.isdigit():
list3.append(sum)
c += 1
else:
list4.append(sum)
d += 1
print(f'大写字母:{list1},\n小写字母:{list2},\n数字:{list3},\n其他字符:{list4}')
list1 = []
list2 = []
list3 = []
list4 = []
def number(a):
for i in a:
if i.isdigit():
list1.append(i)
elif i.isupper():
list2.append(i)
elif i.islower():
list3.append(i)
else:
list4.append(i)
return list1, list2, list3, list4
a = input('请输入一个字符串:')
a, b, c, d = number(a)
print('大写字母的个数:{}'.format(len(a)))
print('小写字母的个数:{}'.format(len(b)))
print('都是数字的个数:{}'.format(len(c)))
print('其他数字的个数:{}'.format(len(d)))
a = tuple(a)
b = tuple(b)
c = tuple(c)
d = tuple(d)
print(a, b, c, d,)
|