2

I have a two variable, a = ("0", "2", "0") b = ("Jenny", "Christy", "Monica") if I run the code

x = min(zip(a, b))
print(x)

my output will be ('0', 'Jenny')

If I want the output- (('0', 'Jenny'),('0','Monica))- what should I do?

Jo Bi
  • 97
  • 7
  • sorry about the edit, did't know min() works on string too. are you using python or python3 ? zip() is different in the two see https://stackoverflow.com/questions/31683959/the-zip-function-in-python-3 – pippo1980 Jun 11 '21 at 08:55

3 Answers3

2

In fact, you want the min of a:

>>> [(i, j) for i, j in zip(a, b) if i == min(a)]
[('0', 'Jenny'), ('0', 'Monica')]

If you compare each tuple then ('0', 'Jenny') < ('0', 'Monica')

Corralien
  • 109,409
  • 8
  • 28
  • 52
0
a = ("0", "2", "0")
b = ("Jenny", "Christy", "Monica")
x = list(zip(a, b))
output = []

for i in range(len(x)):
    if min(a) in x[i]:
        output.append(x[i])

print(output)

Actually when your code finds two minimum values in a, then it compares the minimum value of b also and it finds 'Jenny' first letter J is smaller than 'Monica' first letter 'M'. Thats why it returns only one value

Rishabh Semwal
  • 328
  • 1
  • 3
  • 12
0
a = ("0", "2", "0")
b = ("Jenny", "Christy", "Monica")
x = ()
for i in range(len(a)):
    if a[i] == "0":
        x += (a[i],b[i])

print(x)



#Brute force way.
  • sorry to bother I'm on no question ban. Why do you think ff =['1c','2','3','4','5','6'] for i in ff: print(type(i), min(ff), type(min(ff))), gives 1c as minimum – pippo1980 Jun 11 '21 at 09:00
  • Well, I was just trying to help. I thought it would be affected because it's a string. lol – Celestine Akpanoko Jun 11 '21 at 09:10