How to generate a list comprehension with a conditional where the conditional is a NameError - python-3.x

Suppose the following list:
l = ['1','2','M']
How can I transform l to l_1 via a list comprehension?
l_1 = [1, 2, 'M']
I attempted the below without success.
[eval(c) if eval(c) not NameError else c for c in l]
File "<ipython-input-241-7c3f63ffe51b>", line 1
[eval(c) if c not NameError else c for c in list(a)]
^
SyntaxError: invalid syntax

Hi,
You could validate each element of the list l as strings, even if you already have some digits on it such as l = ['1','2','M',3,'p'], that way you have that validation there by passing them all to string and then verifying if the element is digit, and if so, you pass it to int, of float, or number to the new l_1 and if it is not, you simply pass it as string. In this case I will convert each numeric element to int, but you could choose to pass it to number in any other type such as float, for example, if the list contains floating elements.
l = ['1','2','M',3,'p']
l_1 = [int(el) if str(el).isdigit() else el for el in l]
print(l_1)
I certainly hope this helps, good luck and keep coding, it's always fun!

Assuming your general case looks like this, you could use isdigit to only convert numbers.
l=['1','2','M']
l1=[int(c) if c.isdigit() else c for c in l]

Related

how do i replace character from string in list?

I wanted to try to make a simple game to play in my terminal and I am stuck on this problem.
I have tried
l[y][x] = l[y][x].replace(' ','2')
And
l[y][x] = '2'
And all of these returns a type error
'str' object does not support item assignment.
how do I solve the problem above?
Edit:
l contains
['111111111',
'120000001',
'100000001',
'100000001',
'111111111'
]
You need to convert your list l into characters - what you have is a list of string elements.
#convert elements into characters
m = [list(x) for x in l]
print(m)
print(m[1][2])
m[1][2] = m[1][2].replace('0', '9')
print(m[1][2])

How to convert a tuple list string value to integer

I have the following list:
l = [('15234', '8604'), ('15238', '8606'), ('15241', '8606'), ('15243', '8607')]
I would like to converted it such that the tuple values are integers and not string. How do I do that?
Desired output:
[(15234, 8604), (15238, 8606), (15241, 8606), (15243, 8607)]
What I tried so far?
l = [('15234', '8604'), ('15238', '8606'), ('15241', '8606'), ('15243', '8607')]
new_list = []
for i in `l:
new_list.append((int(i[0]), i[1]))
print(tuple(new_list))
This only converts the first element i.e. 15234, 15238, 15241, 15243 into int. I would like to convert all the values to int. How do I do that?
The easiest and most concise way is via a list comprehension:
>>> [tuple(map(int, item)) for item in l]
[(15234, 8604), (15238, 8606), (15241, 8606), (15243, 8607)]
This takes each tuple in l and maps the int function to each member of the tuple, then creates a new tuple out of them, and puts them all in a new list.
You can change the second numbers into integers the same way you did the first. Try this:
new_list.append((int(i[0]), int(i[1]))

Conditional string splitting in Python using regex or loops

I have a string c that has a respective repetitive pattern of:
integer from 0 to 10,
character S, D, or T,
special character * or # (optional)
For instance, c could look like 1D2S#10S, or 1D#2S*3S, or so on.
I have a further calculation to make with c, but in order to do so I thought splitting c into substrings that include integer, character, and a possible special character would be helpful. Hence, for example, 1D2S#10S would be split into 1D, 2S#, 10S. 1D#2S*3S would be split into 1D#, 2S*, 3S.
I am aware that such string split can be concisely done with re.split(), but since this is quite conditional, I wasn't able to find an optimal way to split this. Instead, I tried using a for loop:
clist = []
n = 0
for i in range(len(c)):
if type(c[i]) != 'int':
if type(c[i+1]) == 'int':
clist.append(c[n:i+1])
n = i
else:
clist.append(c[n:i+2])
n = i
This raises an indexing issue, but even despite that I can tell it isn't optimal. Is there a way to use re to split it accordingly?
Use re.findall():
>>> re.findall(r'\d*[SDT][\*#]?', '1D2S#10S')
['1D', '2S#', '10S']
>>> re.findall(r'\d*[SDT][\*#]?', '1D#2S*3S')
['1D#', '2S*', '3S']

how do I call an element in a list?

a= int(input())
# I input 12345
b = a
list(map(int, b))
print (list[0]*2+list[3]*1)
#can't seem to get 6 as my answer
how do I attain my answer? I can't seem to call the elements in the list. Thank you for your help.
Since you're treating the input as individual digits, you should avoid converting the input to an integer as a whole, but map the individual digits to integers as a sequence of characters:
a= input()
b = list(map(int, a))
print(b[0] * 2 + b[3] * 1)
There are several reasons why your code won't work, including your use of the map function, the fact that you do not assign the result to a variable and the use of list (which is a keyword in Python).
However, consider this code snippet which calculates your desired output:
a = int(input('Enter a number: '))
b = [int(digit) for digit in str(a)]
res = 2 * b[0] + b[3]
print(res)
Basically you have to transform your integer into a string to be able to iterate over it. Afterwards you create your list of digits out of it and can do your calculations.
Generally speaking, you should learn the basics of Python properly. A good starting point would be the official documentation (LINK).

iteration and matching items in lists

Am trying to check if elements of a list match elements of another. But there is a slight twist to the problem.
alist = ['949', '714']
blist = ['(714)824-1234', '(419)312-8732', '(949)555-1234', '(661)949-2867']
Am trying to match the elements of alist to the blist, but only the area code part(in blist). Here is my current code:
def match_area_codes(alist, blist):
clist =[]
for i in alist:
for j in blist:
if i in j:
clist.append(j)
return clist
The code works for the most part, except when there is a string matching the area code anywhere else in the list. It should only print:
['(714)824-1234', '(949)555-1234']
but it ends up printing
['(714)824-1234', '(949)555-1234', '(661)949-2867']
as there is a '949' in the last phone number. Is there a way to fix this?
You can use a regular expression to get the part within (...) and compare that part to alist.
import re
def match_area_codes(alist, blist):
p = re.compile(r"\((\d+)\)")
return [b for b in blist if p.search(b).group(1) in alist]
Example:
>>> alist = set(['949', '714'])
>>> blist = ['(714)824-1234', '(419)312-8732', '(949)555-1234', '(661)949-2867']
>>> match_area_codes(alist, blist)
['(714)824-1234', '(949)555-1234']
If you really really want to do it without regular expressions, you could, e.g., find the position of the ( and ) and thus get the slice from the string corresponding to the region code.
def match_area_codes(alist, blist):
find_code = lambda s: s[s.index("(") + 1 : s.index(")")]
return [b for b in blist if find_code(b) in alist]
However, I would strongly suggest to just take this as an opportunity for getting started with regular expressions. It's not all that hard, and definitely worth it!

Resources