更新字典列表中的值

时间:2018-09-17 16:27:55

标签: python list dictionary python-3.6 updates

假设您有一本词典,其中包含一个日期列表(从excel中以浮点数形式提取)作为县的子词典中的值。

 master_dictionary = {'MO': {
   'Lincoln County': [43378.0, 43378.0, 43378.0],
   'Franklin County': [43357.0, 43357.0],
   'Camden County': [43208.0, 43208.0, 43208.0],
   'Miller County': [43208.0],
   'Morgan County': [43208.0, 43208.0]},
  'WI': {'Marathon County': [43371.0, 43371.0, 43371.0, 43371.0, 43371.0]},
  'NJ': {'Atlantic County': [43340.0, 43340.0]}}

我的目标是1)获取这些“日期”的最大值,以及2)使用'%M/%D/%Y'将最大“日期”转换为datetime.strftime值。我可以获取最大值并将其转换,但是我试图获取它以更新主词典中的日期值。我该怎么办?

for key, value in master_dictionary.items():
    counties = value
    for k, v in counties.items():
        d = max(v)
        year, month, day, hour, minute, second = xldate_as_tuple(d, book_datemode)
        n_date = rawDate = (str(month) + "/" + str(day) + "/" + str(year))
        print(n_date)

3 个答案:

答案 0 :(得分:2)

只需使用某种方法即可获取i = np.argmax(v)(numpy),这样便有了索引,然后访问该位置并用master_dictionary[key][k][i] = n_date更新。如果要替换整个列表,请使用master_dictionary[key][k] = [n_date],并且不需要argmax。祝你好运!

答案 1 :(得分:1)

使用库中的 xldate_as_datetime 函数:

for key, val in master_dictionary.items():
    for skey, sval in val.items():
        # temporary assignment to overwrite dates
        # with max for the given county (skey)
        cdate = xldate_as_datetime(max(sval),0).strftime('%m/%d/%Y')
        # assign the max date to 
        # the county in master_dictionry
        master_dictionary[key][skey] = cdate

答案 2 :(得分:1)

通常,最简单的方法是制作新词典,而不是尝试修改现有词典(尤其是在添加或删除键的情况下):

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: mapserver
spec:
  selector:
    matchLabels:
      app: mapserver
  template:
    metadata:
      labels:
        app: mapserver
    spec:
      volumes:
      - name: storage
        persistentVolumeClaim:
          claimName: mapserver-pv-claim
      containers:
      - name: maptiles
        image: klokantech/tileserver-gl
        command: ["/bin/sh"]
        args:
        - -c
        - |
           echo "[INFO] Startingcontainer"; if [ $(DOWNLOAD_MBTILES) = "true" ]; then
             echo "[INFO] Download MBTILES_PLANET_URL";
             rm /data/*
             cd /data/
             curl -k -sSL -X GET -u user:ww $(MBTILES_PLANET_URL) -O
             echo "[INFO] Download finished";
           fi;
         env:
         - name: MBTILES_PLANET_URL
           value: 'https://abc-dev/nexus/repository/xyz-raw/2017-07-03_europe_netherlands.mbtiles'
         - name: DOWNLOAD_MBTILES
           value: 'true'
        livenessProbe:
          failureThreshold: 120
          httpGet:
            path: /health
            port: 80
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 30
          successThreshold: 1
          timeoutSeconds: 5
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        readinessProbe:
          failureThreshold: 120
          httpGet:
            path: /health
            port: 80
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 30
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          limits:
            cpu: 300m
            memory: 3Gi
          requests:
            cpu: 100m
            memory: 1Gi
        volumeMounts:
        - mountPath: "/data"
          name: storage

打印

from xlrd import xldate_as_datetime
from pprint import pprint

new_dict = {k: {k1: xldate_as_datetime(max(v1),0).strftime('%m/%d/%Y') for k1, v1 in v.items()} 
            for k, v in master_dictionary.items()}

pprint(new_dict)