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]
Related
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
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 2 years ago.
Improve this question
I have the following list:
a=['c1','c2','c3']
And I would like to get to:
b=[sametext['c1'],sametext['c2'],sametext['c3']]
I've tried to make list in list but I'm not able to get any result? How can I get to b?
In [sametext['c1'],sametext['c2'],sametext['c3']], is sametext a dictionary which contains a mapping of some sort?
If that's the case then the way to do it will be with this list comprehension:
b = [sametext[x] for x in a]
Without list comprehensions:
b=[]
for x in a:
b.append(sametext[x])
If by sametext all you mean is some constant operation, like adding a prefix then similar to the first approach the way would be:
b = [f"yourprefix_{x}]" for x in a]
b= [ele+'some_text' for ele in a]
To make change in-place
a[:] = [ele+'some_text' for ele in a]
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 2 years ago.
Improve this question
I have the list of sorted tuples, containing floats. But when I try to output the floats (without commas and brackets) it output separate tuples BUT with brackets and commas, which I do not need.
This is the part of the code:
data=[tuple1, tuple2, tuple3, tuple4]
a=sorted(data, key = lambda x: (x[0], x[1]))
b="\n".join(map(str,a))
print(b)
try this:
data=[tuple1, tuple2, tuple3, tuple4]
s=''
for i in range(len(a)):
for j in a[i]:
s += str(j)
print(s)
Try this:
print('\n'.join(' '.join(map(str, x)) for x in a))
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])
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')]