Sort each string inside list - Python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to sort each string inside a list. For example, if I have the list ['bca', 'fed'] then I want my function to return ['abc', 'def'].
Currently trying to do this like so:
lines = [''.join.(sorted(e)) for e in lines]
What's wrong with this syntax?

Just remove the redundant dot (.):
lines = [''.join(sorted(e)) for e in lines]

Related

Why my List show a string separate the characters and showing it as list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
My current csv file :
'Date','Category','Ability'
'21,14,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'
'21,14,5','Penguin','water,land'
my code:
Living_beings=[]
with open(users_read,'r') as f:
reader=DictReader(f)
for row in reader:
if date.today().day in row['Date']:
Living_beings+=row['Category']
print(Living_beings)
Output ; ['S','p','a','r','r','o','w','P','e','n','g','u','i','n']
Expected output: [Sparrow, penguin]
I am not sure why it was split up...Any ideas on this.
The line Living_beings+=row['Category'] treats the contents of row['Category'] as a list of characters (because it is a string), and extends the Living_beings list with the row['Category'] list of characters.
Instead, you should use:
Living_beings.append(row['Category'])
Try Living_beings.append(row['Category']) instead.
My suspicion is the original code is treating row['Category'] as a list of individual characters so it can combine the lists.

Can Someone help me in removing two single quotes for a string inside list in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
list1 = [''ABC'',''XYZ'',''PQR'',''MNS'']
to
list1 = ['ABC','XYZ','PQR','MNS']
#I am using this code but it is not working
for i in range(0,len(list1)):
list1[i] = list1[i].replace("''","")
I find list comprehension to be the cleanest solution:
list2 = [s.replace("'", "") for s in list1]
In the snippet above, I iterate through the list binding s to each of the characters (as we traverse the list). In each iteration, I do the replacement.

Use Sum() function with lists [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is the correct syntax for using Sum() function in python for lists?
Say we have a list called list_1. When I tried these two syntax I get the same result:
total_1 = Sum(list_1)
total_2 = list_1.Sum()
What is the best syntax for that?
To sum a list of integers or floats, the correct method is:
list_1 = [1, 2, 3]
total = sum(list_1)
print(total)
which results in the output:
6

How to pass Lists as arguments using for loop in a function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
def conversion(putlist):
if(len(putlist)>len(set(putlist))):
putlist = set(putlist)
putlist = list(putlist)
print("{}have duplicates".format(putlist))
else:
putlist = set(putlist)
putlist = list(putlist)
print(putlist)
for i in range[brent, camden, redbridge, southwark]:
conversion(i)
error
1
Neglect the indentation
I want to pass 4 lists as arguments to function conversion() with the help of for loop so I ain't have to pass the arguments 4 times.
Replace:
for i in range[brent, camden, redbridge, southwark]: <-------getting error
with
for i in [brent, camden, redbridge, southwark]:
https://www.w3schools.com/python/ref_func_range.asp
range - generator, this function makes a list for you
You are not needed in this function if you already have a list
wiki page about generator: https://wiki.python.org/moin/Generators
for i in [brent, camden, redbridge, southwark]:
conversion(*i)

how to unpack a tuple in this form ('abc', [20, 20])? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want this to be unpacked in two variable for examaple.
something like..
a,*b = ('abc', [20,20])
i want a to contain 'abc' and b to hold the list [20,20].
Simply:
a,b = 'abc', [20,20]
If you removed the asterix, your code would have worked
a, b = ('abc', [20,20])
It is that simple. Afterwards:
a is 'abc'
b is [20,20]

Categories

Resources