上面的是java代码,下面的是python代码。
class
// java
public class A extends B {
}
# python
class A(B):
method
// java
public void method(int a) {
this.var = a;
this.methodX();
}
# python
def method(self, a):
self.var = a
self.methodX()
for
次数循环:
// java
for (int i = 0; i < 10; i++) {
}
# python
for i in range(10):
列表遍历:
// java
for (String x : a) {
}
# python
for x in a:
if
// java
if (a == 1) {
} else if (a == 2) {
} else {
}
# python
if a == 1:
elif a == 2:
else:
while
// java
while (true) {
}
# python
while True:
charList 2 string 2 charList
// java
char[] a = {'h', 'a', 'h', 'a', 'o'};
String s = new String(a);
String s2 = "1,2,3";
String[] a2 = s2.split(",");
System.out.println(Arrays.toString(a2));
# python
a = ['h', 'a', 'h', 'a', 'o']
s = ''.join(a)
s2 = '1,2,3'
a2 = s2.split(',')
print(a2) # ['1', '2', '3']
instance
// java
String s = "123";
if (s instanceof String)
System.out.println("true");
# python
s = "123"
if isinstance(s, str):
print("true")
type
// java
obj.getClass();
# python
type(obj)
type-conversion
// java
double a = 2.123;
System.out.println((int) a);
# python
a = 2.123
print(int(a))
key-value
// java
// 增删改查
Map<String, String> m = new HashMap<String, String>();
m.put("hello", "world");
m.put("hello") = "haha";
String v = m.get("hello");
m.remove("hello");
// 获取key
Set ks = m.keySet();
// 判断是否存在key
if (m.containsKey("hello")) {}
// 遍历
for (String k : m.keySet()) {
System.out.println(k + '=' + m.get(k));
}
for (Map.Entry<String, String> e : m.entrySet()) {
System.out.println(e.getKey() + '=' + e.getValue());
}
# python
# 增删改查
d = dict()
d = {"hello": "world"}
d['hello'] = 'world'
d['hello'] = 'haha'
v = d['hello']
del d['hello']
# 获取key
ks = d.keys()
# 判断是否存在key
if 'a' in d.keys():
# 遍历
for k in d:
print(d[k])
for k, v in d.items():
print(k, v)
list
// java
ArrayList<String> a = new ArrayList<String>(); // 查询效率高
LinkedList<String> a = new LinkedList<String>(); // 增删效率高
// 增删改查
a.add("you");
a.set(0, "ok");
System.out.println(a.get(0));
a.remove("ok");
// 遍历
for (String x : a) {
}
// 切割
a.subList(1, a.size())
# python
a = list()
a = ['you']
# 增删改查
a.append('you')
a[0] = 'ok'
print(a[0])
a.remove('you')
# 遍历
for x in a:
# 切割
a[1:]
tuple
// java没有内置tuple,有第三方库javatuples
import org.javatuples.*;
// 1元组
Unit<String> u = new Unit<String>("hello");
// 2元组
Pair<String,Integer> p = Pair.with("ok", 1);
// 3元组
Triplet<String,Integer,Double> triplet = Triplet.with("wa", 2, 1.0);
// ... Quartet Quintet Sextet Septet Octet Ennead Decade 一共有10个
KeyValue<String,String> kv = KeyValue.with("haha", "23");
LabelValue<String,String> lv = LabelValue.with("all", "68");
# python
a = ("hello", 2, 0.5, "ok")
date
// java
// 当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.format(new Date());
// 当前日期
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.format(new Date());
// 前一天
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
df.format(c.getTime());
// 前一天此时
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
df.format(c.getTime());
# python
# 当前时间
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 当前日期
datetime.datetime.now().strftime('%Y-%m-%d')
datetime.date.today()
# 前一天
(datetime.datetime.now() + datetime.timedelta(-1)).strftime('%Y-%m-%d')
datetime.date.today() + datetime.timedelta(-1)
# 前一天此时
(datetime.datetime.now() + datetime.timedelta(-1)).strftime('%Y-%m-%d %H:%M:%S')
null
// java
String a = null;
# python
a = None
format
// java
int a = 1;
String b = "hello";
double c = 2.3;
System.out.println(String.format("%d -- %s -- %.1f", a, b, c));
# python
a = 1
b = "hello"
c = 2.3
print(f"{a} -- {b} -- {c}")