Joining characters not seperated by a particular character in lists - python

So I have an input of
6,10,47,3,6,9,54,7,9,0;3
and I put it into a list that appears as follows
['6', ',', '1', '0', ',', '4', '7', ',', '3', ',', '6', ',', '9', ',', '5', '4', ',', '7', ',', '9', ',', '0', ';', '3']
Now as you see double digit numbers are now seperate. I understand that if I use .split(',') I could have split everything perfectly from the start, but I was wondering if it is possible in this state to join the numbers that are meant to be together (the ones not seperated by a "," character) and keep them in the same place in the list. ie 1,0 are replaced by 10 in the list:
['6', ',', '10', ',',...]
I tried
def join_nums(v):
for id2, char in enumerate(v):
if id2 == len(v) - 1:
return v
elif isinstance(v[id2 + 1], int):
v[id2:id2 + 1] = ["".join(v[id2:id2 + 1])]
it runs but doesn't do anything (not even sure if close because I haven't quite got my head around enumerating lists yet!)
Can anyone push me in the right direction. Thank you.
EDIT: The ";" is not a typo. I reason I chose to not split from the start was because I needed the ","s later else I would have to rewrite a few functions. I should of specified this from the beginning, sorry.

Assuming you can still operate the original string (or you can join the list you've got back to a string), you can use re.findall() in this case. This would output the numbers and the delimiters in the same list in order:
>>> import re
>>> re.findall(r"\d+|[,;]", s)
['6', ',', '10', ',', '47', ',', '3', ',', '6', ',', '9', ',', '54', ',', '7', ',', '9', ',', '0', ';', '3']
Here \d+|[,;] would match one or more digits (\d+) or a single comma, or a single semi-colon.

Here's a simple solution that uses itertools:
lst = ['6', ',', '1', '0', ',', '4', '7', ',', '3', ',', '6', ',', '9', ',', '5', '4', ',', '7', ',', '9', ',', '0', ';', '3']
import itertools
groups = itertools.groupby(lst, key=lambda x: x.isdigit())
result = []
for is_int, vals in groups:
if is_int:
result.append(''.join(vals))
else:
result.extend(vals)
print(result) # ['6', ',', '10', ',', '47', ',', '3', ',', '6', ',', '9', ',', '54', ',', '7', ',', '9', ',', '0', ';', '3']

Try the following for loop:
x = ['6', ',', '1', '0', ',', '4', '7', ',', '3', ',', '6', ',', '9', ',', '5', '4', ',', '7', ',', '9', ',', '0', ';', '3']
y = []
for i in range(len(x)):
if i < len(x)-1:
if x[i+1] in ',;':
y.append(int(x[i]));
else:
if x[i] not in ',;':
y.append(int(x[i]+x[i+1]))
else:
y.append(int(x[i]))
print y #[6, 10, 0, 47, 7, 3, 6, 9, 54, 4, 7, 9, 0, 3]

Why not
corrected = ''.join(wrongly_split).split(',')

Related

Turning list elements into a single string

I have a list of lists in this form:
[
['3', ',', '1', ',', '0', ',', '2', ',', '0'],
['2', ',', '1', ',', '0', ',', '0', ',', '0'],
['0', ',', '1', ',', '0', ',', '3', ',', '0']
]
I'm trying to put it into a flat list where each element is the contents of each of the sub-list's contents as one string in a new list:
['3,1,0,2,0', '2,1,0,0,0', '0,1,0,3,0']
I've tried this:
for subs in newBallots:
for i in subs:
transferedBallots.append(str(i))
But it only makes every character in its own list element:
['3', ',', '1', ',', '0', ',', '2', ',', '0', '2', ',', '1', ',', '0', ',', '0', ',', '0', '0', ',', '1', ',', '0', ',', '3', ',', '0']
Any suggestions?
list comprehension should work.
I am assuming the comma in the sublist is often the element you want to remove. Else it could be another list to be checked.
my_list=[['3', ',', '1', ',', '0', ',', '2', ',', '0'], ['2', ',', '1', ',', '0', ',', '0', ',', '0'], ['0', ',', '1', ',', '0', ',', '3', ',', '0']]
my_list_concat=[",".join([v1 for v1 in v if v1 != ","]) for v in my_list]
output:
>>> my_list_concat
['3,1,0,2,0', '2,1,0,0,0', '0,1,0,3,0']
for subs in newBallots:
transferedBallots.append("".join(subs))

How do I create a list from a CSV file column?

I am fairly new to coding, and I need to put columns from a CSV file into a list. I cannot use any libraries like Pandas. This is the current code I have, but it is taking each character individually. What do I need to change so it takes the entire word?
def readfile(f):
with open(f) as csv_file:
csv_reader= csv.reader(csv_file, delimiter= ',')
for i in csv_reader:
newlist= list(i[1])
print(newlist)
This is an example of the output created.
['P', 'O', 'P', 'U', 'L', 'A', 'T', 'I', 'O', 'N']
['5', '2', '2', ',', '8', '1', '8']
['1', '5', '5', ',', '6', '5', '6']
['9', '6', '6', ',', '7', '0', '9']
['7', '7', '3', ',', '8', '8', '7']
['8', ',', '4', '4', '7', ',', '6', '0', '9']
['1', '4', ',', '4', '8', '4', ',', '2', '4', '2']
['1', ',', '3', '6', '4', ',', '4', '0', '0']
['1', ',', '1', '7', '1', ',', '0', '2', '7']
['4', ',', '3', '5', '0', ',', '9', '0', '1']
['5', ',', '0', '4', '6', ',', '7', '8', '0']
['4', '0', ',', '6', '0', '1']
['4', '4', ',', '9', '0', '9']
['3', '8', ',', '6', '6', '6']
I need it to all be in one list, like [522,818 , 155,656 , etc]
Assuming you would like to concatenate the rows from a csv containing a list in each row, such that an input csv looking like:
population
1,2
3,4
would print -> [1,2,3,4]
You can use the extend function on the python list builtin.
Here's how it would look:
import csv
with open('example.csv') as ff:
reader = csv.reader(ff)
reader.next() # skip the header that you arent using
concat_output = []
for row in reader:
concat_output.extend(row)
print(concat_output)
Perhaps this is what you are looking for:
>>>''.join(['5', '2', '2', ',', '8', '1', '8'])
'522,818'
I just found this earlier thread which provides more background/terminology: How to concatenate items in a list to a single string?.

Python: How to convert a set of strings that contains commas represented as an element of a list to a sub-list?

I have a list that contains a set of strings like this:
list = ['235,ACCESS,19841136,22564960,4291500,20,527434,566876','046,ALLOWED,24737321,27863065,1086500,3,14208500,14254500']
I'm trying to make the elements of the list a sublist but without splitting the string.
I tried new_list = list(map(list, list)). This is the result taking as reference the first element of the list:
print(new_list[0]):
[['2', '3', '5', ',', 'A', 'C', 'C', 'E', 'S',',','1', '9', '8', '4', '1', '1', '3', '6', ',', '2', '2', '5', '6', '4', '9', '6', '0', ',', '4', '2', '9', '1', '5', '0', '0', ',', '2', '0', ',', '5', '2', '7', '4', '3', '4', ',', '5', '6', '6', '8', '7', '6']]
I would like this output:
print(new_list[0]):
[[235,'ACCESS',19841136,22564960,4291500,20,527434,566876]]
Thanks in advance for your help!
You can try split() with delimiter , like this -
new_list = [i.split(',') for i in list]
print (new_list[0])
Output:
['235', 'ACCESS', '19841136', '22564960', '4291500', '20', '527434', '566876']
One thing is that here the numbers are also represented as string. If you want integers instead you can use isdigit() method like this -
new_list = [[int(e) if e.isdigit() else e for e in i.split(',') ]for i in list]
print(new_list[0])
Output:
[235, 'ACCESS', 19841136, 22564960, 4291500, 20, 527434, 566876]
Also, please try to avoid naming your list list

Split list on smaller lists with equal elements

In my list "A" i have got numbers and ' ', so I want to make a list of list named e.g "b", every list should have nine number (if it possible), no matter how much it have ' '.
Any idea how to do this?
A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
B = [ ['1', '3, '4', '5', '7', '8', '9', ' ', '13', '16'], ['3', ' ', '5', '17'] ]
This will help you:
>>> a = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
>>> b=[a[i:i+9] for i in xrange(0,len(a),9)]
>>> b
[['1', '3', '4', '5', '7', '8', '9', ' ', '13'], ['16', '3', ' ', '5', '17']]
>>>
This can be done with two nested while loops:
>>> A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
>>> B = []
>>> while A:
... L = []
... c = 0
... while A and c < 9:
... L.append(A.pop(0))
... if L[-1].isdigit():
... c += 1
... B.append(L)
...
>>> B
[['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16'], ['3', ' ', '5', '17']]
The outer one loops while A is not empty and the inner one while A is not empty and the number of digit only strings appended to the current sub-list is less than 9. The counter is only incremented after a string consisting of only digits is found.
It would be worth your time to get deep into list comprehensions
And there is no xrange in Python 3.x or rather range (in 3.x) does exactly what xrange did in Python 2.x.
A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
B = [i for i in A[0:9]] #is cleaner.
Though I'm not sure exactly what your goal is. Do you want the second list (the remainder list as I'm thinking of it) to be in the same variable? So if you had 28 elements in your list you'd want three lists of 9 and one list of 1?
This is a bit dirty solution but I think you might need to check isdigit part and pop.
def take(lst, n):
if not lst:
return ValueError("Empty list, please check the list.")
items = list(lst)
new_list = []
count = 0
while items:
item = items.pop(0)
new_list.append(item)
if item.isdigit():
count += 1
if count >= n:
yield new_list
new_list = []
count = 0
if new_list:
yield new_list
A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
B = [ii for ii in take(A, 9)]
#[['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16'], ['3', ' ', '5', '17']]
Check the following:
https://docs.python.org/2/library/stdtypes.html#str.isdigit

[Optimizing]Appending Numbers from a list with delimeters

So I am working on a piece of code, and I am trying to find out the most efficient and fastest method to split the list into the numbers I need.
Here is the code that I am using:
eq=[' ', '1', '.', '3', '3', '5', '9', '2', '0', 'e', '0', '6', ' ', '4', '.', '0', '2', '0', '7', '4', '9', 'e', '0', '1']
coeff=[]
i=0
while i < len(eq)-1:
temp=""
if eq[i]==' ':
for x in range(i+1,len(eq)):
if eq[x]== ' ':
break
else:
temp+=eq[x]
coeff.append(float(temp))
i=x
print coeff
This does give me the desired result, which is [1335920.0, 40.20749], but I am wondering if there is a better way to do this.
Please note that the numbers are coming from R, and as such, I cannot guarantee they will be in exactly the same format all the time, so slicing the list is not an option.
In [1]: eq = [' ', '1', '.', '3', '3', '5', '9', '2', '0', 'e', '0', '6', ' ', '4', '.', '0', '2', '0', '7', '4', '9', 'e', '0', '1']
In [2]: map(float, ''.join(eq).strip().split(' '))
Out[2]: [1335920.0, 40.20749]
Explanation:
''.join(eq) joins the strings from the list
strip() removes the leading and trailing whitespace
split(' ') splits the string by a single space
map applies float to each string in the list

Categories

Resources