Python: How to print position and value of list element? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list which includes infinite an unknown number of elements (I don't have control on the length of the list or the type of values)
a = [x, y, z ...]
where x = 1, y = 9, z = 'Hello' ...
And I would like to loop over list "a" and print all the elements' names and values.
I hope if I can implement something like that:
for i in a:
print i $i
I would like to have output as:
x 1
y 9
z Hello

from itertools import cycle # an infinite repeating set
my_infinite = cycle(["a","b","c"])
a=1,b=4,c=99
for val in my_infinite:
print val, globals()[val]
maybe ... its aweful and hackey but it might work

Something like this:
import string
for a, b in zip(string.ascii_lowercase, xrange(1, 27)):
print a, b

You should use a dictionary:
a = {'x': 1, 'y': 2}
for i in a:
print i, a[i]

Related

Deleting max value in set without max() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a working code
task = set()
x = 1
while x != 0:
x = int(input('input your number '))
task.add(x)
print('Just 0 could stop it!')
task.remove(max(task))
print(max(task))
And need to get the same result without using max(). What could be an alternative?
Something like this, unless you have really large sets, I don`t see the advantage
task = set()
x = 1
m = 0
while x != 0:
x = int(input('input your number '))
task.add(x)
if x > m:
m = x
print('Just 0 could stop it!')
task.remove(m)
print(max(task))
Notice this will only work for positive numbers, if you want to the complete int range you should init m like this m = -sys.maxsize - 1
You could use min with a key arg that inverts the element:
>>> task = {1, 2, 3, 4, 5}
>>> max(task)
5
>>> min(task, key=lambda x: -x)
5
Or you could sort it and take the last element...
>>> sorted(task)[-1]
5

How to make a list of lists? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is there some way to create a list of lists in Python? Analogous to zeroes or similar functions.
What I am thinking of is something like this (pseudocode to follow):
def listoflists(nlists, nitems, fill)
...
listoflists(2,3, -1) # returns [[-1,-1,-1],[-1,-1,-1]]
Not sure if there's a library function to do it, but here's how I would do it:
def listoflists(nList, nItem, fill):
return [[fill for _ in range(nItem)] for _ in range(nList)]
This uses list comprehension to make a list of size nItem nList times.
You can create a list
root_list = []
and just append your sublists
root_list.append([1, 1, 1])
just need to do this:
def list_of_list(n_list, n_elements, fill):
x = [fill] * n_elements
y = [x] * n_list
return y
print(list_of_list(2, 3, -1))
Bye.
This should work, thanks!
def listoflists(nlists, nitems, fill):
f_list = []
for i in range(nlists):
n_list = []
for j in range(nitems):
n_list.append(fill)
f_list.append(n_list)
n_list = []
return f_list
print(listoflists(2,3,-1))

How can I generate all possible words with a specified set of characters? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I play to HackNet game and i have to guess a word to bypass a firewall.
The key makes 6 characters long and contains the letters K,K,K,U,A,N.
What is the simplest way to generate all possible combinations either in bash or in python ? (bonus point for bash)
Here is a backtracking-based solution in Python:
db = {"K" : 3, "U" : 1, "A" : 1, "N" : 1}
N = 6
def possibilities(v):
l = []
for k in db.keys():
if v.count(k) < db[k]:
l.append(k)
return l
def generateImpl(a):
if len(a) < N:
lst = []
for c in possibilities(a):
lst += generateImpl(a+[c])
return lst
else:
return [''.join(a)]
def generate():
return generateImpl([])
Just run generate() to obtain a list of possible words.
You have 6 letters and need to find a combination of 6 letters. If you are not using the same character in ['K','K','K','U','A','N'] again and again, then there is only 1 permutation.
If you can use the same character again and again, you can use the following code to generate all possible combinations.
import itertools
y = itertools.combinations_with_replacement(['K','U','A','N'],6)
for i in list(y):
print(i)

Compare values stored in variables, then print the variable name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have tried to use the max code but it printed out the largest number not the variable name.
a=3
b=4
c=7
d=6
e=8
max(a,b,c,d,e)
this is the code I've tried but it printed 8 instead of e.
forgot to add that my values for a b c etc came from a loop so im not suppose to know their values myself...
You need to use a dictionary or a named tuple to do something like that.
>>> my_list = {'a': 1, 'b': 2, 'c': 3}
>>> max(my_list, key=my_list.get)
'c'
Let me know if there are any other methods.
Try this
dct={'a':3 ,'b':4 ,'c':7 ,'d':6 ,'e':8}
for i, j in dct.iteritems():
if j == max(dct.values()):
print i
From this question, there seems to be a way to retrieve the name of your variable (see caveat below before using):
a = 3
b = 4
c = 7
d = 6
e = 8
m = max(a, b, c, d, e)
for k, v in list(locals().items()):
if v is m:
v_as_str = k
break
v_as_str
output:
'e'
Caveat:
This may work if you can be sure that there are no other (unrelated) variables with the same value as the max of a to e
somewhere in your code, and appearing earlier in the locals.
Thanks to #D.Everhard & #asongtoruin in the comments

How to form a given list of elements in python to create another set of lists from it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
List1 =['000095', '000094', '000092', '000101', '000099', '000096', '000095']
def makecycle(list, startElement):
A loop which forms the bottom list which is made from the upper one's elements!
if i pass that function the start element and the list it shoul print like this:
makecycle(list1, 000094) it should print:
['000094', '000092', '000101', '000099', '000096', '000095', '000094']
and if pass
makecycle(list1, 000101) it should print:
['000101', '000099', '000096', '000095', '000094', '000092', '000101']
and if pass
makecycle(list1, 000092) it should print:
['000092', '000101', '000099', '000096', '000095', '000094', '000092']
i know its kinda not clear enough but thats all i can point!
def makecycle(list1,startElement):
ind = list1.index(startElement)
l = len(list1)
i = ind
list2 = []
while ind < (l-1):
list2.append(list1[ind])
ind = ind + 1
for x in range(i):
if list1[x] not in list2:
list2.append(list1[x])
print(list2)

Categories