Python using str as bytearray [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to implement a Reed-Solomon encoder.
I start with a list of bytearray and then I have to convert all the elements of the list into str.
So now I have this list: ["bytearray(b'XXXXXXX')"]
But I have to retrieve the value from the list: "bytearray(b'XXXXXXX')" as a bytearray: bytearray(b'XXXXXXX')...
How can I perform this conversion?

I don't think you're doing it right...
If you want to convert all list elements to str, you'd use the bytearray.decode method:
In [10]: lst = [bytearray(b'XXXXXXX')]
In [11]: newlst = [x.decode('ascii') for x in lst]
In [12]: newlst
Out[12]: ['XXXXXXX']
And the reverse of that is
In [13]: [bytearray(s, 'ascii') for s in newlst]
Out[13]: [bytearray(b'XXXXXXX')]

Related

Append the values if it is more than something [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I want to append the number in A to 5 if it is more than 5.
A = [1,2,3,4,5,6,7,8,9,10]
To something like this:
A = [1,2,3,4,5,5,5,5,5,5]
You can try using map
A = [1,2,3,4,5,6,7,8,9,10]
list(map(lambda x: x if x<5 else 5, A))
You can use the min function to take the smaller of each list item and 5:
[min(i, 5) for i in A]
Demo: https://replit.com/#blhsing/InsidiousDigitalIntegrationtesting

Coverting a python list into string with comma separated in efficient way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a list of acc nos:
list1 = ['1234','3456','2345','5543','1344','5679','6433','3243','0089']
Output I need is a string:
print(output): '1234','3456','2345','5543','1344','5679','6433','3243','0089'
You can join all the values with ',' and then adding a ' before and after the string like this:
"'{0}'".format("','".join(list1))
>>> list1 = ['1234','3456','2345','5543','1344','5679','6433','3243','0089']
>>> print(','.join(["'{0}'".format(s) for s in list1]))
'1234','3456','2345','5543','1344','5679','6433','3243','0089'

How to make all numbers in a list of lists into just ints [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
Say I have
A = [[1.0,2.3,1.1],[2.2,1.3,3.2]]
and I want to cast all of those numbers into just ints to have
A = [[1,2,1],[2,1,3]]
How do we do that in python?
Try list comprehension*2:
print([[int(x) for x in i] for i in A])
Or list comprehension + map:
print([list(map(int,i)) for i in A])
Or map+map:
print(list(map(lambda x: list(map(int,x)),A)))
Simple ways all return:
[[1,2,1],[2,1,3]]
Here's an approach using a list comprehension and map:
A = [[1.0,2.3,1.1],[2.2,1.3,3.2]]
print([list(map(int, i)) for i in A])

python format string by length [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have some strings below:
123123|00|992|1111
222222|2222|19|922
997997|3333|922|77
How can I format like this (sort by len):
123123|1111|00|992
222222|2222|19|922
997997|3333|77|922
Using str.split & str.join with sorted
Demo:
s = """123123|00|992|1111
222222|2222|19|922
997997|3333|922|77"""
data = ["|".join(sorted(i.split("|"), key=len, reverse=True)) for i in s.split("\n")]
print( "\n".join(data) )
Output:
123123|1111|992|00
222222|2222|922|19
997997|3333|922|77
Try this
a='''123123|00|992|1111
222222|2222|19|922
997997|3333|922|77'''
'\n'.join(['|'.join(sorted(e.split("|"),key=len,reverse=True)) for e in a.split("\n")])
Output:
123123|1111|992|00
222222|2222|922|19
997997|3333|922|77
The OPs sort order isn't based on string length, as the length of 2 comes before the length of 3 (the very last two segments)
for that purpose, a custom sort mapping is needed
ranks = {6:0, 4:1, 2:2, 3:3}
text = """123123|00|992|1111
222222|2222|19|922
997997|3333|922|77"""
result = '\n'.join(['|'.join(sorted(line.split('|'),key=lambda x: ranks[len(x)]))
for line in text.split('\n')])
print(result)
# outputs:
123123|1111|00|992
222222|2222|19|922
997997|3333|77|922

Split elements in a list in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My code is :
L=['my', 'my']
and I want to split the items of this list so that the output becomes:
['my'],['my']
each item in a new list
Something like this, use a list comprehension:
In [109]: L=['my', 'my']
In [110]: [[x] for x in L]
Out[110]: [['my'], ['my']]
or may you wanted this:
In [129]: print ",".join(str(x) for x in [[x] for x in L] )
['my'],['my']
In [130]: print ",".join("[{0}]".format(x) for x in L)
[my],[my]

Categories