Related
I have these lists in Python
text_0 = ['Weight','Weight', 'Weight']
text_1 = [['x','y','z'],['x','y','z'],['x','y','z']]
text_2 = [['1','2,','3'],['4','5','6'],['7','8','9']]
I would like would to create a new list like this
new_list = ['Weight','x','1','y','2','z','3','Weight','x','4','y','5','z','6','Weight','x','7','y','8','z','9']
How do I do this in Python?
In your case we can do with python
sum([[x] + [ items for item in zip(y,z) for items in item]
for x, y, z in zip(text_0,text_1,text_2)],[])
['Weight', 'x', '1', 'y', '2', 'z', '3', 'Weight', 'x', '4', 'y', '5', 'z', '6', 'Weight', 'x', '7', 'y', '8', 'z', '9']
Any suggestion how to merge it better so the dual digits numbers does not split?
Sorry for bad english.
def merge(strArr):
newList = []
for x in range(len(strArr)):
newList += strArr[x]
return newList
array_test = ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
print(merge(array_test))
output =['1', ',', ' ', '3', ',', ' ', '4', ',', ' ', '7', ',', ' ', '1', '3', '1', ',', ' ', '2', ',', ' ', '4', ',', ' ', '1', '3', ',', ' ', '1', '5']`
expected output= [1,2,3,4,7,13,1,2,4,13,15]
Using list comprehension:
merged_arr = [n for s in array_test for n in s.split(", ")]
print(merged_arr)
This prints:
['1', '3', '4', '7', '13', '1', '2', '4', '13', '15']
It merges this way because for lists += is an array concatenation and that in this context your string object is interpreted as an array of characters:
[] += "Hello"
# Equivalent to
[] += ["H", "e", "l", "l", "o"]
If you want to join strings you can do:
out = "".join(array_test)
Your result becomes the way it is, because you take each inner string and add each character of it to your return-list without removing any spaces or commas.
You can change your code to:
def merge(strArr):
new_list = []
for inner in strArr:
new_list.extend(inner.split(", ")) # split and extend instead of += (== append)
return new_list
array_test =["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
merged = merge(array_test)
as_int = list(map(int,merged))
print(merged)
print(as_int)
Output:
['1', '3', '4', '7', '13', '1', '2', '4', '13', '15']
[1, 3, 4, 7, 13, 1, 2, 4, 13, 15]
Without as_int()you will still hav strings in your list, you need to convert them into integers.
I have a dictionary of lists, each list has 3 elements, i want to convert the 2nd and 3rd element to ints from strings
dict = {1:['string', '2', '3',],
2:['string', '2', '3',],
3:['string', '2', '3',],}
to become:
dict = {1:['string', 2, 3,],
2:['string', 2, 3,],
3:['string', 2, 3,],}
Thank you
Firstly, don't name your dictionaries dict, as it's a reserved keyword and you don't wanna overwrite it.
Coming to your solution.
d = {
1: ['string', '23', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3'],
}
d2 = {
k: [
int(i) if i.isdigit() else i
for i in v
]
for k, v in d.items()
}
Will give you an output of:
{
1: ['string', '23', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3']
}
{
1: ['string', 23, 3],
2: ['string', 2, 3],
3: ['string', 2, 3]
}
If you dictionary has many elements you might not want to create a second dictionary in memory, but modify the existing one in-place:
data = {
1: ['string', '2', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3'],
}
for v in data.values():
v.append(int(v.pop(1)))
v.append(int(v.pop(1)))
print(data)
Output:
{1: ['string', 2, 3], 2: ['string', 2, 3], 3: ['string', 2, 3]}
array([
['192', '895'],
['14', '269'],
['1', '23'],
['1', '23'],
['50', '322'],
['19', '121'],
['17', '112'],
['12', '72'],
['2', '17'],
['5,250', '36,410'],
['2,546', '17,610'],
['882', '6,085'],
['571', '3,659'],
['500', '3,818'],
['458', '3,103'],
['151', '1,150'],
['45', '319'],
['44', '335'],
['30', '184']
])
How can I remove some of the rows and left the array like:
Table3=array([
['192', '895'],
['14', '269'],
['1', '23'],
['50', '322'],
['17', '112'],
['12', '72'],
['2', '17'],
['5,250', '36,410'],
['882', '6,085'],
['571', '3,659'],
['500', '3,818'],
['458', '3,103'],
['45', '319'],
['44', '335'],
['30', '184']
])
I removed the index 2,4,6. I am not sure how should I do it. I have tried few ways, but still can't work.
It seems like you actually deleted indices 2, 5, and 10 (not 2, 4 and 6). To do this you can use np.delete, pass it a list of the indices you want to delete, and apply it along axis=0:
Table3 = np.delete(arr, [[2,5,10]], axis=0)
>>> Table3
array([['192', '895'],
['14', '269'],
['1', '23'],
['50', '322'],
['17', '112'],
['12', '72'],
['2', '17'],
['5,250', '36,410'],
['882', '6,085'],
['571', '3,659'],
['500', '3,818'],
['458', '3,103'],
['151', '1,150'],
['45', '319'],
['44', '335'],
['30', '184']],
dtype='<U6')
How do I convert this list of lists:
[['0', '1'], ['0', '2'], ['0', '3'], ['1', '4'], ['1', '6'], ['1', '7'], ['1', '9'], ['2', '3'], ['2', '6'], ['2', '8'], ['2', '9']]
To this list of tuples:
[(0, [1, 2, 3]), (1, [0, 4, 6, 7, 9]), (2, [0, 3, 6, 8, 9])]
I am unsure how to implement this next step? (I can't use dictionaries,
sets, deque, bisect module. You can though, and in fact should, use .sort or sorted functions.)
Here is my attempt:
network= [['10'], ['0 1'], ['0 2'], ['0 3'], ['1 4'], ['1 6'], ['1 7'], ['1 9'], ['2 3'], ['2 6'], ['2 8'], ['2 9']]
network.remove(network[0])
friends=[]
for i in range(len(network)):
element= (network[i][0]).split(' ')
friends.append(element)
t=len(friends)
s= len(friends[0])
lst=[]
for i in range(t):
a= (friends[i][0])
if a not in lst:
lst.append(int(a))
for i in range(t):
if a == friends[i][0]:
b=(friends[i][1])
lst.append([b])
print(tuple(lst))
It outputs:
(0, ['1'], ['2'], ['3'], 0, ['1'], ['2'], ['3'], 0, ['1'], ['2'], ['3'], 1, ['4'], ['6'], ['7'], ['9'], 1, ['4'], ['6'], ['7'], ['9'], 1, ['4'], ['6'], ['7'], ['9'], 1, ['4'], ['6'], ['7'], ['9'], 2, ['3'], ['6'], ['8'], ['9'], 2, ['3'], ['6'], ['8'], ['9'], 2, ['3'], ['6'], ['8'], ['9'], 2, ['3'], ['6'], ['8'], ['9'])
I am very close it seems, not sure what to do??
A simpler method:
l = [['0', '1'], ['0', '2'], ['0', '3'], ['1', '4'], ['1', '6'], ['1', '7'], ['1', '9'], ['2', '3'], ['2', '6'], ['2', '8'], ['2', '9']]
a=set(i[0] for i in l)
b=list( (i,[]) for i in a)
[b[int(i[0])][1].append(i[1]) for i in l]
print(b)
Output:
[('0', ['1', '2', '3']), ('1', ['4', '6', '7', '9']), ('2', ['3', '6', '8', '9'])]
Alternate Answer (without using set)
l = [['0', '1'], ['0', '2'], ['0', '3'], ['1', '4'], ['1', '6'], ['1', '7'], ['1', '9'], ['2', '3'], ['2', '6'], ['2', '8'], ['2', '9']]
a=[]
for i in l:
if i[0] not in a:
a.append(i[0])
b=list( (i,[]) for i in a)
[b[int(i[0])][1].append(i[1]) for i in l]
print(b)
also outputs
[('0', ['1', '2', '3']), ('1', ['4', '6', '7', '9']), ('2', ['3', '6', '8', '9'])]
You can use Pandas:
import pandas as pd
import numpy as np
l = [['0', '1'], ['0', '2'], ['0', '3'], ['1', '4'], ['1', '6'], ['1', '7'], ['1', '9'], ['2', '3'], ['2', '6'], ['2', '8'], ['2', '9']]
df = pd.DataFrame(l, dtype=np.int)
s = df.groupby(0)[1].apply(list)
list(zip(s.index, s))
Output:
[(0, [1, 2, 3]), (1, [4, 6, 7, 9]), (2, [3, 6, 8, 9])]