This question already has answers here:
Join list of strings with a comma
(2 answers)
Closed 3 years ago.
Have a list of list that looks like this:
mylist = [['A'],['A', 'B'], ['A', 'B', 'C']]
Need to remove and replace all ', ' instances with a comma only (no spaces). Output should look like this:
mynewlist = [['A'],['A,B'], ['A,B,C']]
Tried the following:
mynewlist = [[x.replace("', ''",",") for x in i] for i in mylist]
This works on other characters within the nested lists (e.g. replacing 'A' with 'D', but does not function for the purpose described above (something to do with with the commas not being literal strings?).
Try this :
mynewlist = [[','.join(k)] for k in mylist]
OUTPUT :
[['A'], ['A,B'], ['A,B,C']]
Related
This question already has answers here:
Why is it string.join(list) instead of list.join(string)?
(11 answers)
Closed 2 years ago.
Here is my code to combine the three lists.
one=['nlo_90', 'nhi_76', 'nhi_88']
two=['12', '44', '84']
three=[['a','a','b','c'], ['g','a','g','g'], ['b','g','g','b']]
new_three=[list(dict.fromkeys(q)) for q in three]
z=zip(one,two,new_three)
for a,b,c in z:
print(f'a:{a},\tb:{b},\tc:{c}')
Below is the output:
a:nlo_90, b:12, c:['a', 'b', 'c']
a:nhi_76, b:44, c:['g', 'a']
a:nhi_88, b:84, c:['b', 'g']
My desired output is:
a:nlo_90, b:12, c:a, b, c
a:nhi_76, b:44, c:g, a
a:nhi_88, b:84, c:b, g
Just convert the list into a joined string with ', ' as the internal separator. Currently, it's printing the full list: ['a', 'b', 'c'].
Using ', '.join(c) will take each item in the list, and put the ', ' between them as a combined string. Since your items are strings, there isn't any conversion during the combining, so final output is: a, b, c.
for a, b, c in z:
c_str = ', '.join(c)
print(f'a:{a},\tb:{b},\tc:{c_str}')
This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(30 answers)
Closed 4 years ago.
I have a list that containts a tuple, and that tuple contains another list with values:
list
[('5c3b542e2fb59f2ab86aa81d',
['hello', 'this', 'is', 'a', 'test', 'sample', 'this', 'is', 'duplicate'])]
As you can notice 'this and 'is' are already present in this list and I would like to remove them.
So I want something like this:
new_list
[('5c3b542e2fb59f2ab86aa81d',
['hello', 'this', 'is', 'a', 'test', 'sample', 'duplicate'])]
Just to be clear, the List above contains multiple values, see my screenshot attached to this question.
So far I tried the following method:
final_list = []
for post in new_items:
if post[1] not in final_list:
_id = post[0]
text = post[1]
together = _id, text
final_list.append(together)
I tried to loop through each item in the list and if it's not in the list, it would be added to the final_list. But so far this method doesn't work and gives me the exact same list back.
One easy way to remove duplicates from a list is to first convert it to a set (sets cannot hold duplicate elements) and the convert it back to a list. Example
alist = ['a', 'b', 'a']
alist = list(set(alist))
The result of alist will be ['a', 'b']
You can add this in your for loop
This question already has answers here:
How can I partition (split up, divide) a list based on a condition?
(41 answers)
Closed 9 years ago.
For the following code:
print("Welcome to the Atomic Weight Calculator.")
compound = input("Enter compund: ")
compound = H5NO3
lCompound = list(compound)
I want to create two lists from the list lCompund. I want one list for characters and the other for digits. So that I may have something looking like this:
n = ['5' , '3']
c = ['H' , 'N' , 'O']
Can somebody please help by providing a simple solution?
Use a list comprehension and filter items using str.isdigit and str.isalpha:
>>> compound = "H5NO3"
>>> [c for c in compound if c.isdigit()]
['5', '3']
>>> [c for c in compound if c.isalpha()]
['H', 'N', 'O']
Iterate the actual string only once and if the current character is a digit, then store it in the numbers otherwise in the chars.
compound, numbers, chars = "H5NO3", [], []
for char in compound:
(numbers if char.isdigit() else chars).append(char)
print numbers, chars
Output
['5', '3'] ['H', 'N', 'O']
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create a list with the characters of a string?
Example:
'abc'
becomes
['a', 'b', 'c']
Is it a combination of split and slicing?
>>> x = 'abc'
>>> list(x)
['a', 'b', 'c']
Not sure what you are trying to do, but you can access individual characters from a string itself:
>>> x = 'abc'
>>> x[1]
'b'
If you need to iterate over the string you do not even need to convert it to a list:
>>> n = 'abc'
>>> for i in n:
... print i
...
a
b
c
or
>>> n[1]
'b'
yourstring = 'abc'
[char for char in yourstring]
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 6 years ago.
How can I turn a string (like 'hello') into a list (like [h,e,l,l,o])?
The list() function [docs] will convert a string into a list of single-character strings.
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
Even without converting them to lists, strings already behave like lists in several ways. For example, you can access individual characters (as single-character strings) using brackets:
>>> s = "hello"
>>> s[1]
'e'
>>> s[4]
'o'
You can also loop over the characters in the string as you can loop over the elements of a list:
>>> for c in 'hello':
... print c + c,
...
hh ee ll ll oo