文章内容

2020/2/11 4:24:36,作 者: 黄兵

Python Dic 增加元素

Python Dictionaries add item Example:

count = {}
with open(input_file, 'r', newline='', encoding='utf-8') as input_csv_file:
    file_reader = csv.reader(input_csv_file)
    for row in file_reader:
        ip = row[2]
        # 获取User_Agent,为后面过滤user_agent做准备
        user_agent = row[4]
        if user_agent in ['Amazon-Route53-Health-Check-Service (ref 15559bb2-4070-4b92-9611-dd7a93b58068; report http://amzn.to/1vsZADi)','Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)']:
            continue
        else:
            if ip in count:
                views = count[ip]
            else:
                views = 0
            views += 1
            count[ip] = views


other code:

thisdict =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["color"] = "red"
print(thisdict)

result:

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}


参考资料:

1、Python Dictionaries

分享到:

发表评论

评论列表