I have a list which contains the following string element.
myList = ['120$My life cycle 3$121$My daily routine 2']
I perform a .split("$") operation and get the following new list.
templist = str(myList).split("$")
I want to be able to store all the integer values from this templist which are at even indexes after the split.I want to return a list of integers.
Expected output: [120, 121]
You can split at $ and use a list comprehension with str.isdigit() to extract numbers:
mylist = ['120$My life cycle$121$My daily routine','some$222$othr$text$42']
# split each thing in mylist at $, for each split-result, keep only those that
# contain only numbers and convert them to integer
splitted = [[int(i) for i in p.split("$") if i.isdigit()] for p in mylist]
print(splitted) # [[120, 121], [222, 42]]
This will produce a list of lists and convert the "string" numbers into integers. it only works for positive numbers-strings without sign - with sign you can exchange isdigit() for another function:
def isInt(t):
try:
_ = int(t)
return True
except:
return False
mylist = ['-120$My life cycle$121$My daily routine','some$222$othr$text$42']
splitted = [[int(i) for i in p.split("$") if isInt(i) ] for p in mylist]
print(splitted) # [[-120, 121], [222, 42]]
To get a flattened list no matter how many strings are in myList:
intlist = list(map(int,( d for d in '$'.join(myList).split("$") if isInt(d))))
print(intlist) # [-120, 121, 222, 42]
Updated version:
import re
myList = ['120$My life cycle 3$121$My daily routine 2']
myList = myList[0].split('$')
numbers = []
for i in range(0,len(myList),2):
temp = re.findall(r'\d+', myList[i])[0]
numbers.append(temp)
'''
.finall() returns list of all occurences of a pattern in a given string.
The pattern says map all digits in the string. If they are next to each
other count them as one element in final list. We use index 0 of the
myList as thats the string we want to work with.
'''
results = list(map(int, numbers)) # this line performs an int() operation on each of the elements of numbers.
print(results)
Why not just use re?
re is a library for regular expresions in python. They help you find patterns.
import re
myList = ['120$My life cycle 3$121$My daily routine 2']
numbers = re.findall(r'\d+$', myList[0])
'''
.finall() returns list of all occurences of a pattern in a given string.
The pattern says map all digits in the string. If they are next to each
other count them as one element in final list. We use index 0 of the
myList as thats the string we want to work with.
'''
results = list(map(int, numbers)) # this line performs an int() operation on each of the elements of numbers.
print(results)
First off we split string with '$' as separator. And then we just iterate through every other result from the new list, convert it into integer and append it to results.
myList = ['120$My life cycle 3$121$My daily routine 2']
myList = myList[0].split('$')
results = []
for i in range(0,len(myList),2):
results.append(int(myList[i]))
print(results)
# [120, 121]
Do something like this?
a = ['100$My String 1A$290$My String 1B']
>>> for x in a:
... [int(s) for s in x.split("$") if s.isdigit()]
...
[100, 290]
Related
I need a function that takes in a string, and creates a new list every time there are two whitespace characters.
my_text = '101\n102\n103\n\n111\n112\n113\n'
I want to create a new list each time there's two whitespace characters.
[101, 102, 103]
[111, 112, 113]
I've tried:
def my_list(*args, end='\n\n'):
for arg in args:
new_list = []
if end:
new_list.append(arg)
print(new_list)
but that just adds all the numbers to a single list.
Just append to the final entry in the list, when you get an empty line, add a new empty list. I assume here that you're passing in the whole string. Your *args thing doesn't make much sense, but you didn't show us how it would be called.
def my_list(text):
result = [[]]
for arg in text.split('\n'):
if not arg:
result.append([])
else:
result[-1].append(int(arg))
result.pop()
return result
You could use the split() method to split the string on the '\n\n' and '\n' delimiter
my_text_parts = my_text.split('\n\n')
for text_part in my_text_parts:
numbers = text_part.split('\n')
new_list = []
for number in numbers:
new_list.append(int(number))
print(new_list)
You can do it like this:
def create_lists(my_text):
# Split the string into a list of substrings by two '\n' characters
substrings = my_text.rstrip().split('\n\n')
# Create a list of lists by splitting each substring by '\n' characters
lists = [substring.split('\n') for substring in substrings]
# Convert the list of lists to a list of lists of integers
lists = [[int(x) for x in lst] for lst in lists]
# Return the list of lists
return lists
In this function, we first split the input string into a list of substrings by two \n characters using the split() method. Then, we create a list of lists by splitting each substring by \n characters using a list comprehension.
Next, we convert the list of lists to a list of lists of integers by using a nested list comprehension that converts each string element in the list to an integer using the int() function. Finally, we return the list of lists of integers.
You can test the function using the following code:
my_text = '101\n102\n103\n\n111\n112\n113\n'
lists = create_lists(my_text)
print(lists)
[[101, 102, 103], [111, 112, 113]]
So I just went into python not too long ago, it is to develop my OCR project. I want the software to detect the character "A" and convert it to a set of integers like 101.
list=['haha', 'haaa']
I am thinking of using a dictionary with keys and item to try replacing it. I added a define function for the process. I use this method I found in other post but it doesn't work.
Dictionary={'a':101,'h':111}
for a,b in Dictionary.items():
list = list.replace(a.lower(),b)
print (list)
First, you should make sure your list variable is not list as this is a keyword in python. Then, loop through the items and replace the key with the value at the key as such:
l = ['haha', 'haaa']
refDict = {'a':101,'h':111}
for i, item in enumerate(l):
for key in refDict:
item = item.replace(key, str(refDict[key]))
l[i] = item
Output after this code:
['111101111101', '111101101101']
Never use list as variable since it is already a python function.
One can use this:
l = ['haha', 'haaa']
conv_dict = {'a':101, 'h':111}
for j, ele in enumerate(l):
ele = list(ele)
for i, char in enumerate(ele):
ele[i] = conv_dict[char.lower()]
l[j] = int( ''.join(map(str, ele)))
print(l)
>> [111101111101, 111101101101]
This is not a robuste solution, since every character should be in the conv_dict to convert the char to int.
How it works:
Go over each word in the list
Convert string to list, with each char as element
Go over each character
Replace character with integer
Join the integers to one string and then convert it back to integer
Repeat for every string in list
I'm not very sure what output you're expecting but your question seems like you want the equivalent value of the elements in the dictionary to be substituted by the key values in the dictionary.
As each element of lst is considered in the first loop, an empty string ans is initialized. It then iterates through every character in the nested loop which concatenates the dictionary equivalent of each character into ans. The end result is appended into output
Dictionary={'a':101,'h':111}
lst=['haha', 'haaa']
output = []
for i in lst:
ans = ""
for j in i:
ans+=str(Dictionary[j])
output.append(ans)
print(output)
Output
['111101111101', '111101101101']
It sounds to me like you do not need to map the characters to a specific integer, just any unique integer. I would recommend not creating your own dictionary and using the standardized ascii mappings for characters (https://www.asciitable.com/). Python has a built-in function for converting characters to that value
Here is what that might look like (as others have pointed out, you also shouldn't use list as a variable name.
words = ['haha', 'haaa']
conversions = []
for word in words:
converted_word = []
for letter in word:
converted_word.append(ord(letter))
conversions.append(converted_word)
print(conversions)
This prints:
[[104, 97, 104, 97], [104, 97, 97, 97]]
How about str.translate?
lst = ['haha', 'haaa']
table = {ord('a'): '101', ord('h'): '111'}
lst = [s.translate(table) for s in lst]
print(lst)
Output (Try it online!):
['111101111101', '111101101101']
I have a list of strings consisting of integers, and I am trying to replace them with the sum of their digits. E.g. nums = ["12","23","33"] -> nums = [3,5,6]
Here is my code:
strng = ['12','23','33']
for i in range(len(strng)):
print(list((map(lambda x:int[x],list(strng[i])))))
For the above I am getting a TypeError: 'type' object is not subscriptable. It works up until map(), but when I add the list(map(...)), I get this error.
Any ideas how to fix it?
My after this is fixed, my idea is to do the following:
strng = ['12','23','33']
for i in range(len(strng)):
strng[i] = sum(list((map(lambda x:int[x],list(strng[i]))))))
Which should replace each of strng with the sum of its digits.
The error you're getting is you because you wrote int[x] instead of int(x). However, there are some additional issues with your existing solution.
The short and pythonic solution to this problem would be:
answer = [sum(map(int, list(s))) for s in strng]
To break this down:
[... for s in strng]: this is a list comprehension
list(s): This takes each string and converts it into a list of str of each character, so "123" becomes ["1","2","3"]
map(int, list(s)): This applys the int conversion to each element in list(s), so ["1","2","3"] becomes [1,2,3]
sum(...): We take the sum of the resulting list of ints
The equivalent of the above using a normal for loop would be something like this:
answer = []
for s in strng:
list_of_chars = list(s)
list_of_ints = map(int, list_of_chars)
sum_of_ints = sum(list_of_ints)
answer.append(sum_of_ints)
You can use comprehension, and iterate each digits, convert them to integer, finally pass it to sum builtin to get sum of the values.
>>> [sum(int(i) for i in v) for v in strng]
[3, 5, 6]
Not really efficient, but try this :
strng = ['12','23','33']
def function(strng) :
my_list = []
for string in strng :
my_list.append(0)
for digit in string :
my_list[-1] += int(digit)
return my_list
strng = function(strng)
print(strng)
I have a regex that looks for numbers in a file.
I put results in a list
The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.
What I want to do is to have all the numbers into one list.
I used join() but it doesn't works.
code :
def readfile():
regex = re.compile('\d+')
for num in regex.findall(open('/path/to/file').read()):
lst = [num]
jn = ''.join(lst)
print(jn)
output :
122
34
764
What goes wrong:
# this iterates the single numbers you find - one by one
for num in regex.findall(open('/path/to/file').read()):
lst = [num] # this puts one number back into a new list
jn = ''.join(lst) # this gets the number back out of the new list
print(jn) # this prints one number
Fixing it:
Reading re.findall() show's you, it returns a list already.
There is no(t much) need to use a for on it to print it.
If you want a list - simply use re.findall()'s return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):
import re
my_r = re.compile(r'\d+') # define pattern as raw-string
numbers = my_r.findall("123 456 789") # get the list
print(numbers)
# different methods to print a list on one line
# adjust sep / end to fit your needs
print( *numbers, sep=", ") # print #1
for n in numbers[:-1]: # print #2
print(n, end = ", ")
print(numbers[-1])
print(', '.join(numbers)) # print #3
Output:
['123', '456', '789'] # list of found strings that are numbers
123, 456, 789
123, 456, 789
123, 456, 789
Doku:
print() function for sep= and end=
Printing an int list in a single line python3
Convert all strings in a list to int ... if you need the list as numbers
More on printing in one line:
Print in one line dynamically
Python: multiple prints on the same line
How to print without newline or space?
Print new output on same line
In your case, regex.findall() returns a list and you are are joining in each iteration and printing it.
That is why you're seeing this problem.
You can try something like this.
numbers.txt
Xy10Ab
Tiger20
Beta30Man
56
My45one
statements:
>>> import re
>>>
>>> regex = re.compile(r'\d+')
>>> lst = []
>>>
>>> for num in regex.findall(open('numbers.txt').read()):
... lst.append(num)
...
>>> lst
['10', '20', '30', '56', '45']
>>>
>>> jn = ''.join(lst)
>>>
>>> jn
'1020305645'
>>>
>>> jn2 = '\n'.join(lst)
>>> jn2
'10\n20\n30\n56\n45'
>>>
>>> print(jn2)
10
20
30
56
45
>>>
>>> nums = [int(n) for n in lst]
>>> nums
[10, 20, 30, 56, 45]
>>>
>>> sum(nums)
161
>>>
Use list built-in functions to append new values.
def readfile():
regex = re.compile('\d+')
lst = []
for num in regex.findall(open('/path/to/file').read()):
lst.append(num)
print(lst)
So I have a string and I want to convert it to a list
input:
"123|456|890|60"
output:
[123,456,890,60]
Another example, input:
"123"
output:
[123]
Here is what I did until now.
A=input()
n=len(A)
i=0
z=0
K=""
Y=[0]*n
while(i<n):
if(A[i]=="|"):
Y[z]=int(Y[z])
j=+1
K=""
else:
Y[z]=K+A[i]
i+=1
print(Y)
Thanks for editing in your attempt. Splitting a string and converting a string to an integer are very common tasks, and Python has built in tools to achieve them.
str.split splits a string into a list by a given delimiter.
int can convert a string to an integer. You can use map to apply a function to all elements of a list.
>>> map(int, "123|456|890|60".split('|'))
[123, 456, 890, 60]
Using list comprehension
Code:
[int(a) for a in "123|456|890|60".split("|")]
Output:
[123, 456, 890, 60]
Notes:
Split creates a list of strings here where the current strings are split at |
We are looping over the list and converting the strings into int
Here's a similar approach, using regular expressions instead:
import re
def convert_string(s):
return map(int, re.findall(r'[0-9]+', s))
Or using a list comprehension:
import re
def convert_string(s):
return [int(num) for num in re.findall(r'[0-9]+', s)]
This is a more general approach and will work for any character (in this case '|') that separates the numbers in the input string.