List on python: where has the `u` come from? [duplicate] - python

This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 8 years ago.
Quick question:
disc_list=[] #creating an empty list to put the discs into
rad = 70
for i in range(5):
disc_list.append(cmds.polyCylinder( name = 'disc' + str(i), radius = rad, height = 10)[0]) # create discs
cmds.move(0, 10*i, 0, ('disc' + str(i)))
rad = rad*0.7
print disc_list
anyone know why when I print the disc_list, this is returned:
[u'disc0', u'disc1', u'disc2', u'disc3', u'disc4']
where has the u come from?

The u simply denotes that it is a unicode string. You should not worry about it. When you print it, it will still be the same.
for i in disc_list:
print i
[OUTPUT]
disc0
disc1
disc2
disc3
disc4
Suggested link

Related

Logical oparator OR inside for loop [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
I'm making a function that identify if has two kings in a chess board
And i used an or operator but it just dosn't works. I "fix" this using if and elif but i don't get why it dosn't works with or
chessboard = {
"a1":"" ,"a2":"" ,"a3":"" ,"a4":"" ,"a5":"" ,"a6":"" ,"a7":"" ,"a8":"bking" ,
"b1":"" ,"b2":"" ,"b3":"" ,"b4":"" ,"b5":"" ,"b6":"" ,"b7":"" ,"b8":"" ,
"c1":"" ,"c2":"" ,"c3":"" ,"c4":"" ,"c5":"" ,"c6":"" ,"c7":"" ,"c8":"" ,
"d1":"" ,"d2":"" ,"d3":"" ,"d4":"" ,"d5":"" ,"d6":"" ,"d7":"" ,"d8":"" ,
"e1":"" ,"e2":"" ,"e3":"" ,"e4":"" ,"e5":"" ,"e6":"" ,"e7":"" ,"e8":"" ,
"f1":"" ,"f2":"" ,"f3":"" ,"f4":"" ,"f5":"" ,"f6":"" ,"f7":"" ,"f8":"" ,
"g1":"wking" ,"g2":"" ,"g3":"" ,"g4":"" ,"g5":"" ,"g6":"" ,"g7":"" ,"g8":"" ,
"h1":"" ,"h2":"" ,"h3":"" ,"h4":"" ,"h5":"" ,"h6":"" ,"h7":"" ,"h8":"" }
def kingschess(board):
kings = 0
print(board.values())
for a in board.values():
print(a)
if a == "bking" or "wking":
print("True")
kings += 2
isValidChessBoard(mychessboard)
you should actually use if a == "bking" or a == "wking": since the interpreter cannot infer that you are considering a for both statements

How do I use an input to call a function? [duplicate]

This question already has answers here:
How to access (get or set) object attribute given string corresponding to name of that attribute
(3 answers)
Closed 2 years ago.
how can I make a print to change to the input value?
import cryptowatch as cw
time = input("Time:") #15m, 1h , 1d
x = cw.markets.get("KRAKEN:ATOMEUR", ohlc = True, periods = [time])
print(x.of_15m[1][4]))
for example:
time = input("Time:") #1h
print(x.of_1h[1][4])
or:
time = input("Time:") #1d
print(x.of_1d[1][4])
EDIT:
I leave more information
cryptowatch-sdk
https://github.com/cryptowatch/cw-sdk-python
Module file where the functions are:(line 255)
https://github.com/cryptowatch/cw-sdk-python/blob/master/cryptowatch/resources/markets.py
I couldn't really test this properly since I don't have cryptowatch installed, but I think it would work. It uses the user's input to determine the name of an x object attribute, and then uses getattr() to retrieve its current value.
import cryptowatch as cw
time = input("Time:")
x = cw.markets.get("KRAKEN:ATOMEUR", ohlc=True, periods=[time])
interval = getattr(x, 'of_'+time, None)
if interval is not None:
print(interval[1][4])
else:
print('Error: unknown time', time)

How to write regular expression Python [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I'd like to return the value of left battery capacity parsed from the given string. It means I want to get CurrentCapacity / MaxCapacity.
data = '''
"SuperMaxCapacity" =0
"MaxCapacity": +4540;
'CurrentCapacity'= 2897,
"LegacyBatteryInfo" = {"Amperage"=18446744073709550521,"Flags"=4,"Capacity"=4540,"Current"=2897,"Voltage"=7283,"Cycle Count"=406}
"MegaMaxCapacity" = 6700
'''
This will do the job quite nicely, and will get the match, even though your data input format is quite iffy:
import re
data = '''
"SuperMaxCapacity" =0
"MaxCapacity": +4540;
'CurrentCapacity'= 2897,
"LegacyBatteryInfo" = {"Amperage"=18446744073709550521,"Flags"=4,"Capacity"=4540,"Current"=2897,"Voltage"=7283,"Cycle Count"=406}
"MegaMaxCapacity" = 6700
'''
max_capacity = re.search(r"[\"']MaxCapacity.*?[:=].*?(\d+)", data).group(1)
current_capacity = re.search(r"[\"']CurrentCapacity.*?[:=].*?(\d+)", data).group(1)
print("Max capacity:", max_capacity)
print("Current capacity:", current_capacity)
Output:
Max capacity: 4540
Current capacity: 2897

Wrong sequence for list using sorted() [duplicate]

This question already has answers here:
Sort a python list of strings with a numeric number
(3 answers)
Closed 3 years ago.
I have some images which I generate from url with random pictures. Then I try to sort them to work with it properly, but they sorting is messed up. Appreciate any advices or pointing to what I missing
Code ( image list generating ):
def image_downloader():
image_url = 'url'
for count in tqdm(range(20)):
image_data = requests.get(image_url).content
with open(f'image_{count}.jpg', 'wb') as handler:
handler.write(image_data)
sleep(0.5)
And my sorting ( trying to get it by generated picture "id" ):
local_folder_content = os.listdir('.')
images_list = list((image for image in local_folder_content if image.endswith('.jpg')))
pprint((sorted(images_list, key=lambda x: x[:-4].split('_')[1])))
Result( sorting is messed up) :
['image_0.jpg',
'image_1.jpg',
'image_10.jpg',
'image_11.jpg',
'image_12.jpg',
'image_13.jpg',
'image_14.jpg',
'image_15.jpg',
'image_16.jpg',
'image_17.jpg',
'image_18.jpg',
'image_19.jpg',
'image_2.jpg',
'image_3.jpg',
'image_4.jpg',
'image_5.jpg',
'image_6.jpg',
'image_7.jpg',
'image_8.jpg',
'image_9.jpg']
You can try something like this :
images_list.sort(key= lambda i: int(i.lstrip('image_').rstrip('.jpg')))
You have to generate all filenames with two (or more) digits:
with open(f'image_{str(count).zfill(2)}.jpg', 'wb') as handler:
Output:
image_01.jpg
image_02.jpg
image_04.jpg
In this case your images will be correctly sorted.

Transpose block of text [duplicate]

This question already has answers here:
Transposing a text file in Python
(3 answers)
Closed 8 years ago.
I have a block of ones and zeroes, in string:
1111110000111111
1110110110110111
1101010110101011
1011100110011101
0001111111111011
1000110111110111
0100010011110000
0110000001111110
0111000000110110
0000100010010100
1110110011000111
1101111111100011
1011100110000011
1101010111100001
1110110110111101
1111110000111111
I want to transpose it, as if it was a matrix - but keep it in string.
Before I start writing nested for loops, is there an easier way?
s = """1111110000111111
1110110110110111
1101010110101011
1011100110011101
0001111111111011
1000110111110111
0100010011110000
0110000001111110
0111000000110110
0000100010010100
1110110011000111
1101111111100011
1011100110000011
1101010111100001
1110110110111101
1111110000111111"""
>>> [''.join(i) for i in zip(*s.split())]
['1111010000111111',
'1110001110110111',
'1101000110101011',
'1011100010011101',
'1101110001111011',
'1110111000110111',
'0000100000010000',
'0111110000011110',
'0111111001111110',
'0000111100110100',
'1110111110010111',
'1101111111000011',
'1011100100000011',
'1101010111100011',
'1110110110111001',
'1111110000111111']
Edit
If you indeed want a single string as your output, add one more join
>>> '\n'.join(''.join(i) for i in zip(*s.split()))
'1111010000111111\n1110001110110111\n1101000110101011\n1011100010011101\n1101110001111011\n1110111000110111\n0000100000010000\n0111110000011110\n0111111001111110\n0000111100110100\n1110111110010111\n1101111111000011\n1011100100000011\n1101010111100011\n1110110110111001\n1111110000111111'

Categories