Related
L1 =['0-0-3-0-0-80-0', '0-0-3-0-0-82-0']
L2 = [['0', '4', '0', '0', '42', '71','42','0-0-0-0-0-4-0'],['0', '4', '2', '0', '42', '72','42', '0-0-0-1-0-4-2'],['0', '80', '0', '0', '42', '81','43', '0-0-3-0-0-80-0'],['0', '80', '0', '1', '21', '81','43', '0-0-3-0-0-80-0'],['0', '81', '0', '0', '43', '82', '21', '0-0-3-1-0-81-0',],['0', '82', '0', '0', '21', '83', '43', '0-0-3-0-0-82-0']]
So I want to search L1's values in L2 lists if code finds the value
'0-0-3-0-0-80-0' is in ['0', '80', '0', '0', '42', '81','43', '0-0-3-0-0-80-0'] and ['0', '80', '0', '1', '21', '81','43', '0-0-3-0-0-80-0']
and
'0-0-3-0-0-82-0' is in ['0', '82', '0', '0', '21', '83', '43', '0-0-3-0-0-82-0']
Last result will be shown as like that
L2 = [['0', '4', '0', '0', '42', '71','42','0-0-0-0-0-4-0',""],['0', '4', '2', '0', '42', '72','42', '0-0-0-1-0-4-2',""],['0', '80', '0', '0', '42', '81','43', '0-0-3-0-0-80-0',"found"],['0', '80', '0', '1', '21', '81','43', '0-0-3-0-0-80-0',"found"],['0', '81', '0', '0', '43', '82', '21', '0-0-3-1-0-81-0',],['0', '82', '0', '0', '21', '83', '43', '0-0-3-0-0-82-0',"found"]]
for i in range(0,len(L2)):
for x in L1:
if x in L2[i]:
result.append(L2[i]+["found"])
else:
result.append(L2[i]+[""])
I tried this but it duplicates results two times.
The code creates results for two times.
It sounds like you want to append "found" or "" to each list in L2.
The code below appends "" by default and overwrites it with "found" each time something is found. There is no result variable needed:
for i in range(0,len(L2)):
if len(L2[i]) == 8:
L2[i].append('')
for x in L1:
if x in L2[i]:
L2[i][8] = 'found'
Output as requested
The Data that I have is like :
A = [1,2,3,4,5,6,7,8,~9,10,11,12,13,14,15,16,~17,18,19,20,21,22,23,24,~GV=15,ld=34,gain=15,c=12,ld=45,bpm=12,#*#31,32,33,34,35,36,37,38,~39,40,41,42,43,44,45,46,~47,48,49,50,51,52,53,54,~GV=15,ld=34,gain=15,c=12,ld=100,bpm=130]
I need them to be separated like:
B = [
1,2,3,4,5,6,7,8,
9,10,11,12,13,14,15,16,
17,18,19,20,21,22,23,24,
31,32,33,34,35,36,37,38,
39,40,41,42,43,44,45,46,
47,48,49,50,51,52,53,54
]
C = [
GV=15,ld=34,gain=15,c=12,ld=45,bpm=12
GV=15,ld=34,gain=15,c=12,ld=100,bpm=130
]
What should I do to get an output like this in python?!
A_str = "1,2,3,4,5,6,7,8,~9,10,11,12,13,14,15,16,~17,18,19,20,21,22,23,24,~GV=15,ld=34,gain=15,c=12,ld=45,bpm=12,#*#31,32,33,34,35,36,37,38,~39,40,41,42,43,44,45,46,~47,48,49,50,51,52,53,54,~GV=15,ld=34,gain=15,c=12,ld=100,bpm=130"
A = A_str.split(',')
new_A = []
for elem in A:
if elem[0] == '~':
new_A.append(elem[1:])
elif len(elem) > 3 and elem[0:3] == "#*#":
new_A.append(elem[3:])
else:
new_A.append(elem)
A = new_A
B = []
C = []
for elem in A:
try:
B.append(int(elem))
except ValueError:
C.append(elem)
print(B)
print(C)
You can use python list comprehension for pulling out the elements that match your pattern. I don't exactly know what your pattern is, but it looks like if it has an "=" you'd like to assign it to C, otherwise you'd like to assign it to B. Additionally, you have cleaned some other characters. I'm going to assume you don't care about data types and are happy to have lists of strings.
# you'll need the re library to help you clean up the data
import re
# store your data as a string
A = "1,2,3,4,5,6,7,8,~9,10,11,12,13,14,15,16,~17,18,19,20,21,22,23,24,~GV=15,ld=34,gain=15,c=12,ld=45,bpm=12,#*#31,32,33,34,35,36,37,38,~39,40,41,42,43,44,45,46,~47,48,49,50,51,52,53,54,~GV=15,ld=34,gain=15,c=12,ld=100,bpm=130"
# convert to a list
x = A.split(",")
# create B output based on elements that don't have "=" and keep only the numbers
B = [re.search(r'[0-9]+', i).group(0) for i in x if "=" not in i]
# ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54']
# create C output based on elements that have "=" and remove "~"
C = [i.replace("~","") for i in x if "=" in i]
# ['GV=15', 'ld=34', 'gain=15', 'c=12', 'ld=45', 'bpm=12', 'GV=15', 'ld=34', 'gain=15', 'c=12', 'ld=100', 'bpm=130']
edit: of course, if you do care about data types you can intergerise those strings pretty safely. C has to be strings really.
B = [int(re.search(r'[0-9]+', i).group(0)) for i in x if "=" not in i]
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54]
edit2: The data structures you've illustrated in the question are not something that exist in python but it looks like a list of lists. So this would be the solution where you want lists of lists where we include the delimiters "~" and "#*#"
import re
# store your data as a string
A = "1,2,3,4,5,6,7,8,~9,10,11,12,13,14,15,16,~17,18,19,20,21,22,23,24,~GV=15,ld=34,gain=15,c=12,ld=45,bpm=12,#*#31,32,33,34,35,36,37,38,~39,40,41,42,43,44,45,46,~47,48,49,50,51,52,53,54,~GV=15,ld=34,gain=15,c=12,ld=100,bpm=130"
# convert to a list
x = re.split('~|#\*#', A)
B = [ list(filter(None, i.split(","))) for i in x if "=" not in i]
C = [list(filter(None, i.split(","))) for i in x if "=" in i]
print(B)
#[['1', '2', '3', '4', '5', '6', '7', '8'], ['9', '10', '11', '12', '13', '14', '15', '16'], ['17', '18', '19', '20', '21', '22', '23', '24'], ['31', '32', '33', '34', '35', '36', '37', '38'], ['39', '40', '41', '42', '43', '44', '45', '46'], ['47', '48', '49', '50', '51', '52', '53', '54']]
print(C)
#[['GV=15', 'ld=34', 'gain=15', 'c=12', 'ld=45', 'bpm=12'], ['GV=15', 'ld=34', 'gain=15', 'c=12', 'ld=100', 'bpm=130']]
I have this list that's a summary of a few NHL player stats in 2018. I want to sort them by pts which is the 7th value using bubble. I am aware of the built-in sort function on python but I would rather use bubble sort or even quicksort for that matter. Can anyone help out?
[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
This is what I did so far:
def sortByPoints(stats):
lengthOfstats = len(stats) - 1
for i in range(lengthOfstats):
for j in range(lengthOfstats - i):
if stats[j] < stats[j + 1]:
stats[j], stats[j + 1] = stats[j + 1], stats[j]
return stats
print(sortByPoints(readStatsFromFile()))
Create bubble sort that can sort nested-arrays based upon an index of sub-array
Modification of BubbleSort
def bubbleSort(arr, ind = 6):
"""Bubble sort arr based upon subelement ind (default of index 6)
which is 7th element of sub-array since 0 based indexing"""
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if int(arr[j][ind]) > int(arr[j+1][ind]) :
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
Test
arr = [['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
import pprint
print('pre-sorted')
pprint.pprint(arr)
print('sorted')
pprint.pprint(bubbleSort(arr))
Output
pre-sorted
[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'],
['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'],
['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'],
['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'],
['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'],
['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
sorted
[['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'],
['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'],
['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'],
['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15'],
['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'],
['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88']]
After reading from a file I have a list of lists contaning not only digits but also other characters, which I would like to get rid of.
I've tried using re.sub function but this doesn't seem to work
import re
Poly_id= [['0', '[4', '8', '18', '20', '5', '0', '4]'], ['1', '[13', '16',
'6', '11', '13]'], ['2', '[3', '1', '10', '9', '2', '15', '3]'], ['3',
'[13', '12', '16', '13]'], ['4', '[13', '11', '17', '14', '7', '13]']]
for x in Poly_id:
[re.sub(r'\W', '', ch) for ch in x]
This doesn't seem to change a thing in this list.
I would like to have a list with only numbers as elements so that I could convert them into integers
I guess technically [4 is non numeric so you can do something like this:
Poly_id = [[char for char in _list if str.isnumeric(char)] for _list in Poly_id]
Output:
['0', '8', '18', '20', '5', '0']
['1', '16', '6', '11']
['2', '1', '10', '9', '2', '15']
['3', '12', '16']
['4', '11', '17', '14', '7']
If you just want to remove the non numeric values and not the complete entry then you can do this:
Poly_id = [[''.join(char for char in substring if str.isnumeric(char)) for substring in _list] for _list in Poly_id]
Output:
['0', '4', '8', '18', '20', '5', '0', '4']
['1', '13', '16', '6', '11', '13']
['2', '3', '1', '10', '9', '2', '15', '3']
['3', '13', '12', '16', '13']
['4', '13', '11', '17', '14', '7', '13']
Here a solution if you want to get rid of the '[' in '[4' but keep the '4':
res = [[re.sub(r'\W', '', st) for st in inlist] for inlist in Poly_id]
res is:
[
['0', '4', '8', '18', '20', '5', '0', '4'],
['1', '13', '16', '6', '11', '13'],
['2', '3', '1', '10', '9', '2', '15', '3'],
['3', '13', '12', '16', '13'],
['4', '13', '11', '17', '14', '7', '13']
]
You can use a module, "itertools"
import itertools
list_of_lists = [[1, 2], [3, 4]]
print(list(itertools.chain(*list_of_lists)))
>>>[1, 2, 3, 4]
This question already has answers here:
Element-wise string concatenation in numpy
(6 answers)
Closed 4 years ago.
I have following code:
a = np.arange(25).reshape(5,5)
b = np.arange(25).reshape(5,5)
c = a.astype( str )
d = b.astype( str )
Matrix a and b are:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
Matrix c and d are:
array([['0', '1', '2', '3', '4'],
['5', '6', '7', '8', '9'],
['10', '11', '12', '13', '14'],
['15', '16', '17', '18', '19'],
['20', '21', '22', '23', '24']], dtype='|S11')
I want to get the following matrix by using c and d manipulation.
array([['00', '11', '22', '33', '44'],
['55', '66', '77', '88', '99'],
['1010', '1110', '1212', '1313', '1414'],
['1515', '1616', '1717', '1818', '1919'],
['2020', '2121', '2222', '2323', '2424']], dtype='|S11')
How to do?
Why not do
>>> np.core.defchararray.add(a.astype(str), b.astype(str))
array([['00', '11', '22', '33', '44'],
['55', '66', '77', '88', '99'],
['1010', '1111', '1212', '1313', '1414'],
['1515', '1616', '1717', '1818', '1919'],
['2020', '2121', '2222', '2323', '2424']], dtype='<U22')
Reproducibility material
import numpy as np
a = np.array(
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]
)
b = np.array(
[['0', '1', '2', '3', '4'],
['5', '6', '7', '8', '9'],
['10', '11', '12', '13', '14'],
['15', '16', '17', '18', '19'],
['20', '21', '22', '23', '24']],
dtype='|S11'
)