Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python & js #82

Open
Huauauaa opened this issue Jul 28, 2024 · 6 comments
Open

python & js #82

Huauauaa opened this issue Jul 28, 2024 · 6 comments
Assignees

Comments

@Huauauaa
Copy link
Owner

Huauauaa commented Jul 28, 2024

python - javascript - java

init

const books = [
  { id: 1, name: 'book1' },
  { id: 2, name: 'book2' },
];
books = [{"id": 1, "name": "book1"}, {"id": 2, "name": "book2"}]
@Huauauaa Huauauaa self-assigned this Jul 28, 2024
@Huauauaa
Copy link
Owner Author

Huauauaa commented Jul 28, 2024

初始化二维数组

const res = Array.from({ length: 10 }, () => Array.from({ length: 10 }).fill(0));
res = [[0]*10 for i in range(10)]

@Huauauaa
Copy link
Owner Author

Huauauaa commented Jul 28, 2024

map

book_names = books.map((item) => item.name);
book_names = [item["name"] for item in books]
book_names = next(map(lambda item: item["name"], books))

@Huauauaa
Copy link
Owner Author

find

target = books.find((item) => item.id === 1); // {id: 1, name: 'book1'}
target = books.find((item) => item.id === 3); // undefined
target = next((item for item in books if item["id"] == 2), None)  # {'id': 2, 'name': 'book2'}
target = next((item for item in books if item["id"] == 3), None)  # None

@Huauauaa
Copy link
Owner Author

Huauauaa commented Jul 28, 2024

filter

res = books.filter((item) => item.id === 1);
res = books.filter((item) => item.id === 3); // []
target = list(filter(lambda item: item["id"] == 1, books))
target = list(filter(lambda item: item["id"] == 3, books))
target = next(filter(lambda item: item["id"] == 1, books), [])
target = next(filter(lambda item: item["id"] == 3, books), [])

@Huauauaa
Copy link
Owner Author

pad

const n = '10';

console.log(n.padStart(4, '*')); // **10
console.log(n.padEnd(4, '*')); // 10**
n = "10"
print(n.rjust(4, "*"))  # **10
print(n.ljust(4, "*"))  # 10**

@Huauauaa
Copy link
Owner Author

CRUD

nums = [1, 2, 3];

console.log(nums.push(5));
console.log(nums.pop());
console.log(nums.unshift(1));
console.log(nums.shift());
console.log(nums.splice(1, 0, 9));
console.log(nums);
nums = [1, 2, 3]

print(nums.append(5))
print(nums.pop())
print(nums.insert(0, 1))
print(nums.pop(0))
print(nums.insert(1, 9))
print(nums)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant