当前位置:网站首页>String alignment method, self use, synthesis, newrlcjust
String alignment method, self use, synthesis, newrlcjust
2022-07-05 07:28:00 【work-harder】
background
- The content format of multiple strings in the list is inconsistent , There is pure Chinese , Pure English , There is also a mixture of Chinese and English .
- Use python Built-in function str.rjust Isochronous , Unable to align
- win10, anaconda 4.8.3, python 3.8.3
Way
- Calculate the middle of each string 、 Number of English single characters , Adjustment
- See code for details
- Fill character ( Fill in spaces to keep the length of characters ) Only single byte characters are supported temporarily
#
# pure English , Pure Chinese , A list of mixed English and Chinese strings (list) Align content : Right , Left , in
# After import , function chn_en_alignment.test() See how to use
# After import , function chn_en_alignment.newrlcjust(listchk, ttllength, direction ='r', fillwith = '0')
# or import chn_en_alignment.newrlcjust as rlcjust, Again according to rlcjust(' The order of the above parameters ') function
# V1, 20201016
#
# data for test
list1=['abc', 'a', 'abcded', 'aa']
list2=[' Middle core ', ' TSMC ', ' Huawei Technology ']
list3=['NXP', ' Middle core ', ' TSMC ']
list4=['NXP', ' Middle core ', 'Huawei Technology ']
list5=['123', ' in " core "', ' Hua Hua by Wei']
usage=''' Usage: newrlcjust(listchk, ttllength, direction ='r', fillwith = '0') listchk: String element list, To be checked ttllength: Integers , list The width requirements of all string alignment in , ttllength: The numbers are ttllength And list Maximum length of Chinese character conversion The biggest of direction: r-right, l-left, c-center, Default r fillwith: What to fill the empty space with ( If there is ), Default 0 ( zero ), Non English or non single byte characters are not supported at the moment '''
# chn =2, en=1, return additional len
def getLen(strchk):
# get total length, chn calculated as 2, en as 1
strlen = 0
for letter in strchk:
# print(letter, flush=True)
if ord(letter) > 255:
strlen += 2
else:
strlen += 1
return strlen
def newrlcjust(listchk, ttllength, direction ='r', fillwith = '0'):
# listchk: list with elements to be aligned;
# ttllength: total length you see after output;
# direction, default as 'r', one of the three 'r', or 'l', or 'c',
# fillwith, default as '0';
listchkLenmax=max(getLen(x) for x in listchk)
for strchk in listchk:
difflength = 0
if ttllength > listchkLenmax:
listchkLenmax = ttllength
ttl = listchkLenmax - (getLen(strchk)-len(strchk))
if direction == 'r':
print(strchk.rjust(ttl, fillwith))
elif direction == 'l':
print(strchk.ljust(ttl, fillwith))
else:
print(strchk.center(ttl, fillwith))
def test():
print('\n Chinese numeration 2 Width , English numeration 1 Width .')
print('len(str) Middle 、 A single part of English , Average position length 1')
print(usage)
print( "*" * 66, '\n')
print('following are examples: \n')
i_sequence = 0
for lst in [list1, list2, list3, list4, list5]:
print('lst:',lst)
direction=['r','l','c'][i_sequence]
print("example as: newrlcjust(lst, ttllength=1, direction ='" +direction + "', fillwith = '0')")
newrlcjust(lst, ttllength=1, direction =direction, fillwith = '0')
print('\n')
i_sequence += 1
i_sequence = i_sequence % 3
if __name__ == '__main__':
# newrlcjust(list4, 20, direction ='r', fillwith = '…')
# fill Of … , Chinese input method shift + 6, Again backspace once . powershell Cannot enter under
test()
边栏推荐
- Batch convert txt to excel format
- selenium 元素定位
- Ggplot2 drawing learning notes in R
- (top) pretty girl binary color code portal
- 并查集理论讲解和代码实现
- 611. Number of effective triangles
- [node] NVM version management tool
- Concurrent programming - deadlock troubleshooting and handling
- Idea to view the source code of jar package and some shortcut keys (necessary for reading the source code)
- Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
猜你喜欢

Rough notes of C language (1)

Daily Practice:Codeforces Round #794 (Div. 2)(A~D)

Microservice registry Nacos introduction

Three body goal management notes

DataGrid offline installation of database driver

611. 有效三角形的个数

Don't confuse the use difference between series / and / *

Concurrent programming - deadlock troubleshooting and handling

Daily Practice:Codeforces Round #794 (Div. 2)(A~D)

Play with grpc - go deep into concepts and principles
随机推荐
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
Microservice registry Nacos introduction
Netease to B, soft outside, hard in
Matrix and TMB package version issues in R
SOC_ SD_ CMD_ FSM
M2dgr slam data set of multi-source and multi scene ground robot
And play the little chestnut of dynamic agent
docker安装mysql并使用navicat连接
[software testing] 02 -- software defect management
The mutual realization of C L stack and queue in I
I implement queue with C I
Mathematical analysis_ Notes_ Chapter 8: multiple integral
arcpy. SpatialJoin_ Analysis spatial connection analysis
(tool use) how to make the system automatically match and associate to database fields by importing MySQL from idea and writing SQL statements
611. Number of effective triangles
Graduation thesis project local deployment practice
借助 Navicat for MySQL 软件 把 不同或者相同数据库链接中的某数据库表数据 复制到 另一个数据库表中
I 用c I 实现队列
Course learning accumulation ppt
[software testing] 06 -- basic process of software testing