combining three list into one list [duplicate] - python

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}')

Related

How to print a jagged array vertically? [duplicate]

This question already has answers here:
Transpose list of lists
(14 answers)
Closed 3 years ago.
I have an array like this:
arr = [['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h'],
['i', 'j']]
How to get the output like this?
str = aei bfj cg dh
So basically, how to print a jagged array vertically?
from itertools import zip_longest
for row in zip_longest(*arr, fillvalue=''):
print(' '.join(row))
You can use itertools.zip_longest to stride column-wise, and then filter out when None is encountered. Then pass that as a generator expression through str.join to create a single space-delimited string.
>>> import itertools
>>> ' '.join(''.join(filter(None, i)) for i in itertools.zip_longest(*arr))
'aei bfj cg dh'

Replace Single Quotes and Comma in Nested List [duplicate]

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']]

Separating a list into two - Python [duplicate]

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']

How do I convert string characters into a list? [duplicate]

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]

How can I turn a string into a list in Python? [duplicate]

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

Categories

Resources