社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Dipen Dadhaniya

Dipen Dadhaniya 最近创建的主题
Dipen Dadhaniya 最近回复了
6 年前
回复了 Dipen Dadhaniya 创建的主题 » Python:更新动态生成的嵌套json键值的递归函数

只需编写一个函数:

def update_x_from_y(x, y):
    y = y.split('.')
    n = len(y)

    for idx, item in enumerate(y):
        if idx == n - 1:
            x[item] = "india"
        else:
            x = x[item]

x = {
    "payload": {
        "name": "kabilan",
        "address": {
            "country": "value_to_change"
        }
    }
}
y = "payload.address.country"

update_x_from_y(x, y)

print(x)
5 年前
回复了 Dipen Dadhaniya 创建的主题 » python中的buggy置换

只需改变:

tempList = tempList[:-1]

致:

tempList.pop()

原因是 tempList = tempList[:-1] 在当前作用域中创建新列表(当前函数调用) 是的。因此, 调用此函数的父函数仍将指向旧列表 即父函数将具有不同的值 tempList 比你想象的要多。

6 年前
回复了 Dipen Dadhaniya 创建的主题 » 如何在django中显示外键值而不是pk?

尝试做:

from django.shortcuts get_object_or_404

def filter_staff_users(request):
    # ...
    organization = get_object_or_404(Organization, pk=request.GET.get('organization'))
    print(organization.name)

通过做:

organization = request.GET.get('organization')

我们只得到 pk (as a string) organization . 你需要的是 Organization 模型。

6 年前
回复了 Dipen Dadhaniya 创建的主题 » 无法从其他目录导入python类

尝试使用:

from src.models.UserModel import UserModel
6 年前
回复了 Dipen Dadhaniya 创建的主题 » python方法1变量嵌套字典

尝试做:

dct = dict()
dct['hits'] = dict()
dct['hits']['hits'] = dict()
dct['hits']['hits']['a'] = 'b'
dct['hits']['hits']['b'] = 'c'
dct['aggregations'] = dict()
dct['aggregations']['a'] = 1
dct['aggregations']['b'] = 2


def foo(dct, *fields):
    n = len(fields)

    for idx in range(n):
        if idx == n - 1:
            return dct[fields[idx]]
        else:
            dct = dct[fields[idx]]


print(foo(dct, 'hits'))    
print(foo(dct, 'hits', 'hits'))
print(foo(dct, 'hits', 'hits', 'a'))
print(foo(dct, 'aggregations'))
print(foo(dct, 'aggregations', 'a'))