首页 > 编程语言 >How to use Javascript JSON.stringify similar method in Python All In One

How to use Javascript JSON.stringify similar method in Python All In One

时间:2023-08-13 14:22:58浏览次数:46  
标签:use temp Python Javascript stringify next json result test

How to use Javascript JSON.stringify similar method in Python All In One

如何在 Python 中使用类似 JavaScript JSON.stringify 的方法

应用场景

比较两个数组(列表)对象是否相等 / compares two array (list) objects for equality

// js
arr1 = [1,2,3]
arr2 = [1,2,3]

JSON.stringify(arr1) === JSON.stringify(arr2)

image

# python
import json

list1 = [1,2,3]
list2 = [1,2,3]

json.dumps(list1) == json.dumps(list2)

demos

比较两个列表是否相等

import json

list1 = [1,2,3]
list2 = [1,2,3]

result = json.dumps(list1) == json.dumps(list2)
print("result =", result)

""" 

$ py3 ./json.stringify_json.dumps.py
result = True

"""

image

比较两个链表是否相等

# Definition for singly-linked list.

from typing import Optional

class ListNode:
  def __init__(self, val=0, next=None):
    self.val = val
    self.next = next

class Solution:
  def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
    # DFS / BFS
    def getLength(node):
      temp = node
      length = 1;
      while temp.next != None:
        length += 1
        temp = temp.next
      return length
    def dfs(node, length):
      temp = node
      # 向下取整除法
      l = length // 2
      while l > 0:
        l -= 1
        temp = temp.next
      return temp
    return dfs(head, getLength(head))


def SingleLinkedListGenerator(num: int, start: int = 1) -> Optional[ListNode]:
  head = ListNode()
  temp = head
  for i in range(num):
    if(start == 1):
      temp.next = ListNode(i + start)
      # print("temp.next.val =", temp.next.val)
      temp = temp.next
    else:
      if(i < start):
        temp.next = ListNode(i + start)
        # print("temp.next.val =", temp.next.val)
        temp = temp.next
  return head.next

# 比较两个链表是否相等
def compareListEqual(one, two):
  while one and two and one.val == two.val:
    one = one.next
    two = two.next
  # 同时为 None ✅
  if not one and not two:
    return True
  return False

input = SingleLinkedListGenerator(7)
# print("\n")
result = SingleLinkedListGenerator(7, 4)
# i = [1,2,3,4,5,6,7]
# r = [4,5,6,7]

solution = Solution()
case = solution.middleNode(input)

test = compareListEqual(case, result)
if test:
  print("✅ test =", test)
else:
  print("❌ test =", test)

# how to compare two linked lists are equal in Python ??? JSON.stringify(list)
# https://www.cnblogs.com/xgqfrms/p/17626511.html

# # test cases
# t1 = 14
# t2 = 8
# t3 = 123
# tests = [t1, t2, t3]

# r1 = 6
# r2= 4
# r3 = 12
# results = [r1, r2, r3]

# def test():
#   solution = Solution()
#   for index, test in enumerate(tests):
#     value = solution.numberOfSteps(test)
#     result = results[index]
#     if(str(value) == str(result)):
#       print("✅ passed", index)
#     else:
#       print("❌ failed", value, result)

# test()


""" 

$ py3 ./876_middle-of-the-linked-list.py


https://leetcode.com/problems/middle-of-the-linked-list/

https://www.cnblogs.com/xgqfrms/p/17624242.html

"""

https://www.cnblogs.com/xgqfrms/p/17624242.html

(

标签:use,temp,Python,Javascript,stringify,next,json,result,test
From: https://www.cnblogs.com/xgqfrms/p/17626511.html

相关文章

  • JavaScript之循环及其案例
    1循环循环的目的在实际问题中,有许多具有规律性重复性操作,因此在程序中要完成这类操作就需要重复执行某些语句。1.1JS中的循环在JS中,主要有三种类型的循环语句:for循环while循环do...while循环2for循环在程序中,一组被重复执行的语句被称之为循环体,能否继续重复执行,取决于循环的终......
  • JavaScript基础知识
    JavaScript基础知识1、简介1、javascript是一面向对象的解释型语言,是实现动态前端页面的,它是基于对象和事件驱动并具有相对安全性的客户端脚本语言。它的主要目的是,验证发往服务器端的数据、增加Web互动、加强用户体验度。2、javascript体系由ECMAScript、DOM、BOM组成。2、......
  • - csrf跨站请求的相关装饰器 - Auth模块的使用 - 凡是跟登录、注册、修改密码、注销
    csrf跨站请求的相关装饰器 Django中有一个中间件对csrf跨站做了验证,我只要把csrf的这个中间件打开,意味着所有的方法都要被验证在所有的视图函数中:只有几个视图函数做验证只有几个函数不做验证csrf_protect:哪个视图函数加了这个装饰器,这个函数就会做验证 csrf_exemp......
  • Python - 会考必过篇
    文章简介该文章是为了帮助身边朋友为了会考信息与技术将Python这门语言给学会而创作的。开发工具在线运行环境腾讯Python实验室:Python|Coding1024Code:Python|1024CodeLwebapp:Python|Lwebapp!>以上在线运行环境,前两种需要登录,目前找到的仅有第三种比较好用,......
  • Python 农历公历相互转换
    Python农历公历相互转换stackOverflowsh关注IP属地:江苏0.2252019.02.1918:23:48字数2,054阅读6,862背景日常用python处理各种数据分析工作,最近需要对历年春节期间的数据做一些对比工作,本来只是用了一个简单的日期数组来进行,但后来发现一些数据在农历日期进行对比......
  • 8-13|Cannot run program "C:\Users\Administrator\AppData\Local\Temp\GoLand
    您的错误消息指的是尝试运行的程序与您当前的Windows版本不兼容。这可能是因为您正在使用一个旧版本的Windows(例如32位的版本)并试图运行一个为新版本(例如64位)编译的程序。以下是解决这个问题的建议步骤:1.**确认您的操作系统版本**: -打开“运行”(按`Win+R`),输入`msinfo32......
  • Python代码分享-获取B站粉丝数量
    分享一下一段Python代码。#获取B站粉丝数量#冰河之刃#2020-08-02importrequestsimportjsonurl="https://api.bilibili.com/x/relation/stat?vmid=490458635"payload={}#发起请求response=requests.request("GET",url,data=payload)#print(response.......
  • 深入理解JavaScript正则表达式:释放其强大力量
    深入理解JavaScript正则表达式:释放其强大力量正则表达式是一种强大的工具,用于在字符串中搜索、匹配和替换特定的模式。在JavaScript中,正则表达式是一种内置的功能,可以帮助开发人员处理各种字符串操作。本文将深入探讨JavaScript正则表达式的原理、语法和应用场景,帮助读者充分理解......
  • WARNING: Use of this script to execute namenode is deprecated. WARNING: Attempti
    问题描述在我使用这个命令进行hdfs初始化时:hadoop-daemon.shstartnamenode爆出了这样的警告问题解决发现是这个命令现在已经有一点过时,需要换成这个命令才行:hdfsnamenode......
  • 学习笔记-流畅的Python 1st
    P31和*都遵循不修改原有的操作对象,而是创建一个新的序列>>>a=[1,2,3]>>>c=a*2>>>a[0]=3>>>c[1,2,3,1,2,3]如果在a*n这个语句中,序列a里的元素是对其他可变对象的引用的话,你就需要格外注意了,因为这个式子的结果可能会出乎意料。比如,你想用my......