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))
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 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 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
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 6 months ago.
Improve this question
How do I print certain elements of a list for example:
list1=["0","0","0","0","0","0","Element1","0","0","0","0"]
Is there any simple way to print only Element1 that specifies that you should not print out anything that is equal to 0.
Use a list comprehension or (as in this example) a generator expression to filter out the "0" items, and loop through the filtered list:
for item in (x for x in list1 if x != "0"):
print(item)
This prints all items that are not "0".
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]