首页 > 编程语言 >Python与Java的语法区别

Python与Java的语法区别

时间:2023-09-25 20:45:18浏览次数:56  
标签:map Java Python list 语法 key print my 字典

数据容器/数组/集合
Python: 对数据容器的操作

# 对list进行切片,从1开始,4结束,步长1(默认步长为1)
my_list = [0, 1, 2, 3, 4, 5, 6]
result1 = my_list[1:4]
print(f"结果1:{result1}")

# 对tuple进行切片,从头开始,到最后结束,步长1
my_tuple = (0, 1, 2, 3, 4, 5, 6)
result2 = my_tuple[:] # 其实和结束不写表示从头到尾,步长为1可以省略
print(f"结果2:{result2}")

# 对str进行切片,从头开始,到最后结束,步长2
my_str = "01234567"
result3 = my_str[::2]
print(f"结果3:{result3}")

# 对str进行切片,从头开始,到最后结束,步长-1
my_str = "01234567"
result4 = my_str[::-1] # 等同于序列反转了
print(f"结果4:{result4}")

# 对列表进行切片,从3开始,到1结束,步长-1
my_list = [9, 10, 21, 3, 4, 5, 6]
result5 = my_list[3:1:-1]
print(f"结果5:{result5}")

# 对元组进行切片,从头开始,到尾结束,步长-2
my_tuple = (0, 1, 2, 3, 4, 5, 6)
result6 = my_tuple[::-2]
print(f"结果6:{result6}")

Java: 对数组的操作

//对list进行切片,从1开始,4结束,补偿1
int[] my_list = {0, 1, 2, 3, 4, 5, 6};
int[] sub_list = Arrays.copyOfRange(my_list, 1, 5);
for (int i : sub_list) {
    System.out.print(i);
}

 

Python: 字典

# 定义字典
my_dict1 = {"王力宏":99, "周杰伦":88, "林俊杰": 77}
score = my_dict1["王力宏"]
print(f"王力宏的考试分数是:{score}")

Java: Map集合

Map<String, Integer> map = new HashMap<>();
map.put("王力宏",99);
map.put("周杰伦",88);
map.put("林俊杰",77);
int score = map.get("王力宏");
System.out.println("王力宏的考试分数是:"+score);


Python: 遍历字典

# 获取全部Key
my_dict = {"周杰伦": 99, "林俊杰": 88, "张学友": 77}
keys = my_dict.keys()
print(f"该字典的全部Keys是:{keys}")

# 遍历字典
# 方式1:通过获取全部的Key来完成遍历
for key in keys:
    print(f"字典的key是:{key}")
    print(f"字典的value是:{my_dict[key]}")

# 方式2:直接对字典进行for循环,每一次循环都是直接得到key
for key in my_dict:
    print(f"2字典的key是:{key}")
    print(f"2字典的value是:{my_dict[key]}")

# 统计字典内的元素数量,len()函数
num = len(my_dict)
print(f"字典中的元素数量有:{num}个")

Java: 遍历Map

//遍历Map
Map<String, Integer> map = new HashMap<>();
map.put("王力宏",99);
map.put("周杰伦",88);
map.put("林俊杰",77);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String name = entry.getKey();
int score = entry.getValue();
System.out.println(name + "的考试分数是:" + score);
}
// 统计字典内的元素数量
int size = scores.size();
System.out.println("Map中元素的数量是:" + size);

 

标签:map,Java,Python,list,语法,key,print,my,字典
From: https://www.cnblogs.com/TylerZhong/p/17728796.html

相关文章

  • Python-day18
    1、常用的文件打开模式rfile=open('a.txt','r')print(file.readlines())file.close()wfile=open('a.txt','w')file.write('whywhywhy')file.close()afile=open('a.txt','a')file.write('whywhywhy......
  • Python学习笔记1
    a="好的,测试字符tester"b=17c=3print(a[1:5])#从第1(包含)个字符取到第5(不包含)个字符print(a[:3])#取到第3个字符(不含3)print(a[-5:-1])#取倒数第5个到倒数第1个print(a[-1:])#取最后一个字符print(len(a))#字符长度#exit()#退出与quit()一样,里面......
  • 试试你的语法
    转载,公众号为 Larry想做技术大佬Inthebustlingmetropolisthatneversleeps,wheretoweringskyscrapersscrapetheheavens,acacophonyofsirens,carhorns,andchatterfillstheair,andmillionsofpeoplefromdiversewalksofliferushthroughcrowdedst......
  • Sentienl基于Jdk17版本运行出错:java.lang.IllegalStateException: Cannot load config
    java.lang.IllegalStateException:Cannotloadconfigurationclass:com.alibaba.csp.sentinel.dashboard.DashboardApplicationatorg.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostP......
  • 无涯教程-JavaScript - RSQ函数
    描述RSQ函数通过known_y和known_x中的数据点返回皮尔逊乘积矩相关系数的平方。语法RSQ(known_y's,known_x's)争论Argument描述Required/OptionalKnown_y'sAnarrayorrangeofdatapoints.RequiredKnown_x'sAnarrayorrangeofdatapoints.RequiredNotes......
  • python2 http服务端和客户端
    server.pyimportSimpleHTTPServerimportSocketServerclassMyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):defdo_GET(self):self.send_response(200)self.send_header('Content-type','text/plain')self......
  • Python的Selenium库:网页元素定位工具
    Selenium是一个用于自动化web应用测试的开源工具。通过Selenium,我们可以模拟真实用户的操作,如点击、输入、滚动页面等,来测试web应用的稳定性和可靠性。PythonSelenium库是Selenium的一个分支,可以方便地与Python语言结合使用。在PythonSelenium库中,元素定位是一项核心功能。通过......
  • 使用JavaScript实现无限滚动的方法
    前言在网页设计中,无限滚动是一种常见的交互方式,用户可持续地加载更多内容而无需刷新页面,提高用户体验。本文将介绍如何运用JavaScript实现无限滚动的效果,使网页能够自动加载更多数据,减轻用户加载新页的负担,为用户提供更好的访问体验。原理理解无限滚动的原理无限滚动的原理是当......
  • NumPy:Python科学计算基础包
    NumPy是Python科学计算的基础包,几乎所有用Python工作的科学家都利用了的强大功能。此外,它也广泛应用在开源的项目中,如:Pandas、Seaborn、Matplotlib、scikit-learn等。Numpy全称NumericalPython。它提供了2种基本的对象:ndarray与ufunc。ndarray是存储单一数据的多维数组,它......
  • JavaScript——递归
    //递归:找到所有节点,并在每个节点上添加属性recursionMethod(data);constrecursionMethod=(array:any)=>{leti;for(iinarray){letarr=array[i];//是否存在children,存在则添加一个value属性,并赋值id//然后继续递归,查找arr.chi......