python as regard to dict

dict

用于快速查询字典中内容

定义

dic = {key1:value1, key2:value2}
key是索引,value为值

dic = {
    "bo":[24, "one", "jiangsu"],
    "qiang":[23, "two", "zhoukou"],
    "lie":[22, "three", "hengshui"],
    "xiang":[23, "four", "zhumadian"],
    "huang":[21,"five", "puyang"]
}
print("xiang" in dic)

true

特性

1.key-value 结构
2.key必须是不可变数据类型(字符串或者是数字),必须唯一,不能用列表
3.value值可以修改,可以不唯一,可以用列表
4.无序存放,有序字典:ordered_dict列表是索引,字典直接用key
5.查询速度快,不受dic大小影响
6.当key多次出现时,则查询以最后一次出现为准

对字典的操作

增加

dic["jong"] = [22, "six", "shangcai"]

note:当添加的key值已经存在时,则原先的value被替换成新的value

删除

n = dic.pop("jong")   # 指定key删除
del dic["xiang"]  # 指定key删除
dic.clear()       # 清空字典

使用dic.pop()时可将删除的value赋值给变量

查找

dic("xiang")

note:只可以用key查找,而不是用value

dic.keys()    #以列表形式返回dic中所用的key 列表名为dic_keys
dic.values()  #以列表形式返回所有的value  列表名为 dic_values
dic.items()   #将字典转换成列表,其中key索引为0 value索引为1

循环

for k in dic.keys():        # 循环key
    print(k)
for k,v in dic.items():     #循环key和value
    print(k, v)
for k in dic:               #循环key和value
    print(k, dic[k])        # 此时效果和 for k,v in dic.items(): 一样,仅仅是需要key时只用k即可

求长度

n =len(dic)        # 将长度赋值给变量,同时可用于字符串和列表

嵌套

dic["dic1"] = {"li":[24, "seven", "dongbei"]}   # 循环嵌套

dic1 {‘li’: [24, ‘seven’, ‘dongbei’]}

寻找时字典两层嵌套

dic["dic1"]

实例

快递分拣 按省份区分

inf = [

   ['王*名', '北京市海淀街重阳路大厦4层'],
   ['孙*化', '北京市昌平区哈哈哈哈路大厦3层'],
   ['李*以', '浙江省杭州市海淀街重阳路大厦4层'],
   ['赵*哈', '上海市海淀街重阳路大厦4层'],
   ['钱*的', '上海市海淀街重阳路大厦4层'],
   ['吴*看', '湖北省武汉市海淀街重阳路大厦4层'],
   ['代*睿', '湖北省武汉市海淀街重阳路大厦4层'],
   ['李*名', '河北省石家庄市海淀街重阳路大厦4层'],
   ['贺*佳', '河北省石家庄海淀街重阳路大厦4层'],
   ['何*名', '四川省重庆市海淀街重阳路大厦4层'],
   ['倪*昵', '四川省重庆市海淀街重阳路大厦4层'],
   ['张*名', '北京市海淀街重阳路大厦4层'],
   ['甲*名', '北京市海淀街重阳路大厦4层'],
   ['乙*8', '北京市海淀街重阳路大厦4层'],
   ['丙*名', '北京市海淀街重阳路大厦4层'],
]
add = []
pro_add = {}
for i in inf:
    # print(i)
    add1 = i[1][:3]
    if add1 in add:
        pro_add[add1].append(i)
    else:
        add.append(add1)
        pro_add[add1] = i

for i in pro_add:
    print(i, pro_add[i])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
翻译Agent 𝑐 𝑖 . In this paper, we regard each charging station 𝑐 𝑖 ∈ 𝐶 as an individual agent. Each agent will make timely recommendation decisions for a sequence of charging requests 𝑄 that keep coming throughout a day with multiple long-term optimization goals. Observation 𝑜 𝑖 𝑡 . Given a charging request 𝑞𝑡 , we define the observation 𝑜 𝑖 𝑡 of agent 𝑐 𝑖 as a combination of the index of 𝑐 𝑖 , the real-world time 𝑇𝑡 , the number of current avail able charging spots of 𝑐 𝑖 (supply), the number of charging requests around 𝑐 𝑖 in the near future (future demand), the charging power of 𝑐 𝑖 , the estimated time of arrival (ETA) from location 𝑙𝑡 to 𝑐 𝑖 , and the CP of 𝑐 𝑖 at the next ETA. We further define 𝑠𝑡 = {𝑜 1 𝑡 , 𝑜2 𝑡 , . . . , 𝑜𝑁 𝑡 } as the state of all agents at step 𝑡. Action 𝑎 𝑖 𝑡 . Given an observation 𝑜 𝑖 𝑡 , an intuitional design for the action of agent𝑐 𝑖 is a binary decision, i.e., recommending 𝑞𝑡 to itself for charging or not. However, because one 𝑞𝑡 can only choose one station for charging, multiple agents’ actions may be tied together and are difficult to coordinate. Inspired by the bidding mechanism, we design each agent 𝑐 𝑖 offers a scalar value to "bid" for 𝑞𝑡 as its action 𝑎 𝑖 𝑡 . By defining 𝑢𝑡 = {𝑎 1 𝑡 , 𝑎2 𝑡 , . . . , 𝑎𝑁 𝑡 } as the joint action, 𝑞𝑡 will be recommended to the agent with the highest "bid" value, i.e., 𝑟𝑐𝑡 = 𝑐 𝑖 , where 𝑖 = arg max(𝑢𝑡)
最新发布
07-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值