insert in list of python - list

I want insert list in list_of_words includes some value such that any list placed in position -2 of the list_of_words but the data are not ordered as I want
def h(i):
a, b = i.split(';')
list_of_words = [['z', -1],['z', len(b)]]
for key, word in enumerate(a):
if b.find(word) != -1:
list_of_words.insert(-2, [word, b.find(word)])
print list_of_words
h("XMJYAUZ;MZJAWXU")
list
list_of_words = [['z', -1], ['z', 7]]
assumed after run code
list_of_words = [['z', -1], ['X', 5], ['M', 0], ['J', 2], ['A', 3], ['U', 6], ['Z', 1], ['z', 7]]
but
list_of_words = [['X', 5], ['M', 0], ['J', 2], ['A', 3], ['U', 6], ['Z', 1], ['z', -1], ['z', 7]]
why this is happen

If you want to place an item to the -2 position in a list, you will need to use
list_of_words.insert(-1, [word, b.find(word)])
instead of
list_of_words.insert(-2, [word, b.find(word)])
because the item will be inserted before the provided position rather than after.
Full code:
def h(i):
a, b = i.split(';')
list_of_words = [['z', -1], ['z', len(b)]]
for word in a:
if word in b:
list_of_words.insert(-1, [word, b.find(word)])
print list_of_words
h("XMJYAUZ;MZJAWXU")

Related

Zipp lists in python by iterating through a list of lists

I have two lists of lists as follows. To merge them, I usually do the following:
>>>from itertools import imap, ilist
>>>a = [1,2,3]
>>>b = [4,5,6]
>>> c = list(imap(list,izip(a,b)))
>>> c
[[1, 4]], [2, 5], [3, 6]]
However, now I have a list of list as follows:
[[1,2,3],
[4,5,6],
[7,8,9],
]
How do I iterate through each list and pass it to the izip function to obtain the following output:
[[1,4,7],[2,5,8],[3,6,9]]
Answer for edited Question:
>>> input_list=[[1,2,3],
[4,5,6],
[7,8,9],
]
Using map and zip:
>>> map(list,zip(*input_list))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Using imap and izip:
>>> list(imap(list,list(izip(*input_list))))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Answer for previous question:
By using list comprehension and two for loops:
input_list =[[[1],[2],[3]],
[[4],[5],[6]],
[[7],[8],[9]],
]
out_list = [[] for i in range(len(input_list))]
for each_row in input_list:
for i in range(len(each_row)):
out_list[i].extend(each_row[i])
print out_list
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
I think u need something like this:
input_list =[[1,2,3],
[4,5,6],
[7,8,9],
]
result = []
for i in range(len(input_list)):
temp = []
for list in input_list:
temp.append(list[i])
result.append(temp)
print result
result will be:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

How can I correct my list format?

first I write a test code:
a = [-0.2364, 2.2524, 8.4896,'a']
l = []
for i,each in enumerate(a,start=1):
l.append(["{}, {}".format(i,each)])
lst = str(l).translate(None, "'")
print lst
and the output is ok
[[1, -0.2364], [2, 2.2524], [3, 8.4896], [4, a]]
This is what I want like add serial number for each item in a list. And then I try use httplib read data and replace test list a.
a = response.read()
l = []
for i,each in enumerate(a,start=1):
l.append(["{}, {}".format(i,each)])
lst = str(l).translate(None, "'")
print a
print lst
But output is changed like :
[-0.035308122832456316]
[[1, [], [2, -], [3, 0], [4, .], [5, 0], [6, 3], [7, 5], [8, 3], [9, 0], [10, 8], [11, 1], [12, 2], [13, 2], [14, 8], [15, 3], [16, 2], [17, 4], [18, 5], [19, 6], [20, 3], [21, 1], [22, 6], [23, ]]]
lista seems ok. but outputlst is not what I expected.For this case I trying to get output like:
[-0.035308122832456316]
[[1,-0.035308122832456316]]
how can I fix this?Thanks!
As I mentioned above if a is a string '[-0.035308122832456316]' or even with multiple list values like this a = '[-0.035308122832456316,89427873479875]' simply use below code and it will work fine for you.
import ast
a = response.read()
a = ast.literal_eval(a) #Convert String representation of list to list
l = []
for i,each in enumerate(a,start=1):
l.append(["{}, {}".format(i,each)])
lst = str(l).translate(None, "'")
print a
print lst
Try working with a dictionary instead of a list:
a = [-0.2364, 2.2524, 8.4896, 'a']
l = {}
for i, each in enumerate(a, start=1):
l[i] = each
print l
To simulate your indication that a is a list with 1 item:
a = [-0.035308122832456316]
l = {}
for i, each in enumerate(a, start=1):
l[i] = each
print l
If, as others have indicated, that a is a string with value "[-0.035308122832456316]", then convert it to a list:
a = "[-0.035308122832456316]"
if isinstance(a, str):
a = [a[1:-1]]
l = {}
for i, each in enumerate(a, start=1):
l[i] = each
print l

Generate Successive Number Pairs - Prolog

I have some code that takes a given list of pairs of numbers and solves for chains of 7. However, it takes an obnoxious amount of time to solve for even one (well, i haven't solved for 1 yet and it has been a large amount of time). I was wondering if there was a better/more efficient way of coding this.
Here's what I did, with out the numbers in the list "L". (the list looks like such: L= [[1,2],[2,3],...])
length(L,LEN),
interval(N1,1,LEN),
interval(N2,1,LEN),
interval(N3,1,LEN),
interval(N4,1,LEN),
interval(N5,1,LEN),
interval(N6,1,LEN),
interval(N7,1,LEN),
nth1(N1,L,A),
nth1(N2,L,B),
nth1(N3,L,C),
nth1(N4,L,D),
nth1(N5,L,E),
nth1(N6,L,F),
nth1(N7,L,G),
nth1(2,A,A2),
nth1(1,B,B1),
A2 = B1,
nth1(2,B,B2),
nth1(1,C,C1),
B2 = C1,
nth1(2,C,C2),
nth1(1,D,D1),
C2 = D1,
nth1(2,D,D2),
nth1(1,E,E1),
D2 = E1,
nth1(2,E,E2),
nth1(1,F,F1),
E2 = F1,
nth1(2,F,F2),
nth1(1,G,G1),
F2 = G1,
nth1(2,G,G2),
nth1(1,A,A1),
G2 = A1,
R = (A,B,C,D,E,F,G).
If I understand your intention correctly, you can write this shorter as
use_module(library(clpfd)).
q(L,R) :-
[A,B,C,D,E,F,G] ins 1 .. 7,
R = [[A,B],[B,C],[C,D],[D,E],[E,F],[F,G],[G,A]],
permutation(L, R),
label([A,B,C,D,E,F,G]).
Example:
3 ?- q([[1,7],[2,3],[5,4],[3,1],[7,6],[6,5],[4,2]],X).
X = [[1, 7], [7, 6], [6, 5], [5, 4], [4, 2], [2, 3], [3, 1]] ;
X = [[2, 3], [3, 1], [1, 7], [7, 6], [6, 5], [5, 4], [4, 2]] ;
X = [[5, 4], [4, 2], [2, 3], [3, 1], [1, 7], [7, 6], [6, 5]] ;
X = [[3, 1], [1, 7], [7, 6], [6, 5], [5, 4], [4, 2], [2, 3]] ;
X = [[7, 6], [6, 5], [5, 4], [4, 2], [2, 3], [3, 1], [1, 7]] ;
X = [[6, 5], [5, 4], [4, 2], [2, 3], [3, 1], [1, 7], [7, 6]] ;
X = [[4, 2], [2, 3], [3, 1], [1, 7], [7, 6], [6, 5], [5, 4]] ;
false.
But your question is really unclear.
update: We can create the kind of lists we use above, of any length, with
vars(N, Vars):-
length(Vars, N).
pairs(Vars, N, Pairs):- % assuming vars(N, Vars)
N #> 0,
append(Vars,[A],[A|B]), % |B| = N
maplist( pair, Vars, B, Pairs).
pair( A, B, [A,B]).
Such that q/2 can be generalized as
gen_q(L,R) :-
length( L, N),
vars( N, Vars),
Vars ins 1 .. N,
pairs( Vars, N, R),
permutation(L, R),
label(Vars).
But computational feasibility of this for larger inputs is another matter entirely. The brute force of permutation/2 may have to be replaced with something more specific.
Also, the N results produced comprise a clear pattern; there's no need to re-enter the search to produce them all after the first one is found.

Finding the value of each letter in a word using map and reduce

I have a function wordScore(word, scoreList). The word is a string that consists of only letters and scoreList is a list containing the value of each letter in the alphabet:
scoreList = [ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1],
["f", 4], ["g", 2], ["h", 4], ["i", 1], ["j", 8],
["k", 5], ["l", 1], ["m", 3], ["n", 1], ["o", 1], ["p", 3],
["q", 10], ["r", 1], ["s", 1], ["t", 1], ["u", 1], ["v", 4],
["w", 4], ["x", 8], ["y", 4], ["z", 10] ]
I have to find the total score of a word and then return it using map and reduce. Example:
wordScore('agile', scoreList)
>>> 6
I believe that I am not able to do this problem because I am not sure what map does. I would really appreciate some help.
Second Problem:
So now I am given a list of letters that(lets say listOfwords = ["a", "am", "at", "apple", "bat", "bar", "babble", "can", "foo", "spam", "spammy", "zzyzva"]) When I input a list of letters in scoreList(Rack) (such as scoreList(["a", "s", "m", "t", "p"]) then it should output all possible words that can be made with the letters that are in listOfwords. So basically it would be like:
>>> scoreList(["a", "s", "m", "t", "p"])
[['a', 1], ['am', 4], ['at', 2], ['spam', 8]]
>>> scoreList(["a", "s", "m", "o", "f", "o"])
[['a', 1], ['am', 4], ['foo', 6]]
This is my code so far:
def scoreList(Rack):
test = [x for x in Dictionary if all(y in Rack for y in x)]
return test
However when I run this it only gives me the words that can be made with the letters and I'm not sure how to get the score for each word. Could I possibly use wordScore? Also same restriction apply.
Map creates a sequence by applying a function to every items in a sequence:
>>> map(lambda x: x + 1, [1, 2, 3])
[2, 3, 4]
So to solve your problem:
If you can use dict, loop over all letters in word and map it to its score. Then sum all letter scores with reduce:
def wordScore(word, score_list):
score_dict = dict(score_list)
letter_scores = map(lambda letter: score_dict[letter], word)
return reduce(lambda x, y: x + y, letter_scores)
If you cannot, loop over all letters in score list and multiply each score by number of occurence of letter in word, then sum all letter scores with reduce:
def wordScore(word, score_list):
letter_scores = map(
lambda letter_score: word.count(letter_score[0]) * letter_score[1],
score_list
)
return reduce(lambda x, y: x + y, letter_scores)
Read map documentation in python 2.7

Generating random lists in Python query

I want my program to take numbers from 1 to X and randomly distribute those numbers between X/2 numbers of lists Y times. I don't want a number to be repeated during one shuffle, also I don't want the lists to repeat at all. So if there is list [1,2] there shouldn't be another list containing 1 or 2 in same shuffle and there shouldn't be another [1,2] or [2,1] in the whole result.
This is what I came up with, however, it keeps on repeating numbers. Any adice?
import random
def Shuffler():
amount = int(raw_input("Numbers up to: "))
times = int(raw_input("Number of shuffles: "))
numberslist = range(1,amount+1)
twos = []
thisshuffle = []
final = []
while len(final) < (amount/2)*times:
twos = []
thisshuffle = []
while len(twos) < 2:
if len(numberslist)!=0:
randomnumber = random.choice(numberslist)
if (randomnumber in twos) or (randomnumber in thisshuffle):
numberslist.remove(randomnumber)
else:
twos.append(randomnumber)
thisshuffle.append(randomnumber)
numberslist.remove(randomnumber)
else:
numberslist = range(1,amount+1)
if (twos or list(reversed(twos))) not in final:
final.append(twos)
k=0
for i in range(times): #this shit prints shit
print "%s:" % (i+1), final[k:k+amount/2]
print
k = k + amount/2
Shuffler()
Shuffler()
As ccf pointed out, your requirements aren't trivial. A few more steps and you'd have a sudoku generator :)
I tried a few solutions but they either didn't produce random output or were rather inefficient. Ccf's solution is clearly well written but it seems to have the same problem; it produces ordered output (e.g. [1, 2], [1, 3], [1, 4], [1, 5], [1, 6]).
#cff - wouldn't it be better to use itertools.combinations rather than itertools.permutations to avoid generating repetitions?
Here's a "solution" that's quite similar to ccf's (doesn't produce random output either):
import itertools
def Shuffler():
amount = int(raw_input("Numbers up to: "))
times = int(raw_input("Number of shuffles: "))
rng = range(1, amount+1)
perms = list(itertools.combinations(rng, 2))
lst_single = []
lst_all = []
for p in perms:
if len(lst_all) >= times:
for i, lst in enumerate(lst_all):
print str(i+1) + ": ", lst
break
if len(lst_single) == amount/2:
lst_all.append(lst_single)
lst_single = []
elif p[0] < p[1]:
p = list(p)
lst_single.append(p)
Shuffler()
Output
Numbers up to: 6
Number of shuffles: 3
1: [[1, 2], [1, 3], [1, 4]]
2: [[1, 6], [2, 3], [2, 4]]
3: [[2, 6], [3, 4], [3, 5]]
And here's a slightly hackish solution that seems to produce the output you want but in an inefficient way. It relies on a set for filtering out unwanted combinations but still wastes resources producing them in the first place.
import random
def Shuffler():
amount = int(raw_input("Numbers up to: "))
times = int(raw_input("Number of shuffles: "))
rng = range(1, amount+1)
final = []
lst_len = amount/2
combos_unique = set()
while len(combos_unique) < lst_len*times:
combo_rand = random.sample(rng, 2)
if combo_rand[0] < combo_rand[1]:
combos_unique.add(tuple(combo_rand))
tmp = []
for combo in combos_unique:
tmp.append(list(combo))
if len(tmp) >= lst_len:
final.append(tmp)
tmp = []
for i, lst in enumerate(final):
print str(i+1) + ": ", lst
Shuffler()
Output
Numbers up to: 6
Number of shuffles: 3
1: [[2, 6], [4, 6], [5, 6]]
2: [[4, 5], [1, 3], [1, 6]]
3: [[3, 4], [2, 4], [3, 5]]
You don't want any repeated numbers in one shuffle, and any repeated list ... and so on. This is not an easy task. Plus another fact is that, the unique sets of numbers are fixed, which cannot be set too high. For example, if you set "Numbers up to:" 5, and "Number of shuffles: " 20, for sure you will get repeated numbers.
The issue with your code, I see, is in this if statement:
if (twos or list(reversed(twos))) not in final:
final.append(twos)
(twos or list(reversed(twos))) is logical OR, the result is twos, because twos is not empty. I suggest you change the if statement to:
if (twos not in final) and (list(reversed(twos)) not in final):
final.append(twos)
The following code (python 2.7x) uses permutations and shuffle to generate numbers list. Next, make the list unique (e.g., no [1,2] and [2,1] in same list). then, divide them into groups based on number of shuffles specified by the user. Press any letter, script will exit. Hope it helps:
from itertools import permutations
from random import shuffle
def Shuffler():
try:
amount = input("Numbers up to: ")
p = list(permutations(range(1, amount + 1), 2))
p_uniq = [list(x) for x in p if x[::-1] in p and x[0]<=x[1]]
shuf_max = len(p_uniq) /(amount / 2)
times = shuf_max + 1 # set a higher value to trigger prompt
while times > shuf_max:
shuffle(p_uniq) # shuffle the unique list in place
times = input("Number of shuffles (MAX %s): " % (shuf_max))
else:
for i, group in enumerate(list(zip(*[iter(p_uniq[: (amount /2) * times + 1])]* (amount/2)))):
print "%i: " % (i + 1), list(group)
Shuffler()
except:
print 'quitting...'
Shuffler()
Output:
Numbers up to: 10
Number of shuffles (MAX 9): 8
1: [[6, 7], [1, 9], [2, 5], [5, 9], [9, 10]]
2: [[1, 10], [3, 8], [4, 10], [8, 10], [1, 5]]
3: [[1, 4], [6, 8], [3, 6], [2, 4], [4, 7]]
4: [[2, 10], [5, 8], [3, 9], [1, 7], [4, 9]]
5: [[1, 2], [7, 9], [1, 3], [6, 9], [1, 6]]
6: [[2, 9], [4, 8], [3, 5], [8, 9], [7, 10]]
7: [[2, 7], [2, 3], [7, 8], [3, 7], [3, 10]]
8: [[3, 4], [2, 6], [5, 6], [5, 7], [4, 6]]
Numbers up to: