一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
dic={
1:[],
2:[],
}
for i in li:
if i<66:
dic[1].append(i)
if i >=66:
dic[2].append(i)
print dic
s={}
a=[]
b=[]
li=[11,22,33,44,55,66,77,88,99]
for i in li:
if i>66:
a.append(i)
if i<=66:
b.append(i)
s={'k1':a,'k2':b}
print s
二、查找
查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
for i in li:
i.strip()
print li
for i in li:
if i.startswith('a') or i.startswith('A') and i.endswith('c') is True:
print i
# 元组同上
for i in dic.values:
i.strip()
print dic
for i in dic.values:
if i.startswith('a') or i.startswith('A') and i.endswith('c') is True:
print i
m=input('how much do you have:')
goods = [
{"name": "computer", "price": 1999},
{"name": "mouse", "price": 10},
{"name": "boat", "price": 20},
{"name": "woman", "price": 998},
]
#car_dict={电脑:{'price':单价,'num':数量}}
car_dict={}
for i in goods:
print i['name']+str(i['price'])
while True:
i2=input('which one do you need:')
if i2=='y':
print car_dict
break
for i in goods:
if i['name']==i2:
name=i['name']
if name in car_dict.keys():
car_dict[name]['num']+=1
else:
car_dict[name]={'single_price':i['price'],'num':1}
all=0
for k,v in car_dict.items():
n=v['single_price']
m=v['num']
all_sum=m+n
all+=all_sum
if int(m)>all:
print '跟你回家'
else:
print '穷逼,滚!'
五、用户交互,显示省市县三级联动的选择
dic = {
"q": {
"w": ["r", "t", "y"],
"e": ["d", "f", "g"],
}
}
for i in dic:
print i
a=input()
for t in dic[a]:
print t
b=input()
for x in dic[a][b]:
print x
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
for i in li:
i.strip()
print li
for i in li:
if i.startswith('a') or i.startswith('A') and i.endswith('c') is True:
print i
# 元组同上
for i in dic.values:
i.strip()
print dic
for i in dic.values:
if i.startswith('a') or i.startswith('A') and i.endswith('c') is True:
print i
四、购物车
功能要求:
- 要求用户输入总资产,例如:2000
- 显示商品列表,让用户根据序号选择商品,加入购物车
- 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
- 附加:可充值、某商品移除购物车
1
2
3
4
5
6
|
goods
=
[
{
"name"
:
"电脑"
,
"price"
:
1999
},
{
"name"
:
"鼠标"
,
"price"
:
10
},
{
"name"
:
"游艇"
,
"price"
:
20
},
{
"name"
:
"美女"
,
"price"
:
998
},
|
m=input('how much do you have:')
goods = [
{"name": "computer", "price": 1999},
{"name": "mouse", "price": 10},
{"name": "boat", "price": 20},
{"name": "woman", "price": 998},
]
#car_dict={电脑:{'price':单价,'num':数量}}
car_dict={}
for i in goods:
print i['name']+str(i['price'])
while True:
i2=input('which one do you need:')
if i2=='y':
print car_dict
break
for i in goods:
if i['name']==i2:
name=i['name']
if name in car_dict.keys():
car_dict[name]['num']+=1
else:
car_dict[name]={'single_price':i['price'],'num':1}
all=0
for k,v in car_dict.items():
n=v['single_price']
m=v['num']
all_sum=m+n
all+=all_sum
if int(m)>all:
print '跟你回家'
else:
print '穷逼,滚!'
五、用户交互,显示省市县三级联动的选择
1
2
3
4
5
6
7
8
9
10
11
12
13
|
dic
=
{
"河北"
: {
"石家庄"
: [
"鹿泉"
,
"藁城"
,
"元氏"
],
"邯郸"
: [
"永年"
,
"涉县"
,
"磁县"
],
}
"河南"
: {
...
}
"山西"
: {
...
}
}
|
"q": {
"w": ["r", "t", "y"],
"e": ["d", "f", "g"],
}
}
for i in dic:
print i
a=input()
for t in dic[a]:
print t
b=input()
for x in dic[a][b]:
print x