会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132834个问题

商城项目.zip

为什么“家电”跑到上面去了,咋办

WEB前端全系列/第一阶段:HTML5+CSS3模块/商城官网项目 18661楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 18663楼

from django.http import JsonResponse
from rest_framework.parsers import JSONParser

from rest_app.models import Student
from rest_app.serializers import StudentSerializer
from django.views.decorators.csrf import csrf_exempt


#不进行csrf检测
@csrf_exempt
def students(request):
    #GET请求(获取)
    if request.method == 'GET':
        #获取所有的student数据
        student_li = Student.objects.all()
        #进行序列化,many=True
        serializer = StudentSerializer(student_li,many=True)
        #返回一个json格式的响应到客户端
        #safe为True,必须传入字典。但是前面传入的是列表,只要是字典以外的格式都要将safe设为false
        #没有指定响应状态吗,默认是200
        return JsonResponse(serializer.data,safe=False)

    #POST请求(新增)
    elif request.method == 'POST':
        #使用JSONParser将request的主题内容进行反序列化
        data = JSONParser().parse(request)
        #将上一部的字典数据传入到序列化中
        serializer = StudentSerializer(data=data)
        #进行验证
        if serializer.is_valid():
            #保存数据到数据库
            serializer.save()
            #返回201状态码 和 辛堡村的数据库的数据          201:新增
            return JsonResponse(serializer.data,status=201)
        #数据验证失败,则返回400状态码 和 序列化类的所有错误信息
        return JsonResponse(serializer.errors,status=400)

老师 我的代码何老师的一样,为什么 不管是 在Terminal中还是 在程序跑起来,都会报一个错误,报错代码如下image.png

我尝试调整版本问题 依据是如下图

f9a0ecc7cef5c9967121870e9b4b3b0.png


不管怎么样调整还是不对 ,麻烦老师帮忙解答下。已经换了两台电脑进行检测了

Python 全系列/第十二阶段:Python_Django3框架/Django高级 18664楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 18665楼
人工智能/第二阶段:人工智能基础-Python基础/Python集合操作 18666楼

wsk.rar

老师:我的IDEA这个页面上没有 Allow parellel run,不知道在哪儿啊?


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 18670楼
Python 全系列/第二阶段:Python 深入与提高/模块 18671楼
Python 全系列/第二阶段:Python 深入与提高/坦克大战 18672楼

def dictionary_reader(filename):

    """
    :param filename: The type of the filename is string,
                    which means users need to input a string
                    type's filename.
    :return: a list which contain each word in the file
    """
    words_no_punctuation = []
    words_less_than_9 = []

    try:
        # open the file and execute the read operation
        f = open(filename, 'r')
        # append the each word speratly into a list
        words = f.readlines()
        for word in words:
            words_no_punctuation.append(word.strip())
        for word in words_no_punctuation:
            if len(word) <= 9:
                words_less_than_9.append(word)
        # print(len(words_less_than_9))


        # This is the test part of the dictionary_reader
        # Test passd, if length of list equal to 235887
        x = ''
        for word in words_less_than_9:
            if len(word) > 9:
                x = False
                break
            else:
                x = True
                continue

        if x == False:
            print("Not all of the len of word in this list, which is less than 9 characters.")
        else:
            print("-----Test passed! All words in this list are less than 9 characters-----")




        if len(words) == 235887:
            print("-----Test passed! There are 235887 words in the list!-----")
        else:
            print("The len of words is not equal to the len of word in the words.txt.")

        if isinstance(words_less_than_9, list):
            print("-----Test pssed! The function dictionary_reader will return the all words which len are less than 9 characters in a list!-----")
        else:
            print("The return stuff is not the list type.")
        return words_less_than_9
    except BaseException as e:
        # print the error information when programme meet error
        # it is easy to help users matain the programme
        print(e)
    finally:
        # make sure the file stream will be closed in any suitation of programme
        f.close()
        
        
def word_lookup():
    # read file at first
    # words is a list, and each elements in the words list is a word.
    words = dictionary_reader('words.txt')
    sorted_dictionary = []
    longest_words = []
    possibility_words = []
    for word in words:
        """  
            every single word in the words list will be sorted by alphabetical.
        """
        word = word.strip()
        # ensure every single word in the list is lowercase
        sorted_dictionary.append("".join(sorted(word)).lower())
    print(sorted_dictionary)
words.txt
WechatIMG945.png

老师您好,这是我写的的两个函数。在word_lookup里面我尝试把文件里面的每一个单子都变成字母排序,但是控制台输出的第一个单词并不是按字母排序去排序的,请问一下这是为什么?

输出在附件里面传上去了,不知道为什么不能够上传图片,抱歉。

谢谢老师

Python 全系列/第四阶段:函数式编程和核心特性/函数式编程 18673楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637