During phash computation original image is resized. hashSize parameter basically controls height and width of resized image.
Algorithm can be found here. Implementation of the first step (reduce size):
image = image.convert("L").resize((hash_size, hash_size), Image.ANTIALIAS)
See sources of imagehash.phash
Lets see what the line imghash3.append(bin( int(imghash2, 16))[2:].zfill(64)) does.
In [16]: imghash2 = '11b97c7eb158ac'
First of all it converts hexadecimal string into integer
In [17]: int(imghash2, 16)
Out[17]: 4989018956716204
The builtin bin function is applied to convert the integer into a binary string
In [18]: bin( int(imghash2, 16))
Out[18]: '0b10001101110010111110001111110101100010101100010101100'
Drops first two characters using list slice
In [19]: bin( int(imghash2, 16))[2:]
Out[19]: '10001101110010111110001111110101100010101100010101100'
Adds 0 on the left side to make a string of 64 characters total
In [20]: bin( int(imghash2, 16))[2:].zfill(64)
Out[20]: '0000000000010001101110010111110001111110101100010101100010101100'
Answer from Konstantin on Stack OverflowYes, there are the string justification methods, ljust and rjust.
>>> '12'.rjust(5, '#')
'###12'
>>> 'txt'.rjust(5, ' ')
' txt'
>>> '12'.ljust(5, '#')
'12###'
If all you need is simple padding, I'd go with @PM2Ring's answer, but there is another, more versatile way, using the str.format method (Python 2.6 onward). This method allows you to interpolate the format specifier by nesting the replacement fields:
'{string:{fill}>{num}}'.format(string=string, fill=fill, num=num)
Replace > with < if you need to left-align the string instead.
there is no inbuilt zfill but I use this zfl function
def zfl(s, width):
# Pads the provided string with leading 0's to suit the specified 'chrs' length
# Force # characters, fill with leading 0's
return '{:0>{w}}'.format(s, w=width)
This might be useful to you? Just pass the string and the string width you require.
welcome to SO!
No, MicroPython does not have a zfill method on strings.
If you're looking for a specific width, you'll need to get a len(str) and then concatenate the desired string of "0"s to the start of string.
You can use format string syntax 02.
change the line : print(*range(1,n+1)) to print(*(f"{i:02}" for i in range(1, n + 1)))
Full code:
n = 15
for i in range(1, n + 1):
print(*(f"{i:02}" for i in range(1, n + 1)))
n = n - 1
output:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
01 02 03 04 05 06 07 08 09 10 11 12 13 14
01 02 03 04 05 06 07 08 09 10 11 12 13
01 02 03 04 05 06 07 08 09 10 11 12
01 02 03 04 05 06 07 08 09 10 11
01 02 03 04 05 06 07 08 09 10
01 02 03 04 05 06 07 08 09
01 02 03 04 05 06 07 08
01 02 03 04 05 06 07
01 02 03 04 05 06
01 02 03 04 05
01 02 03 04
01 02 03
01 02
01
Another option is to use zfill like :
print(*(str(i).zfill(2) for i in range(1, n + 1)))
you need to convert the i into str because zfill is a method of string objects.
There are many ways to do this. Here's one of them:
n = 15
for i in range(1, n+1):
print(' '.join(f'{j:02d}' for j in range(1, n-i+2)))
You could use str.format:
def formatBusinessCodes(code):
""" Function that formats business codes. Pass in a business code which will convert to a string with 6 digits """
return '{:06d}'.format(code)
In [23]: formatBusinessCodes(1)
Out[25]: '000001'
In [26]: formatBusinessCodes(10)
Out[26]: '000010'
In [27]: formatBusinessCodes(123)
Out[27]: '000123'
The format {:06d} can be understood as follows:
{...}means replace the following with an argument fromformat, (e.g.code).:begins the format specification0enables zero-padding6is the width of the string. Note that numbers larger than 6 digits will NOT be truncated, however.dmeans the argument (e.g.code) should be of integer type.
Note in Python2.6 the format string needs an extra 0:
def formatBusinessCodes(code):
""" Function that formats business codes. Pass in a business code which will convert to a string with 6 digits """
return '{0:06d}'.format(code)
parser.add_argument('-b',help='Specify length of the district code')
businessformat=args.d
businessformat=businessformat.strip()
df2['business_code']=df2['business_code'].apply(lambda x: str(x))
def formatBusinessCodes(code):
bus=code bus.zfill(4)
return bus
formatBusinessCodes(businessformat)
You are doing zfill on list object. Instead, you need to perform zfill on each item of list. Below is the sample example for range 10:
>>> a = range(0, 10)
# v this value represent the count of zeros
# v It should be `7` in your case
>>> [str(i).zfill(10) for i in a]
['0000000000', '0000000001', '0000000002', '0000000003', '0000000004', '0000000005', '0000000006', '0000000007', '0000000008', '0000000009']
As per the str.zfill() document:
string.zfill(s, width)
Pad a numeric string s on the left with zero digits until the given width is reached. Strings starting with a sign are handled correctly.
you can also let str.format handle the filling:
for a in range(1000000):
print('{:07d}'.format(a))
As maybe a alternative more portable [1] and efficient [2], actually you can just use str.ljust.
In [2]: '190'.ljust(8, '0')
Out[2]: '19000000'
In [3]: str.ljust?
Docstring:
S.ljust(width[, fillchar]) -> str
Return S left-justified in a Unicode string of length width. Padding is
done using the specified fill character (default is a space).
Type: method_descriptor
[1] format is not present on old python versions. format specifier was added since Python 3.0 (see PEP 3101) and Python 2.6.
[2] reverse twice is an expensive operation.
See Format Specification Mini-Language:
In [1]: '{:<08d}'.format(190)
Out[1]: '19000000'
In [2]: '{:>08d}'.format(190)
Out[2]: '00000190'
This also works with the Formatted String Literals, or f-strings for short (New in version 3.6):
In [1]: f'{190:<08d}'
Out[1]: '19000000'
In [2]: f'{190:>08d}'
Out[2]: '00000190'
The answer depends on the used data types by pandas. You can check this printing df.dtypes.
If all data types are integers, than this will work:
import pandas as pd
df = pd.DataFrame([[1.0, 2, 3],[4,5,6], [6,7,8], [11,22,33]], columns=['A', 'B', 'C'])
df[['A','B']] = df[['A','B']].apply(lambda x: x.astype(str).str.zfill(2))
If at least on value is of type float, you have to convert the value to an integer first, than to a string and call zfill() afterwards.
df = pd.DataFrame([[1.0, 2, 3],[4,5,6], [6,7,8], [11,22,33]], columns=['A', 'B', 'C'])
df.dtypes
df[['A','B']] = df[['A','B']].apply(lambda x: x.astype(int).astype(str).str.zfill(2))
In both cases the output is
A B C
0 01 02 3
1 04 05 6
2 06 07 8
3 11 22 33
This answer is only an extention of the first one by Mondaa.
First, you need to assign the new columns to your data frame after changing the column types. You can change the column types to string by :
df[['A','B']] = df[['A','B']].astype(str)
Then, use the lambda function to apply the zfill mehtod:
df[['A','B']] = df[['A','B']].apply(lambda x: x.str.zfill(2))
The output in your input case would be like:
A B C
0 01 02 3
1 04 05 6
2 06 07 8
3 11 22 33
note: the last row is unaffected as it is already of 2 digit numbers.