- Trong Python, tôi có thể chỉ đơn giản là ++ một char không?
- Một cách hiệu quả để làm điều này là gì?
Tôi muốn lặp lại thông qua các URL và tạo chúng theo cách sau:
www.website.com/term/#
www.website.com/term/a
www.website.com/term/b
www.website.com/term/c
www.website.com/term/d ...
www.website.com/term/z
Jayrizzo
2.7923 huy hiệu vàng31 Huy hiệu bạc42 Huy hiệu đồng3 gold badges31 silver badges42 bronze badges
hỏi ngày 19 tháng 6 năm 2013 lúc 3:59Jun 19, 2013 at 3:59
MillsonwheelsmillsonwheelsMillsOnWheels
1.2832 huy hiệu vàng8 Huy hiệu bạc4 Huy hiệu đồng2 gold badges8 silver badges4 bronze badges
1
Bạn có thể sử dụng string.ascii_lowercase
đơn giản là một chuỗi các chữ cái viết thường thuận tiện,
Python 2 Ví dụ:
from string import ascii_lowercase
for c in ascii_lowercase:
# append to your url
Python 3 Ví dụ:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from string import ascii_lowercase as alc
for i in alc:
print[f"www.website.com/term/{i}"]
# Result
# www.website.com/term/a
# www.website.com/term/b
# www.website.com/term/c
# ...
# www.website.com/term/x
# www.website.com/term/y
# www.website.com/term/z
Hoặc nếu bạn muốn tiếp tục làm tổ, bạn có thể làm như vậy:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
for i in alc:
print[f"www.website.com/term/{i}"]
for j in alc:
print[f"www.website.com/term/{i}{j}"]
# Result
# www.website.com/term/a
# www.website.com/term/aa
# www.website.com/term/ab
# www.website.com/term/ac
# ...
# www.website.com/term/ax
# www.website.com/term/ay
# www.website.com/term/az
# www.website.com/term/b
# www.website.com/term/ba
# www.website.com/term/bb
# www.website.com/term/bc
# ...
# www.website.com/term/bx
# www.website.com/term/by
# www.website.com/term/bz
# www.website.com/term/c
# www.website.com/term/ca
# www.website.com/term/cb
# www.website.com/term/cc
# ...
# ...
# ...
# www.website.com/term/z
# www.website.com/term/za
# www.website.com/term/zb
# www.website.com/term/zc
# www.website.com/term/zd
# ...
# www.website.com/term/zx
# www.website.com/term/zy
# www.website.com/term/zz
Jayrizzo
2.7923 huy hiệu vàng31 Huy hiệu bạc42 Huy hiệu đồng3 gold badges31 silver badges42 bronze badges
hỏi ngày 19 tháng 6 năm 2013 lúc 3:59Jun 19, 2013 at 4:00
1
Millsonwheelsmillsonwheels
1.2832 huy hiệu vàng8 Huy hiệu bạc4 Huy hiệu đồng
Bạn có thể sử dụng string.ascii_lowercase
đơn giản là một chuỗi các chữ cái viết thường thuận tiện,Jun 19, 2013 at 4:10
Python 2 Ví dụ:Brian
Python 3 Ví dụ:15 silver badges29 bronze badges
2
Hoặc nếu bạn muốn tiếp tục làm tổ, bạn có thể làm như vậy:
for c in list[map[chr,range[ord['a'],ord['z']+1]]]:
do_something[base_url+c]
Đã trả lời ngày 19 tháng 6 năm 2013 lúc 4:00
def plusplus[oldChar]:
return chr[ord[oldChar]+1]
plusplus['a'] # output - b
Ngoài string.ascii_lowercase
, bạn cũng nên xem các bản dựng ord
và
from string import ascii_lowercase
for c in ascii_lowercase:
# append to your url
0. from string import ascii_lowercase
for c in ascii_lowercase:
# append to your url
1 sẽ cung cấp cho bạn giá trị ASCII cho from string import ascii_lowercase
for c in ascii_lowercase:
# append to your url
2 và from string import ascii_lowercase
for c in ascii_lowercase:
# append to your url
3 sẽ cung cấp lại cho bạn chuỗi from string import ascii_lowercase
for c in ascii_lowercase:
# append to your url
2.url=www.website.com/term
my_char=ord['a'] # convert char to ascii
while my_char