list, split, join trong Python
Hôm nay ta sẽ học cách chuyển từ chuỗi(string) sang list và ngược lại. Nội dung chính - list, split, join trong Python
- How do you split a list into evenly sized chunks?
- Critique of other answers here
- Cycle Solution
- Updated prior solutions
Ta muốn chuyển từ string sang list. my_string = "Hello world 2020"
list1 = list(my_string) # list breaks a string into letters.
print(list1)
# ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', ' ', '2', '0', '2',
# '0'
list(my_string): sẽ chuyển chuỗi sang một list những ký tự. Còn Ta muốn chuyển 1 chuỗi sang các từ (word) ta dùng split(). # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
Còn nếu muốn chuyển 1 list sang chuỗi ta dùng join(). list4 = ["cat", "dog", "lion"]
my_string_3 = "-".join(list4)
print(my_string_3) # cat-dog-lion
Như vậy ta đã biết cách dùng list, split and join rồi đó. Easy easy ^_^. How do you split a list into evenly sized chunks?Critique of other answers hereminimal variance in length. E.g. 5 baskets for 21 items could have the following results: >>> import statistics
>>> statistics.variance([5,5,5,5,1])
3.2
>>> statistics.variance([5,4,4,4,4])
0.19999999999999998
Cycle Solution Critique of other answers hereCycle Solution Updated prior solutions [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74]]
Ta muốn chuyển từ string sang list. list(my_string): sẽ chuyển chuỗi sang một list những ký tự. Còn Ta muốn chuyển 1 chuỗi sang các từ (word) ta dùng split(). Cycle SolutionUpdated prior solutions from itertools import cycle
items = range(10, 75)
number_of_baskets = 10
Ta muốn chuyển từ string sang list. baskets = [[] for _ in range(number_of_baskets)]
list(my_string): sẽ chuyển chuỗi sang một list những ký tự. Còn Ta muốn chuyển 1 chuỗi sang các từ (word) ta dùng split(). for element, basket in zip(items, cycle(baskets)):
basket.append(element)
Còn nếu muốn chuyển 1 list sang chuỗi ta dùng join(). >>> from pprint import pprint
>>> pprint(baskets)
[[10, 20, 30, 40, 50, 60, 70],
[11, 21, 31, 41, 51, 61, 71],
[12, 22, 32, 42, 52, 62, 72],
[13, 23, 33, 43, 53, 63, 73],
[14, 24, 34, 44, 54, 64, 74],
[15, 25, 35, 45, 55, 65],
[16, 26, 36, 46, 56, 66],
[17, 27, 37, 47, 57, 67],
[18, 28, 38, 48, 58, 68],
[19, 29, 39, 49, 59, 69]]
Như vậy ta đã biết cách dùng list, split and join rồi đó. Easy easy ^_^. from itertools import cycle
from typing import List, Any
def cycle_baskets(items: List[Any], maxbaskets: int) -> List[List[Any]]:
baskets = [[] for _ in range(min(maxbaskets, len(items)))]
for item, basket in zip(items, cycle(baskets)):
basket.append(item)
return baskets
"Evenly sized chunks", to me, implies that they are all the same length, or barring that option, at minimal variance in length. E.g. 5 baskets for 21 items could have the following results: A practical reason to prefer the latter result: if you were using these functions to distribute work, you've built-in the prospect of one likely finishing well before the others, so it would sit around doing nothing while the others continued working hard.When I originally wrote this answer, none of the other answers were evenly sized chunks - they all leave a runt chunk at the end, so they're not well balanced, and have a higher than necessary variance of lengths.step argument to slices. i.e.: # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
0For example, the current top answer ends with: Others, like # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
9, and list4 = ["cat", "dog", "lion"]
my_string_3 = "-".join(list4)
print(my_string_3) # cat-dog-lion
0 both return: list4 = ["cat", "dog", "lion"]
my_string_3 = "-".join(list4)
print(my_string_3) # cat-dog-lion
1. The list4 = ["cat", "dog", "lion"]
my_string_3 = "-".join(list4)
print(my_string_3) # cat-dog-lion
2's are just padding, and rather inelegant in my opinion. They are NOT evenly chunking the iterables.# split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
1Why can't we divide these better? A high-level balanced solution using list4 = ["cat", "dog", "lion"]
my_string_3 = "-".join(list4)
print(my_string_3) # cat-dog-lion
3, which is the way I might do it today. Here's the setup:# split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
2Now we need our lists into which to populate the elements: # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
3Updated prior solutionsTa muốn chuyển từ string sang list. # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
4list(my_string): sẽ chuyển chuỗi sang một list những ký tự. Còn Ta muốn chuyển 1 chuỗi sang các từ (word) ta dùng split(). # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
5Còn nếu muốn chuyển 1 list sang chuỗi ta dùng join(). # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
6Như vậy ta đã biết cách dùng list, split and join rồi đó. Easy easy ^_^."Evenly sized chunks", to me, implies that they are all the same length, or barring that option, at minimal variance in length. E.g. 5 baskets for 21 items could have the following results: # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
7A practical reason to prefer the latter result: if you were using these functions to distribute work, you've built-in the prospect of one likely finishing well before the others, so it would sit around doing nothing while the others continued working hard. # split break a string into word.
list2 = my_string.split()
print(list2) # ['Hello', 'world', '2020']
my_string2 = "hello-world-2020"
list3 = my_string2.split("-")
print(list3) # ['hello', 'world', '2020']
8When I originally wrote this answer, none of the other answers were evenly sized chunks - they all leave a runt chunk at the end, so they're not well balanced, and have a higher than necessary variance of lengths.
|