Convert a file into a list with integers - list

with open('askhsh11.txt', 'r') as f:
raw_list = f.read().split('\n')
for i in range(len(raw_list)):
print raw_list[i].split(',')
for i in range(len(raw_list)):
raw_list[i]=int(i)
print raw_list
the result is :
['1', '2', '3', '4']
['5', '6', '7', '8']
[0, '5,6,7,8']
[0, 1]
but i want the result to be:
['1', '2', '3', '4']
['5', '6', '7', '8']
[1, 2, 3, 4]
[5, 6, 7, 8]
How i convert a list of strings into a integers?

you can use:
result = [int(c) for s in raw_list for c in s.split(',')]
output:
[1, 2, 3, 4, 5, 6, 7, 8]

you can just replace the "'" character to remove them
with open('askhsh11.txt', 'r') as f:
raw_list = f.read().replace("'","").split('\n')
numbers = [int(num) for num in raw_list]

To be honnest . i'm biginner in this langage . but i can tell you that you can use the 2 first output to generate the 2 second output .
what i mean that it's easy to convert
this :
['1', '2', '3', '4']
to this :
[1, 2, 3, 4] .
just use the function that convert the string like '1' to the integer 1 .
sorry i gorget its name but u find it on python support
for evryone who know the function's name .plz make a comment and thank you so much

Related

Multi-Row Calculation

Suppose I have the following dataframe:
df1 = {'Column_1': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'x': ['0', '1', '2', '3', '0', '1', '2', '3']}
df1 = pd.DataFrame (df1, columns = ['Column_1','x'])
df1
I want to create a new column called 'x!'. This is calculated by taking the value in the row 'x' and multiplying it be the row-1 of 'x!'. The value in the first row for 'x!' is 1. I need to calculation to reset when the value in 'Column_1' changes. The desired output would be the follwing:
df2 = {'Column_1': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'x': ['0', '1', '2', '3', '0', '1', '2', '3'],
'x!': ['1', '1', '2', '6', '1', '1', '2', '6']}
df2 = pd.DataFrame (df2, columns = ['Column_1','x', 'x!'])
df2
Where 'x' is 3, 'x!' is 6 because 3 x 2 (x! row-1 = 2) is equal to 6.
How would I do this?
Thanks
Try this simple and clear approach:
let df1 = {'Column_1': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],'x': ['0', '1', '2', '3', '0', '1', '2', '3']}
let newX = ['1'];
for(i=1; i<df1['x'].length; i++){
let check = parseInt(df1.x[i],10)*parseInt(df1.x[i-1],10)
if(check ===0){
newX.push('1')
}else{
newX.push(check.toString())
}
}
console.log(df1.x) //INITIAL ['0', '1', '2', '3', '0', '1', '2', '3']
console.log(newX) //RESULT ['1', '1', '2', '6', '1', '1', '2', '6']
After that you can assign your new x! array:
df1['x!'] = newX
And apply to DataFrame
df2 = pd.DataFrame (df2, columns = Object.keys(df1))
I hope this solves your case. :)

Python: binary vector

I have a set of indices:
indices = (['1', '1.2', '2', '2.2', '3', '4'])
and a dataset, where the first element identifies a person, the second a round, and the third is the index from the indices set:
dataset = [['A', '1', '1'], ['A', '1', '1.2'], ['B', '1', '2'], ['C', '2', '3']]
I would like to form a binary vector, where for each person and for each individual round, the indices are marked either present (with a 1) or not (with a 0).
The desired output would be something like so, where for A, the vector represents the presence of the indices 1 and 1.2, for B, the index 2, and for C, the index 3. Note that for A, there is only one record, but 2 indices are present.
['A', '1', '1, 1, 0, 0, 0, 0']
['B', '1', '0, 0, 1, 0, 0, 0']
['C', '2', '0, 0, 0, 0, 1, 0']
I'm having a bit of trouble getting my head around the looping of the indices over the dataset. My idea was to loop through the indices set the same amount of time as the number of lists in the dataset. But I dont think this is the most efficient way, and any help would be appreciated!
I'd do it something like this:
from itertools import groupby
for k, g in groupby(dataset, lambda x: x[:2]):
vals = [x[2] for x in g]
print(k + [", ".join("1" if x in vals else "0" for x in indices)])
Output
['A', '1', '1, 1, 0, 0, 0, 0']
['B', '1', '0, 0, 1, 0, 0, 0']
['C', '2', '0, 0, 0, 0, 1, 0']
Is this what you were looking for?
Here's a solution without loops
import pandas as pd
indlist=['1', '1.2', '2', '2.2', '3', '4']
dataset = [['A', '1', '1'], ['A', '1', '1.2'], ['B', '1', '2'], ['C', '2', '3']]
df=pd.DataFrame(dataset,columns=['player','round','ind']).set_index('ind').reindex(indlist)
ans=df.reset_index().pivot('player','ind','round').fillna(0)[1:]

Creating a dictionary from list of lists

I have a list of lists in the following format:
[['a'],['1'],['2'],['3'], ['b'],['4'],['5'],['6']]
My desired output is:
[['a', '1'], ['a', '2'], ['a','3'],['b', '4'],['b', '5'],['b', '6']]
or even better would be:
{'a':['1','2','3'], 'b':['4','5','6']}
Essentially, the "number values" are never the same size (think that a could include 1 2 and 3, and b could include 4 5 6 7 and 8, etc)
What would be the easiest way of doing this? Using regex?
Thanks
You can use a for loop and check if the element is a digit or not:
d = {}
for i in lst:
if not i[0].isdigit(): # Check if element is a digit. If not, add a key with the current value of i[0]
d[i[0]] = []
current = i[0]
else:
d[current].append(i[0])
Output:
>>> d
{'a': ['1', '2', '3'], 'b': ['4', '5', '6']}
This is assuming everything in the list is a string

Extract multi-digit numbers from a string in python 3

I am doing the algorithm challenges from HackerRank and one of the problems needs me to accept input in the form of strings of numbers formatted as follows:
3 4
12 14 16
1 2
3 4
5 6
Now, I know how to iterate through the lines and assign them where they need to go, but my issue is with the second line. The others are two two digit numbers so I've been extracting them by just referencing their index in the string. For example, the first line of numbers would be collected with string[0] and string[-1].
The second line, however is of indeterminate length, and may include numbers shorter or longer than three digits. How would I pull those out and assign them to variables? I'm sure there is probably a way to do it with RegEx, but I don't know how to assign multiple matches in one string to multiple variables.
import re
print(re.findall(r"(\d+)",x))
"x" being your line.This will return a list with all the number.
You mean this?
>>> import re
>>> s = """3 4
... 12 14 16
... 1 2
... 3 4
... 5 6"""
>>> m = re.findall(r'\b\d+\b', s, re.M)
>>> m
['3', '4', '12', '14', '16', '1', '2', '3', '4', '5', '6']
Just pickup each value in the final list and assign it to variables.
So if s is your string,
map(int, s.split())
yields a list of integers:
[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6]
That's basically what skamazin suggested.
Given:
>>> txt='''\
... 3 4
... 12 14 16
... 1 2
... 3 4
... 5 6'''
If the lines have meaning, you can do:
>>> [map(int, line.split()) for line in txt.splitlines()]
[[3, 4], [12, 14, 16], [1, 2], [3, 4], [5, 6]]
If the lines have no meaning, you just want all the digits, you can do:
>>> map(int, txt.split())
[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6]
If your source text has the possibility of strings that will not convert to integers:
>>> txt='''\
... 3 4
... 12 14 16
... 1 2
... 3 4
... 5 6
... text that won't be integers
... 99 100 101'''
You can use a conversion function:
>>> def conv(s):
... try:
... return int(s)
... except ValueError:
... return s
...
>>> [[conv(s) for s in line.split()] for line in txt.splitlines()]
[[3, 4], [12, 14, 16], [1, 2], [3, 4], [5, 6], ['text', 'that', "won't", 'be', 'integers'], [99, 100, 101]]
Or filter out the things that are not digits:
>>> map(int, filter(lambda s: s.isdigit(), txt.split()))
[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6, 99, 100, 101]

Find overlapping List in 2 Lists of Lists

I would like to find overlapping lists in two lists of lists.
ListLeft = [['A', 'B', 'C'], ['1', '2', '3', '4'], ['x', 'y'], ['one', 'two', 'three']]
ListRight = [['h', 'i', 'j'], ['A', 'B', 'C'], ['1', '2', '3', '4'], ['5', '6', '7'], ['x', 'y']]
Someone might have a solution to find/print content of overlapping lists and lists which are not in both lists
Maybe this is possible without importing modules.
This can be simply achieved by using loop:
overlap = []
for ll in ListLeft:
for lr in ListRight:
if ll == lr:
overlap.append(ll)
break
print overlap
>>> [['A', 'B', 'C'], ['1', '2', '3', '4'], ['x', 'y']]